slayeroffrog
slayeroffrog

Reputation: 157

improving speed of comunication in java sockets

I am trying to improve the speed at which the sockets transfer information but i am unsure how to do so. the pourpose of the code is to transfer a number, the date, and a short xml which is being sent in the form of a string.

this is the server code

import java.net.*;
import java.io.*;

public class SSocket extends Thread
{
   private ServerSocket serverSocket;
   public SSocket(int port) throws IOException

   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(100000);
   }

   public void run()
   {
      System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
      while(true)
      {
         try
         {

            Socket server = serverSocket.accept();

            DataInputStream in = new DataInputStream(server.getInputStream());
            int cor=in.readInt();
            int i=0;
            String transaccion = in.readUTF();
            String fecha = in.readUTF();

            System.out.println(cor);
            System.out.println(transaccion);          
            System.out.println(fecha);

            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            if(transaccion!=null && fecha != null && cor>0){
                    out.writeInt(cor);
            }
            else {
                out.writeInt(-1);
            }
            if (i==100){
                out.flush();
                i=0;
            }
            i++;
            server.close();


         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      int port = 1337;
      try
      {
         Thread t = new SSocket(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

the code for the client is

import java.net.*;
import java.io.*;

public class ClientSocket
{
   public static void send(int correl, String transaccion, String fecha)
   {
      String serverName = "localhost";
      int port = 1337;

      try
      {

         Socket client = new Socket(serverName, port);
         int i=0;
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out =
                       new DataOutputStream(outToServer);



         out.writeInt(correl);
         out.writeUTF(transaccion);
         out.writeUTF(fecha);

         InputStream inFromServer = client.getInputStream();
         DataInputStream in =
                        new DataInputStream(inFromServer);

         int corin=in.readInt();

         if(corin>0){
             Envio.updater(corin);
         }

         else {

         }
         if (i==100){
             out.flush();
             i=0;
         }
         i++;
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

i have done some reading on the mater and it seems that posible solutions are to use either a buffer or swich to a datagram. however my experience on working with sockets is rather limited and i am unsure which would be best to use for this situation or if there is another option i havent yet considered. this code will be moving many transactions and i wish to do it in as short time as posible. thanks in advance

ps. sorry for my bad english it is not my first language

Upvotes: 0

Views: 91

Answers (1)

Tim
Tim

Reputation: 2027

Datagrams imply UDP, which is an unreliable delivery protocol so you're not guaranteed to get all content. That's probably not what you want; I'd stay with plain Sockets (which use TCP, which has reliable delivery).

Will the same client be calling send() repeatedly and connecting to the same server each time? That is, will there be many messages going across a single connection, or will each message be to a different server, with only a single message (or only a few) going to each of the many servers? If there's just one server that a client is going to connect to and if a given client is going to send lots of messages, you should keep the Socket open between send() calls; setting up and tearing down Sockets is expensive, so you're paying a high price for making a new connection each time.

Also, your server appears to only be able to handle a single connection at a time: you accept a connection, read from it, and then close it and accept a new one. So to make this work for more than one client, you'll need to separate the logic for accepting connections onto a different thread from the logic that reads data. If you'll only have a few clients at a time, you can just start a new thread to read from each socket as you create it for a new client; if you'll have lots of clients (thousands), you'll probably need to look at NIO for its ability to service multiple sockets from a single thread. But I suspect you're a long way from having that problem, if you ever do, so I'd just spawn a new thread for each socket.

Upvotes: 1

Related Questions