Monday 15 July 2013

Socket issue c# client to java server -



Socket issue c# client to java server -

this problem, messages sent c# client aren't received server until shutdown socket on client side , server receive info in 1 time time.

c# client side

public static class asynchronousclient { // port number remote device. private const int port = 8888; // manualresetevent instances signal completion. private static manualresetevent connectdone = new manualresetevent(false); private static manualresetevent senddone = new manualresetevent(false); private static manualresetevent receivedone = new manualresetevent(false); public static socket client; // response remote device. private static string response = string.empty; public static void startclient() { // connect remote device. seek { // found remote endpoint socket. // name of // remote device "host.contoso.com". iphostentry iphostinfo = dns.gethostentry("127.0.0.1"); ipaddress ipaddress = iphostinfo.addresslist[0]; ipendpoint remoteep = new ipendpoint(ipaddress, 8888); // create tcp/ip socket. client = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); // connect remote endpoint. client.beginconnect(remoteep, new asynccallback(connectcallback), client); connectdone.waitone(); // send test info remote device. send(client, "test connection"); sentdown.waitone(); } grab (exception e) { console.writeline(e.tostring()); } } public static void connectcallback(iasyncresult ar) { seek { // retrieve socket state object. socket client = (socket)ar.asyncstate; // finish connection. client.endconnect(ar); console.writeline("socket connected {0}", client.remoteendpoint.tostring()); // signal connection has been made. connectdone.set(); } grab (exception e) { console.writeline(e.tostring()); } } public static void send(socket client, string data) { // convert string info byte info using ascii encoding. byte[] bytedata = encoding.ascii.getbytes(data); // begin sending info remote device. client.beginsend(bytedata, 0, bytedata.length, socketflags.none, new asynccallback(sendcallback), null); } public static void sendcallback(iasyncresult ar) { seek { // retrieve socket state object. // finish sending info remote device. int bytessent = client.endsend(ar); console.writeline("sent {0} bytes server.", bytessent); // signal bytes have been sent. senddone.set(); } grab (exception e) { console.writeline(e.tostring()); } }

java server side

public class connection_to_port extends thread { final static int port = 8888; private socket socket; string message = ""; public static void main(string[] args) { seek { serversocket socketserveur = new serversocket(port); while (true) { socket socketclient = socketserveur.accept(); connection_to_port t = new connection_to_port(socketclient); t.start(); system.out.println("connected client : " + socketclient.getinetaddress()); } } grab (exception e) { e.printstacktrace(); } } public connection_to_port(socket socket) { this.socket = socket; } public void run() { handlemessage(); } public void handlemessage() { seek { bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); message = in.readline(); system.out.println(message); } grab (exception e) { e.printstacktrace(); } }

in client have send info server

asynchronousclient.send(asynchronousclient.client, "{myjsondata}");

my client sending, not receive.

the problem is, java server receive nil ! client said it's sent, , see on wireshark that's it's send.

when do asynchronousclient.client.shutdown(socketshutdown.both);

finally see message on server @ same time. if sent 1 message.

the java side trying read line (you using readline).

this method not homecoming until either there end-of-line character, or stream closed.

when shutdown client, in effect, stream closes.

your test message not include end-of-line character, way readline homecoming @ end of stream.

java c# sockets

No comments:

Post a Comment