Rohit Singh
Rohit Singh

Reputation: 18222

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 {
        socket=new Socket("127.0.0.1",8080);
        System.out.println("connected");
        dos=new DataOutputStream(socket.getOutputStream());
        dis=new DataInputStream(socket.getInputStream());
        sc=new Scanner(System.in);

        //reading from server i dont know what is the size of array at server side
        String temp=null;
        while((temp=dis.readUTF())!=null)
        {
            System.out.println(temp);
        }
        System.out.println("out of the loop");

        ////now client sends the msg;

        String msg=sc.nextLine();
        dos.writeUTF(msg);

        System.out.println("sent");




    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
}

output at client side:

connected
String1
String2
String3

Upvotes: 0

Views: 837

Answers (2)

Em Ae
Em Ae

Reputation: 8724

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

Upvotes: 1

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.

Upvotes: 0

Related Questions