Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,500 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,389 people online right now. Registration is fast and FREE... Join Now!




Socket: C++ Server reads text wrongly from Java client

 
Reply to this topicStart new topic

Socket: C++ Server reads text wrongly from Java client

Voicon
22 Mar, 2007 - 10:08 PM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 2


My Contributions
I have a client in Java and a server in C++ (using wsock32.lib). The client is very simple, just sends a text to server. I have the server loop to receive a message from client, like this:

CODE
while(1){
                int nret = 0;    
            nret = recv(m_socket,buffer,100,0);
            if(nret == SOCKET_ERROR) {
               nret = WSAGetLastError();        
               //throw error
            }
            buffer[nret-1] = '\0';
        printf("Recv:%s\n",buffer);
    }



When the client sends a message, ended by "\n", e.g. "hello\n", I debug, and the process is hereafter:

- server calls recv for the first time. buffer gets "h????????????", nret = 1. so the buffer is got "" after truncating.
- it continues to call recv, buffer gets "ello????????", nret = 5. the buffer is "ello" then.

I don't know why the recv called 2 times. Please help me! Thank you in advance.

Best regards,
Voicon
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Socket: C++ Server Reads Text Wrongly From Java Client
23 Mar, 2007 - 05:46 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
I think that Java uses Unicode character strings, and C++ uses ASCII character strings. Somehow it will be important to verify or type cast the characters to be of the same type.
User is offlineProfile CardPM
+Quote Post

Voicon
RE: Socket: C++ Server Reads Text Wrongly From Java Client
23 Mar, 2007 - 06:16 PM
Post #3

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 2


My Contributions
QUOTE(ajwsurfer @ 23 Mar, 2007 - 06:46 AM) *

I think that Java uses Unicode character strings, and C++ uses ASCII character strings. Somehow it will be important to verify or type cast the characters to be of the same type.


Thanks for your reply. I will check it out. But I think it's not the problem. Because if Java send Unicode byte (e.g 10bytes), in C++ it will receives 5 bytes only, is it right? Here, it separates 2 times to receive: first for 1 byte and second (last), for the rest.

Have a good day!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month