title: best ways for optimizing socket programming in java?
body: <p>i try to program a part of a university research project about multi client - server socket programming.my code works as well as so that i give valide result but the problem is that evalutor of our group said that my code have not a good speed on connection for data transfer.i will be thankfull if you found the problem(s) in my code that cause this issue.</p>
  
  <p>server part:</p>
  
  <pre><code>    import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import j
  
  ava.io.PrintStream;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.SocketAddress;
  import java.net.UnknownHostException;
  import java.util.ArrayList;
  
  /**
   * 
   */
  
  /**
   * @author Sina
   *
   */
  public class BoxServer {
  
      ServerSocket serversocket;
      static ThreadHandler t[]=new ThreadHandler[100];
      static int size=0;
      static ArrayList&lt;Message&gt; messagebox=new ArrayList&lt;Message&gt;();
      public static void main(String[] args) {
  
          ServerSocket serverSocket = null;
          try {
              serverSocket = new ServerSocket(79);
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          while(true)
          {
  
          try{
  
  
  
  
                  //InetAddress inetadress=InetAddress.getLocalHost();
                  //System.out.println(inetadress);
                  //System.out.println(inetadress.getHostName());
                  //System.out.println(inetadress.getHostAddress());
                  Socket socket=serverSocket.accept();
                  if(socket==null)
                  {
                      System.out.println("null");
                  }
                  t[size]=new ThreadHandler(socket,"username");
                  size++;
                  t[size-1].start();
  
  
  
          }
          catch(UnknownHostException e){
              System.out.println("salam s");
           System.out.println(e.getMessage());
          }
          catch (IOException e) {
              System.out.println("bye s");
              System.out.println(e.getMessage());
          }
  
  
          }
  
      }
  
  }
  
  class ThreadHandler extends Thread{
  
      private String socname;
      Socket mySocket;
      ObjectInputStream inp;
      ObjectOutputStream outp;
      public ThreadHandler(Socket s,String socketName)
      {
          this.mySocket=s;
          this.socname=socketName;
  
  
  
      }
      public void run()
      {
          try {
              inp=new ObjectInputStream(mySocket.getInputStream());
              outp=new ObjectOutputStream(mySocket.getOutputStream());
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          while(true)
          {
          System.out.println("thread run");
          System.out.println(mySocket.getLocalPort());
          System.out.println(mySocket.getLocalAddress());
  
          try {
          //  System.out.println("my socket:"+mySocket.getOutputStream());
              System.out.println(mySocket.isConnected());
              System.out.println(inp.available());
              System.out.println("inp = "+inp);
              System.out.println("reeead "+ inp.readObject());
              Message mess=(Message)inp.readObject();
              System.out.println("dsd");
              System.out.println("mess: "+mess);
              BoxServer.messagebox.add(mess);
              if(mess.getReceiver().equals("system-use:code=1"))
              {
                  System.out.println(mess.getSender()+" wants to see his/her Inbox");
              }
              //mySocket.close();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              System.out.println("bug dar thread");
              e.printStackTrace();
  
          }
  
           }
  
      }
  }
  </code></pre>
  
  <p>client part</p>
  
  <pre><code>    import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  import java.util.TimerTask;
  import java.util.concurrent.ScheduledExecutorService;
  
  import javax.swing.Timer;
  
  public class Main {
  
      /**
       * @param args
       */
  
  
      static Socket socket=new Socket();
      public static void main(String[] args) {
  
  
  
  
          System.out.println("newuser(n) or login(l)");
          Scanner scanner=new Scanner(System.in);
          String typeOfOperation=scanner.nextLine();
          if(typeOfOperation.equals("n"))
          {
  
  
          }
          else
          if(typeOfOperation.equals("l"))
          {
  
              try {
                  socket = new Socket("127.0.0.1",79);
                  final ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                  ObjectInputStream in=new ObjectInputStream(socket.getInputStream());
  
                  while(true)
                  {
  
                  Thread timer=new Thread()
                  {
                      public void run()
                      {
                          while(true)
                          {
                              Message temp=new Message();
                              temp.setReceiver("system-use:code=1");
                              temp.setSender("username");
  
                              try {
                                  out.writeObject(temp);
                                  sleep(5000);
                              } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
                              catch (InterruptedException e) {
                                  // TODO: handle exception
                                  e.printStackTrace();
                              }
  
                          }
                      }
                  };
                  timer.start();
                  String username=scanner.nextLine();
                  String to=scanner.nextLine();
                  String body=scanner.nextLine();
                  Message all=new Message();
                  all.setText(body);
                  all.setReceiver(to);
                  all.setSender(username);
  
  
                  System.out.println("you connected to system");
                  System.out.println(socket);
  
                  System.out.println("now should write");
                  out.writeObject(all);
                  System.out.println("ghable threAD");
  
                  }
              //  socket.close();
  
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  System.out.println("salaam c");
                  System.out.println(e.getMessage());
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  System.out.println("bye c");
                  System.out.println(e.getMessage());
              }
  
          }
          else
          {
              System.out.println("bad operation. try again!");
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>Message class(Entity only not important i think!):</p>
  
  <pre><code>import java.io.Serializable;
  
  
  public class Message implements Serializable{
      String sender;
      String receiver;
      String text;
      boolean delivered=false;
      public void delived()
      {
          this.delivered=true;
      }
      private String tostringOfClass;
  
      public void setReceiver(String receiver) {
          this.receiver = receiver;
      }
      public void setSender(String sender) {
          this.sender = sender;
      }
      public void setText(String text) {
          this.text = text;
      }
      public String getReceiver() {
          return receiver;
      }
      public String getSender() {
          return sender;
      }
      public String getText() {
          return text;
      }
  
      public String toString()
      {
          tostringOfClass="sender : "+sender+" \n"+"receiver : "+receiver+" \n"+"message: "+text;
  
          return tostringOfClass;
      }
  
  }
  </code></pre>