I'm running into a problem with sending packets between a java client and a c++ server over a tcp/ip socket.
the data being sent between the client and server is a plain text string.
the problem I'm having is the java client can send packets to the server and the server can process the strings send without a problem. However, when I send a string from the server to the client the java client either interprets it as being a blank string or as random characters.
here's the relevant c server code
Here's how I setup the buffer
Here's an example of the first thing I try to write from the server to the client and how I put it into the buffer.Code:char* messageBuf = (char*)malloc(sizeof(char)*1025);
Here's the function call I use to send the packet through the socketCode:messageBuf[0]='?'; messageBuf[1]='\0';
here's the relevant java client codeCode:if(send(cfd->connection,messageBuf,strlen(messageBuf),0)<0){ perror("Server: write error"); cfd->~clientConnection(); close(cfd->connection); delete cfd; return(0); }
I setup my socket in java with
setup my input output streams in java withCode:s = new Socket("127.0.0.1", serverPort);
read from the stream in java usingCode:DataOutputStream out = new DataOutputStream(s.getOutputStream()); DataInputStream in = new DataInputStream(s.getInputStream());
any help would be very much appreciated.Code:char bytes[] = new char[1025]; String data; in.read(bytes); data = bytes.toString();


Reply With Quote
Bookmarks