java - how to tell server has sent data successfully in socket programming? -
i working on problem on socket programming in java. there server , client.
1) server connected client
2) server sends n no of strings stored in array on server side(obviously ;)).
3)client doesn't know size of array
4)server receives strings server 1 one.
5)when client reads strings sends 1 msg server
6)server receives msg.
7)this process goes on(step 2-step 6) multiple times.
the problem facing is, client not know when server sends last string , waiting turn.
have solved problem using:
a)multi threading.
b)telling size of array client @ beginning of first msg
i want know there in-built function indicates if server has stoped sending data?
here code 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 server dont know size of array @ server side string temp=null; while((temp=dis.readutf())!=null) { system.out.println(temp); } system.out.println("out of loop"); ////now client sends msg; string msg=sc.nextline(); dos.writeutf(msg); system.out.println("sent"); } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
output @ client side:
connected
string1
string2
string3
this time learn more protocols. can setup own protocol between server , client i.e., first message server contain # of strings follow. client keep note of , request # of strings server told in first method.
edit: little more enhanced protocol
if chose path open new connection each message suggested other user, have add little more protocol. need
- client information, server knows communication has done client previously
- message information, server knows if client asking new message or sent message earlier client , asking next part of message.
1 can achieved allotting client id. if know how many clients dealing with, can have hardcoded value. otherwise generate @ runtime
2 message information "null" indicating client asking "any new message" him. keep in mind having "null" message_id doesn't mean skip field. have make sure add "message_id" "key" in request keep field empty. reply request expected # of strings server returning plus newly generated message_id. client use message_id in subsequent calls , tell server, asking string x of y message_id z
Comments
Post a Comment