socket between java client and c++ server

H

happyvalley

Hi,
don't know if it is the place to ask this question

I have a c++ server and a java client communicate via socket

in Java client
// create a client socket
Socket clientSocket = new Socket("localhost", 27015);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);

String userInput = "amsg";
System.out.println("send to server:" + userInput.length() + " :" +
userInput);
out.println(userInput);

in c++ server
// code to starup the connection
int recvbuflen = 255;
char recvbuf[255];
int iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
cout<<"iResult = "<<iResult<<endl;


the msg sendout in java client "amsg" is 4, while, in the c++ server,
it says the msg length = 6.
it always +2.
when I send the msg from a java client to a java server via socket,
there is no such problem

any suggestion. thanks
 
A

andrewmcdonagh

Hi,
don't know if it is the place to ask this question

I have a c++ server and a java client communicate via socket

in Java client
// create a client socket
Socket clientSocket = new Socket("localhost", 27015);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);

String userInput = "amsg";
System.out.println("send to server:" + userInput.length() + " :" +
userInput);
out.println(userInput);

in c++ server
// code to starup the connection
int recvbuflen = 255;
char recvbuf[255];
int iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
cout<<"iResult = "<<iResult<<endl;

the msg sendout in java client "amsg" is 4, while, in the c++ server,
it says the msg length = 6.
it always +2.
when I send the msg from a java client to a java server via socket,
there is no such problem

any suggestion. thanks

Its sending the \eol \cr chars as well... (or in Java's speak '\n'
- because you used out.println(...) instead of out.print() )
 
H

happyvalley

andrewmcdonagh said:
Hi,
don't know if it is the place to ask this question

I have a c++ server and a java client communicate via socket

in Java client
// create a client socket
Socket clientSocket = new Socket("localhost", 27015);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);

String userInput = "amsg";
System.out.println("send to server:" + userInput.length() + " :" +
userInput);
out.println(userInput);

in c++ server
// code to starup the connection
int recvbuflen = 255;
char recvbuf[255];
int iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
cout<<"iResult = "<<iResult<<endl;

the msg sendout in java client "amsg" is 4, while, in the c++ server,
it says the msg length = 6.
it always +2.
when I send the msg from a java client to a java server via socket,
there is no such problem

any suggestion. thanks

Its sending the \eol \cr chars as well... (or in Java's speak '\n'
- because you used out.println(...) instead of out.print() )

thanks a lot, seems print() doesnot work, should be write()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top