Java and C ++ socket communication (Java serves as the server, and C ++ serves as the Client client to solve Chinese garbled problem GBK and utf8)

Source: Internet
Author: User

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 socket is the same, so they only need to follow their respective syntax.

 

JavaServer uses serversocketAcceptCreate socket, Similar to common JavaThe communication is consistent.

C ++The client uses makeconnect (server, port, "TCP "), Send, RecvAnd other functions.

 

In this programming, I first encountered that although the connection was successful, Java could not receive messages sent from C ++.

It may be because of the use of wrong functions and the like. It will be okay if the code below is changed to receive it.

  1   1   //  Accept data, but do not allow Chinese characters, because garbled characters  
2   2 Datainputstream in = New Datainputstream (clientsocket. getinputstream ());
3   3 Byte [] Buffer = New Byte [ 10000 ]; // Buffer size
4   4 In. Read (buffer ); // Process received packets and convert them into strings.
5   5 /**
6 6 * C ++ needs to convert the text passed in. C ++ uses GBK by default.
7 7 * gb2312 is a subset of GBK and only has simplified Chinese characters. Because the database uses gb2312, it is directly converted to gb2312
8 8 * */
9   9 Message = New String (buffer, " Gb2312 " ). Trim ();

 

 

In addition

The biggest problem is character encoding. If you find that the strings received by Java are garbled, you should take a closer look at the subsequent instructions.

 

When Java code is running, UTF-8 is used by default to process strings, and the socket sends strings (if the string is directly output using a high-level output stream, finally, the string is automatically split into byte arrays by UTF-8 and then transmitted. (See http://www.cnblogs.com/kenkofox/archive/2010/04/23/1719009.html)

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

 

Therefore, when Java receives a C ++ message, it should be converted to GBK or gb2312 to display the correct Chinese characters.

C ++ needs to convert to GBK or gb2312 encoding when sending the correct Java Message (because C ++ transcoding is much more troublesome than Java, haha)

 

  1 Byte[] Responsebuffer=Newclientrequesthandler (Message). Response (). getbytes ("Gb2312");
2 Out. Write (responsebuffer,0, Responsebuffer. Length );

 

 

For C ++ receiving, you only need to use Buf to install it and convert it to string. Correctly display ...... The code is probably:

Charcount = Recv (socket, Buf, Len, 0 );

String resultstring (BUF );

 

In addition, to better understand the above encoding problems, it is interesting to try the following method when sending messages to the C ++ end in Java.RememberC ++Follow charcount.

  1   //  Get output stream  
2 Out = Newprintstream (clientsocket. getoutputstream ());
3   Out. Print (test ); // Utf8 is output directly, and each bottom layer of Chinese is transmitted in 3 bytes.
4   Out. Print (newstring (test. getbytes (), " GBK " )); // An error occurred while converting to GBK. Actually, four to five bytes are used for each text.
5   Out. Print (newstring (test. getbytes ( " GBK " ), " GBK " )); // Convert to GBK, but the underlying layer still needs to be split into byte arrays. Of course, it is still the same as utf8.

 

 

 

The following describes the complete code.

Java:

Echoserverthread is a server class dedicated to waiting for the customer's connection and then establishing echothread for processing.

Echothread is a thread that processes messages. It mainly includes socket operations for receiving messages and sending messages.

Clientrequesthandler is the actual business logic class that processes strings ......

 

C ++:

Client. cpp is the main function for testing.

Socketmanager. h contains the socketmanager class, which encapsulates operations such as socket startup and sending.

Connection. h contains the connection class, which encapsulates the underlying calls of the socket.

Conn_exception.h defines an exception.

 

Related Article

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.