error while sending a String from C client to Java server -
i send integer c client java server , worked perfectly. when tried same thing string got , error client code send string
char clientstring[30]; printf("string send : \n"); if( send( to_server_socket, &clientstring, sizeof( clientstring ), 0 ) != sizeof( clientstring ) ) { printf( "socket write failed"); exit( -1 ); }
and java code read it
datainputstream din = new datainputstream(socket.getinputstream()); string clientstring=din.readutf(); system.out.println(clientstring);
error
java.io.eofexception @ java.io.datainputstream.readfully(datainputstream.java:180) @ java.io.datainputstream.readutf(datainputstream.java:592) @ java.io.datainputstream.readutf(datainputstream.java:547) @ servicerequest.run(servicerequest.java:43) @ java.util.concurrent.executors$runnableadapter.call(executors.java:439) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:303) @ java.util.concurrent.futuretask.run(futuretask.java:138) @ java.util.concurrent.threadpoolexecutor$worker.runtask(threadpoolexecutor.java:895) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:918) @ java.lang.thread.run(thread.java:680)
edit :i tried using din.readline()
,i don't have error anymore if type fffffff12 on client got fffffff12`?7e^Ê?h in server
readutf
doesn't read bytes socket. starts reading length of string (as 16-bit integer) , reading string. problem send not required readutf
work successfully.
as joachim pileborg noted, sending entire 30 bytes of clientstring
(including remaining bytes not explicitly set). should send instead:
send(to_server_socket, clientstring, strlen(clientstring), 0);
Comments
Post a Comment