Send struct in socket programming in Linux

Source: Internet
Author: User

The send function in the socket can send strings, but cannot directly send struct. Therefore, the sender converts the struct into a string, then sends the string to the receiver using send, Recv the string at the receiving end, and then converts it to the original struct, this is the main idea for solving the problem. The problems to be paid attention to during implementation are described below.

In order to allow clients to communicate with each other and implement private chat, I use the server forwarding method. Therefore, each message sent by a user contains only the message body, it must also contain information such as the sender and receiver ID. Using struct is the best way. The struct I have defined is as follows:

Struct send_info

{

Char info_from [20]; // sender ID

Char info_to [20]; // receiver ID

Int info_length; // The length of the message body sent.

Char info_content [1024]; // Message Body

};

The main code of the sender (for the sake of simplicity, I removed the Verification Code such as the content and length entered by the user ):

Struct send_info info1; // defines the struct variable

Printf ("this is client, please input message :");

// Read user input data from the keyboard and write it into info1.info _ content

Memset (info1.info _ content, 0, sizeof (info1.info _ content); // clear the cache

Info1.info _ length = read (stdin_fileno, info1.info _ content, 1024)-1; // read user input data

  

Memset (snd_buf,); // clears the sending cache. If it is not cleared, garbled characters may occur during receiving,

// Or if the content sent this time is less than the previous time, the snd_buf contains the previous content.

  

Memcpy (snd_buf, & info1, sizeof (info1); // convert the struct to a string

Send (connect_fd, snd_buf, sizeof (snd_buf), 0); // send information

Main Code of the acceptor:

Struct send_info CLT; // defines the struct variable

  

Memset (recv_buf, 'z', 1024); // clear the cache

Recv (FD, recv_buf, 0); // read data

  

Memset (& CLT, 0, sizeof (CLT); // clear the struct

Memcpy (& CLT, recv_buf, sizeof (CLT); // converts the received information into a struct.

  

Clt.info _ content [clt.info _ length] = '';

// The message content ends. If this sentence is not provided, message garbled characters or output exceptions may occur.

// Some netizens suggested that fields of the string type should not be included in the transmitted struct. It is estimated that the string tail character is located.

  

If (clt.info _ content) // determine the received content and output it

Printf ("nclt.info _ from is % snclt.info _ to is % snclt.info _ content is % snclt.info _ length is % DN", clt.info _ from, clt.info _ to, clt.info _ content, clt.info _ length );

// At this point, the sending and receiving of the struct have been successfully completed.

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.