470,810 Members | 1,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

socket between java client and c++ server

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

Dec 14 '06 #1
2 6229


On Dec 14, 10:57 pm, "happyvalley" <nittanymount...@gmail.comwrote:
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() )

Dec 14 '06 #2

andrewmcdonagh wrote:
On Dec 14, 10:57 pm, "happyvalley" <nittanymount...@gmail.comwrote:
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()

Dec 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Al Wilkerson | last post: by
2 posts views Thread by zhebincong | last post: by
3 posts views Thread by spasavvas | last post: by
3 posts views Thread by madsornomads | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.