Java and C + + socket Communication (Java as a server-side server,c++ client, solving Chinese garbled problems GBK and UTF8)

Source: Internet
Author: User

Original link: http://www.cnblogs.com/kenkofox/archive/2010/04/25/1719649.html

Code:

Http://files.cnblogs.com/kenkofox/Client-CPlusPlus.rar
Http://files.cnblogs.com/kenkofox/Server_Java.rar

Java and C + + use socket communication, in fact, the underlying sockets are interlinked, so only need to follow their own syntax to do just that.

Java server-side use ServerSocket the Accept Create socket , with ordinary Java consistent between the communication.

C + + Client uses Makeconnect (server, Port, "TCP") , send , recv and other functions.

In this program, the first thing I encountered was that although the connection was successful, Java was unable to receive messages from C + +.

May be the wrong function and so on, the back to the bottom of the code to receive it is OK.

1//accepts data, but does not allow Chinese, because it is garbled2 DataInputStream in =NewDataInputStream (Clientsocket.getinputstream ());3byte[] buffer =New byte[10000];//size of the buffer4 in.read (buffer);//processing received messages, converting them into strings5/**6 * C + + passed the Chinese characters, need to convert. C + + uses GBK by default. 7 * GB2312 is a subset of GBK, only Simplified Chinese. Because the database is GB2312, so it goes straight to GB23128 **/9 message =NewString (buffer, "GB2312"). Trim ();

Other than that

The biggest problem is the character encoding problem, if found that Java received the string is garbled, it is necessary to take a closer look at the following instructions.

Java code at run time, by default with UTF8 to handle the string, the socket send string (if you use a high-level output stream directly output string, and finally the UTF8 way to split the string into a byte array and then transfer. (Can see http://www.cnblogs.com/kenkofox/archive/2010/04/23/1719009.html)

C + + uses GBK to transmit the socket by default when running XP.

So when Java receives C + + messages, it should switch to GBK or GB2312 to display the correct Chinese.

C + + to receive the correct Java message, it will be sent in Java when the GBK or GB2312 encoding (because C + + transcoding more trouble than Java, haha)

1    byte[] Responsebuffer = Newclientrequesthandler (message). Response (). GetBytes ("GB2312"); 2    out.write (Responsebuffer, 0,responsebuffer.length);

The C + + receive aspect, only needs to use the BUF to install, then turns into the string just. Correct display ... The code is probably:

CharCount = recv (socket, buf, Len, 0);

String resultstring (BUF);

In addition, in order to better understand the above coding problem, everyone in the Java side to send information to the C + + side, try the bottom of the way to try, very interesting. remember to be in C + + attention charcount over there . .

// get output output stream out = newprintstream (Clientsocket.getoutputstream ()); Out.print (test); // Direct UTF8 output, the final bottom of each Chinese with 3 bytes of transmission out.print (newstring (Test.getbytes (), "GBK"); // The GBK failed, and the actual text used 4 to 5 bytes passed out.print (newstring (Test.getbytes ("GBK"), "GBK"); // turn GBK, but the bottom is still to be broken into byte array, of course, eventually, like UTF8

Java and C + + socket Communication (Java as a server-side server,c++ client, solving Chinese garbled problems GBK and UTF8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.