Character garbled problem in CSocket programming

Source: Internet
Author: User

Character garbled problem in CSocket programming

First put oneself in the programming debugging encountered this character garbled problem simple elaboration:

    • Platform: Windows8.1 + VS2010;
    • Use MFC's own CSocket;
    • The server and client belong to a TCP connection,

The server sends a message to the client in the form of a struct, defined as follows:

typedef struct message{    int seq;    char name[10];    char time[24];}Msg;

where SEQ is directly assigned, the fill of the name variable is copied from the character pointer of CString using the strcpy () function, which is generally 4~6 in length and is certainly not more than 10. However, the time variable is also assigned using the strcpy () function, which has a string length of just 24, which means that the char time array has no end flag bit. I did not think too much, in line with the principle of saving, the data packets sent out, the code is as follows

//Server端Msg msg;msg.seq = 10;strcpy(, pname);strcpy(msg,time, ptime);if(!socket_server.Send((char*)&msg, sizeof(Msg), 0)){    ...}  //Client端Msg msg;if(!socket_client.Receive((char*)&msg, sizeof(Msg), 0)){    ...}CString strmsg(msg.time);AfxMessageBox(strmsg);      //这里显示的信息除了正确的一部分,后面总是有乱码

As a result, the time variables received by the client are always garbled, in addition to being able to recover the original data. The error occurs as follows:

In order to debug this problem for a long time, in fact, the reason is very simple, is sent by the character array is not with the end of the flag bit , now the results of the test summarized, in order to simplify the problem, the communication between the two sides of the test is sent to receive character array type, struct type is actually similar.

1. Sender character array is larger than string length

This is the most common situation, certainly will not appear garbled, its code is as follows:

char buf[20] = "I send a message";socket_server.Send(buf, 20, 0);

String length is 16, smaller than the length of the array, if the receiver is also 20 long buf to receive, it will not produce garbled.

2. The sender just sends a character array of the length of the string

This is to verify that garbled characters are not in the end because the character array does not have a stop flag bit, its generation is as follows:

char buf[] = "I send you a message";socket_server.Send(buf, 20, 0);

In this case, the string length is exactly 20, and the length of the transmission is also 20, that is, just send all the word character out, when the receiver receives the data, with greater than or equal to 20 of the BUF, this time will appear garbled.

3. The receiver's buf just equals the string length

This situation is to see that when the sender correctly sends the data, the receiver does not correctly set the size of the BUF to the result of not being able to receive the end of the string, and its code is as follows:

//Server端char buf[25] = "I send you a message";socket_server.Send(buf, 25, 0);//Client端char buf[20];socket_client.Receive(buf, 20, 0);

Because the length of the string is 20, so the receiver is not receiving the end of the flag bit, this situation will also produce garbled.

4. Summary

In short, for a char array type, the sender must send its end flag bit, and the receiver must also receive its end flag bit. If one of the above two situations is not satisfied, it is sure to produce garbled. Write these down and hope you don't make such a low-level mistake next time (--!)

From the perspective of the phenomenon, the receiver from the end of the data to the assignment to your defined buf, it is certain that the pointer points to a memory area. But why is this so?? In the end of the computer in the course of the process which led to the occurrence of the above phenomenon?? It needs to be studied more carefully.

Character garbled problem in CSocket programming

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: 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.