title: Connecting to socket on Tomcat?
body: <p>I'm trying to connect from a standalone applet to a servlet running on tomcat:</p>
  
  <p>Servlet</p>
  
  <pre><code>public void init(ServletConfig config) throws ServletException { 
      super.init(config);
      // Start a daemon thread 
      try { 
          daemonThread = new Daemon(this);
          daemonThread.start();
      }
      catch (Exception e) { 
      }
  }
  
  protected int getSocketPort() { 
      return 8080;
  }
  
  public void handleClient(Socket client){ 
      new ScribbleThread(this, client).start();
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      StringBuffer sb = new StringBuffer();
      sb.append("&lt;html&gt;&lt;body bgcolor=pink text=red&gt;");
      sb.append("&lt;h1 align=center&gt;RUNNING&lt;/h1&gt;&lt;hr&gt;");
      sb.append("&lt;/body&gt;&lt;/html&gt;");
      out.println(sb);
      out.close();
  }
  }
  </code></pre>
  
  <p>Servlet's <code>init()</code> creates this:</p>
  
  <pre><code>class Daemon extends Thread { 
  private ServerSocket serverSocket;
  private SocketServlet servlet;
  
  public Daemon(SocketServlet servlet) { 
  this.servlet = servlet;
  }
  
  public void run() { 
      try { 
          // Create a server socket to accept connections 
          serverSocket = new ServerSocket(servlet.getSocketPort());
      }
      catch (Exception e) { 
          return;
      }
      try { 
          while (true) { 
              try { 
                  servlet.handleClient(serverSocket.accept());
              }
              catch (IOException ioe) { 
              }
          }
      }
  </code></pre>
  
  <p>I have this deployed by eclipse to TomCat. My question is what address does my applet need to make the socket to? When <code>i visit http://localhost:8080/scrabServ/connect</code> I see the 'RUNNING' message from the <code>doGet()</code> so is that where it needs to point?</p>
  
  <p>Applet:</p>
  
  <pre><code>public static String testConnection(){
      InputStream in = null; 
      try { 
          // Make socket connection to servlet
          String servlet = new String("localhost/scrabServ/connect");
          Socket socket = new Socket(servlet, 8080); 
  </code></pre>
  
  <p>this gives me:</p>
  
  <pre><code>Exception in testConnection()java.net.UnknownHostException: localhost/scrabServ/connect
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
  at java.net.Socket.connect(Socket.java:529)
  at java.net.Socket.connect(Socket.java:478)
  at java.net.Socket.&lt;init&gt;(Socket.java:375)
  at java.net.Socket.&lt;init&gt;(Socket.java:189)
  at scribble.Scribble.testConnection(Scribble.java:41)
  at scribble.Scribble.main(Scribble.java:28)
  </code></pre>
  
  <p>and points to the <code>new Socket(servlet, 8080)</code> line. </p>