C ++ and Java send and receive functions during socket communication

Source: Internet
Author: User

The following is a question about how to correctly use the Java receiving function during the communication between Java and C ++ socket, mainly to complete the project, which may not be detailed enough. please correct me more:

1. Sending on the C ++ side: if the message is sent in multiple byte types such as integer type, it must be converted by size before it can be sent, when receiving this type, the Java end uses functions such as readint and readfloat; otherwise, garbled characters may occur on the Java end;

Code Conversion on the Size end is as follows:

#define BSWAP_32(x) \(UINT32) ( (( ((UINT32)(x)) & 0xff000000 ) >> 24) | \(( ((UINT32)(x)) & 0x00ff0000 ) >> 8 ) | \(( ((UINT32)(x)) & 0x0000ff00 ) << 8 ) | \(( ((UINT32)(x)) & 0x000000ff ) << 24) \)#define  BSWAP_16(x) \(UINT16) ( ((((UINT16)(x)) & 0x00ff) << 8 ) | \((((UINT16)(x)) & 0xff00) >> 8 ) \ )

// C ++ end int TMP = 20; int length = bswap_32 (TMP); int ret = Send (sock, (char *) & length, sizeof (INT ), 0 );.................. // Java end ...................int TMP = in. readint ();...........

2. When the C ++ side sends a string, the Java side receives the string using a byte array. The function used is int read (byte [] B, int off, int Len ), if this function is used, the length of the string should be sent before receiving;

// C ++ string STR = "Hello World"; ret = Send (sock, str. c_str (), str. length (), 0 );........ // JAVA byte [] TMP = new byte [size]; In. read (TMP, 0, TMP. length); string STR = new string (TMP );............

3. When C ++ sends character arrays, Java can use int read (byte [] B) to receive them.

// C ++ side char * buffer = ......; int ret = Send (sock, buffer, buffersize, 0 );............. // Java buffer = new byte [buffersize]; int size = in. read (buffer );..................

4. Use void write (byte [] B) to send strings in Java. I personally think the C ++ end does not know how long the sent strings are, therefore, first send a string length to the C ++ end, and then send the string;

// Java sender out. writebyte (byte) filenamelength );.............. out. write (filename. getbytes ());...............

5. Send arrays on the Java end. I personally think that no matter what type of array, it should be converted into a byte array before sending it out.

out.write(buffer, 0, bufferSize);

The above is my personal summary in the project process. If you have any shortcomings, please feel free to contact us.

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.