title: Server with multiclients via thread
body: <p>I'm having a bit of trouble, now I have looked at this tutorial
  <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html</a>
  This tutorial gives you a server that multiple clients can connect to, when they connect to the server they are told to go along with a knock knock job, now I understand how to transfer the data and what not, but how does the threads work?</p>
  
  <p>I'm working on a networked pong game where a server will hold the positions and pass them to the clients, now I have a client connected to the server and the ball position is passed to the client, works fine, a bit jumpy but I'm sure a thread with .sleep will help. but anyways my question is, how can i get my client to become a thread? and how can I store them?</p>
  
  <p>For example here is the knock knock server multiThread class</p>
  
  <pre><code>package knockKnockServer;
  
  import java.net.*;
  import java.io.*;
  
  public class KKMultiServerThread extends Thread {
  private Socket socket = null;
  
  public KKMultiServerThread(Socket socket) {
  super("KKMultiServerThread");
  this.socket = socket;
  }
  
  public void run() {
  
  try {
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
      BufferedReader in = new BufferedReader(
                  new InputStreamReader(
                  socket.getInputStream()));
  
      String inputLine, outputLine;
      KnockKnockProtocol kkp = new KnockKnockProtocol();
      outputLine = kkp.processInput(null);
      out.println(outputLine);
  
      while ((inputLine = in.readLine()) != null) {
      outputLine = kkp.processInput(inputLine);
      out.println(outputLine);
      if (outputLine.equals("Bye"))
          break;
      }
      out.close();
      in.close();
      socket.close();
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  }
  }
  </code></pre>
  
  <p>And here in the server we have</p>
  
  <pre><code>package knockKnockServer;
  
  import java.net.*;
  import java.io.*;
  
  public class MultiKKServer {
  public static void main(String[] args) throws IOException {
      ServerSocket serverSocket = null;
      boolean listening = true;
  
      try {
          serverSocket = new ServerSocket(4444);
      } catch (IOException e) {
          System.err.println("Could not listen on port: 4444.");
          System.exit(-1);
      }
  
      while (listening)
      new KKMultiServerThread(serverSocket.accept()).start();
  
      serverSocket.close();
  }
  }
  </code></pre>
  
  <p>Now looking at the server it will create a new KKMultiServerThread on each connection, but how can i store them? can i make a array of KKMultiServerThread?
  I tried to make an array of KKMultiServerThread
  and when i try this line</p>
  
  <pre><code>multi[0] = new KKMultiServerThread(serverSocket.accept()).start();
  </code></pre>
  
  <p>I get this error "cannot convert void to Thread"
  If anyone can shine some light on my problem it would be great.</p>
  
  <p>Canvas</p>
  
  <p>Update</p>
  
  <p>I now have my own thread class </p>
  
  <pre><code>package Pong;
  
  import java.net.*;
  import java.io.*;
  
  public class PongPlayerThread extends Thread 
  {
  private Socket socket = null;
  private String pongData = "";
  
  public PongPlayerThread(Socket socket, int id) 
  {
      super("PongPlayerThread");
      this.socket = socket;
  }
  
  public void passData(String data)
  {
      pongData = data;
  }
  
  public void run()
  {
      try
      {
          PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
          while(true)
          {
              out.println(pongData);
          }
      }
      catch (IOException e) 
      {
          e.printStackTrace();
      }
  }
  
  }
  </code></pre>
  
  <p>the pongData is a string that holds all the information together in a string, now if i declare a player1 at the top of my pong server like so</p>
  
  <pre><code>private static PongPlayerThread player1;
  </code></pre>
  
  <p>and do this line when it is listening</p>
  
  <pre><code>    while(listen)
      {
          PongPlayerThread player1 = new PongPlayerThread(serverSocket.accept(), 0).start();
      }
  </code></pre>
  
  <p>it gives me this error "cannot convert from void to PongPlayerThread" how do i fix this?</p>