Rohit Singh February 2016

how to tell server has sent data successfully in socket programming?

i am working on a problem on socket programming in JAVA. There is a server and a client.

1) server is connected to client
2) server sends N no of Strings which are stored in an array on server side(obviously ;)).
3)Client doesn't know the size of array
4)Server receives Strings from server one by one.
5)When Client reads all the Strings it sends one msg to server
6)Server receives the msg.
7)This process goes on(step 2-step 6) for multiple times.

The problem i am facing is, Client does not know when server sends the last String and it is waiting for its turn.
I have solved this problem using:
a)Multi threading.
b)Telling size of array to client at the beginning of the first msg

I want to know is there any in-built function which indicates if the server has stoped sending data?

here is the code for 1 iteration(step 1-step 6)

Server code:

public class server {
static String[] a;
static DataOutputStream dos;
static DataInputStream dis;
static ServerSocket server;
static Socket socket;

public static void main(String[] args)
{

    a=new String[]{"String1","String2","String3"};


     try {

        server=new ServerSocket(8080);

        socket=server.accept();
        dos=new DataOutputStream(socket.getOutputStream());
        dis=new DataInputStream(socket.getInputStream());

        ///sending array values

        //String temp=null;

        for(int i=0;i<a.length;i++)
        {
            dos.writeUTF(a[i]);
        }

        String msg_from_client=dis.readUTF();
        System.out.println(msg_from_client);



    } catch (IOException e) {

        e.printStackTrace();
    }

}


}

Client code:

public class client {


static String[] a;
static DataOutputStream dos;
static DataInputStream dis;
static Socket socket;
static Scanner sc;

public static void main(String[] args)
{
    try {
              

Answers


Em Ae February 2016

This is the time to learn more about protocols. You can setup your own protocol between your server and client i.e., the first message from the server would always contain the # of strings to follow. The client would keep a note of it and then it will request for # of strings that server told in first method.

EDIT: Little more enhanced protocol

If you chose the path to open a new connection for each message as suggested by other user, then you would have to add a little more to your protocol. You would need

  1. Client Information, so that server knows what communication it has done with this client previously
  2. Message information, so that server knows if this client is asking for new message or it sent some message earlier to this client and he is asking for next part of this message.

1 can be achieved by allotting a client ID. If you know how many clients you are dealing with, you can have it a hardcoded value. Otherwise generate at runtime

2 Message information could be "null" indicating that the client is asking for "any new message" for him. Keep in mind that having a "null" message_id doesn't mean that you skip this field. You have to make sure you add "message_id" "key" in the request but keep that field empty. The reply to this request would be expected # of strings that server would be returning plus a newly generated message_id. The client will use this message_id in all subsequent calls and will tell the server, I am asking for string x of y from message_id z


guillaume girod-vitouchkina February 2016

You need absolutely to exchange one information between server and client to indicate end of transmission:

  • by sending number of message before: as you suggest;

  • by sending special message at the end "END" for example.

Another solution: instead of looping 6/7, close the connection when data is read, and connect again.

Post Status

Asked in February 2016
Viewed 2,319 times
Voted 14
Answered 2 times

Search




Leave an answer