A UNIX socket Problem

Source: Internet
Author: User
A UNIX socket-Linux general technology-Linux programming and kernel information. For details, see the following. Ladies and gentlemen, I am new to linux programming. Now I want to write a program for inter-process communication. I want to use UNIX domain sockets and UDP. I have the following ideas, I also wrote the program, but the receiving part is always abnormal. For example, IF module A receives A message of char from client B = 5, it actually receives A char character. please help me check my program. Thank you.

Implementation idea: This program sends messages through the UDP protocol. The content and address of the messages are transmitted to the process by the parameters of the main function. Example of parameters: AB5hello
The length of each message is 8 bytes.
The message structure is as follows:
Typedef struct
{
Char self_module_id; // your own ID, which is used to mark each message so that the receiver can know who sent it.
Char dst_module_id; // The destination ID, which determines the destination of each message. The destination path is determined by this ID.
Int length; // message length
Char msg [5]; // message content
} Msg_struct;
Sending implementation process: 1. determine the number of complete messages by dividing the total message length (that is, the total length of the main function parameter) by 8.
2. Send a message by calling the sendto function once, and obtain the destination address based on the data structure to determine the sending path, that is, judge every 8 bytes, the destination address to which the message is sent. Until all the complete messages are sent.
3. The last message may be less than 8 bytes. By modulo the total length of the message, the length of the incomplete message is obtained and the message is sent.
Read implementation process: 1. obtain all the messages on the descriptor through recvfrom and store them in the buffer zone rch.
2. Start to process the messages stored in rch and repackage them into pre-defined message structures through the subcontracting mechanism.
3. Save the complete message before saving the incomplete message.

The program code is as follows:
/*-------------------------------------------------------------------------
This is a client program
-------------------------------------------------------------------------*/
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define SIZE 32
# Define MAXTEXT 256

Int main (int argc, char ** argv)
{
Int src_sockfd;
Int dst_sockfd;
Int self_sockfd;
Int n_read, n_write, len, rcd, I, o, k;
Struct sockaddr_un src_sockaddr, dst_sockaddr, self_sockaddr;
// Int result;
// Char argv [1] [MAXTEXT];
Char rch [MAXTEXT];
// Char dst_module_id;
Char src_module_id;
// Char self_module_id = 'a ';
Fd_setread_fd_set, write_fd_set;
Fd_setwatchset;
Int maxfd;

Typedef struct
{
Char self_module_id;
Char dst_module_id;
Int length;
Char msg [5];
} Msg_struct;
Msg_struct dst_msg;
Dst_msg.self_module_id = 'a ';
Msg_struct src_msg;
Char * ptr_rch, * ptr_sch;
Msg_struct * ptr_rmsg, * ptr_smsg;
Ptr_rmsg = & src_msg;
Ptr_rch = rch;
Ptr_smsg = & dst_msg;
Ptr_sch = argv [1];

Len = sizeof (src_sockaddr );

If (argc <2)
{
Printf ("Aerror1 happen \ n ");
Exit (1 );
}


Unlink ("/home/wewe/code/temp/");
Self_sockfd = socket (AF_LOCAL, SOCK_DGRAM, 0 );
If (src_sockfd <0)
{
Printf ("sokcet err, % s \ n", strerror (errno ));
Return-1;
}
Bzero (& self_sockaddr, sizeof (self_sockaddr ));
Self_sockaddr.sun_family = AF_LOCAL;
Strcpy (self_sockaddr.sun_path, "/home/wewe/code/temp/");
Bind (self_sockfd, (struct sockaddr *) & self_sockaddr, sizeof (self_sockaddr ));



While (1)
// While (1)
{
FD_ZERO (& write_fd_set); // clear the watchset struct.
FD_SET (self_sockfd, & write_fd_set); // Add sever_sockfd to watchset?
FD_ZERO (& read_fd_set );
FD_SET (self_sockfd, & read_fd_set );
Maxfd = self_sockfd;


If (rcd = select (maxfd + 1, & read_fd_set, & write_fd_set, NULL, (struct timeval *) 0)> 0)
{

If (FD_ISSET (self_sockfd, & read_fd_set ))
{
Printf ("nengduA \ n ");
Bzero (& src_sockaddr, len );
Src_sockaddr.sun_family = AF_LOCAL;
// Recvfrom (self_sockfd, & src_module_id, 1, 0, (struct sockaddr *) & src_sockaddr, & len );

N_read = recvfrom (self_sockfd, rch, 10, 0, (struct sockaddr *) & src_sockaddr, & len );
Rch [n_read] = 0;
For (I = 0; I {
Ptr_rmsg-> self_module_id = rch [8 * I + 0];
Ptr_rmsg-> dst_module_id = rch [8 * I + 1];
Ptr_rmsg-> length = rch [8 * I + 2];
Ptr_rmsg-> msg [0] = rch [8 * I + 3];
Ptr_rmsg-> msg [1] = rch [8 * I + 4];
Ptr_rmsg-> msg [2] = rch [8 * I + 5];
Ptr_rmsg-> msg [3] = rch [8 * I + 6];
Ptr_rmsg-> msg [4] = rch [8 * I + 7];
Printf ("ClientA: char from Client % c = % c \ n", ptr_rmsg-> self_module_id, ptr_rmsg-> msg [0], ptr_rmsg-> msg [1], ptr_rmsg-> msg [2], ptr_rmsg-> msg [3], ptr_rmsg-> msg [4]);

}
O = n_read % 8;
If (o = 4)
{
Ptr_rmsg-> self_module_id = rch [8 * I + 0];
Ptr_rmsg-> dst_module_id = rch [8 * I + 1];
Ptr_rmsg-> length = rch [8 * I + 2];
Ptr_rmsg-> msg [0] = rch [8 * I + 3];
Printf ("ClientA: char from Client % c = % c \ n", ptr_rmsg-> self_module_id, ptr_rmsg-> msg [0]);
}
Else if (o = 5)
{
Ptr_rmsg-> self_module_id = rch [8 * I + 0];
Ptr_rmsg-> dst_module_id = rch [8 * I + 1];
Ptr_rmsg-> length = rch [8 * I + 2];
Ptr_rmsg-> msg [0] = rch [8 * I + 3];
Ptr_rmsg-> msg [1] = rch [8 * I + 4];
Printf ("ClientA: char from Client % c = % c \ n", ptr_rmsg-> self_module_id, ptr_rmsg-> msg [0], ptr_rmsg-> msg [1]);
}
Else if (o = 6)
{
Ptr_rmsg-> self_module_id = rch [8 * I + 0];
Ptr_rmsg-> dst_module_id = rch [8 * I + 1];
Ptr_rmsg-> length = rch [8 * I + 2];
Ptr_rmsg-> msg [0] = rch [8 * I + 3];
Ptr_rmsg-> msg [1] = rch [8 * I + 4];
Ptr_rmsg-> msg [2] = rch [8 * I + 5];
Printf ("ClientA: char from Client % c = % c \ n", ptr_rmsg-> self_module_id, ptr_rmsg-> msg [0], ptr_rmsg-> msg [1], ptr_rmsg-> msg [2]);
}
Else if (o = 7)
{
Ptr_rmsg-> self_module_id = rch [8 * I + 0];
Ptr_rmsg-> dst_module_id = rch [8 * I + 1];
Ptr_rmsg-> length = rch [8 * I + 2];
Ptr_rmsg-> msg [0] = rch [8 * I + 3];
Ptr_rmsg-> msg [1] = rch [8 * I + 4];
Ptr_rmsg-> msg [2] = rch [8 * I + 5];
Ptr_rmsg-> msg [3] = rch [8 * I + 6];
Printf ("ClientA: char from Client % c = % c \ n", ptr_rmsg-> self_module_id, ptr_rmsg-> msg [0], ptr_rmsg-> msg [1], ptr_rmsg-> msg [2], ptr_rmsg-> msg [3]);
}
Else
Printf ("recevie nothing \ n ");




Sleep (3 );
// Printf ("nengdu2 \ r \ n ");

}

If (FD_ISSET (self_sockfd, & write_fd_set ))
{Printf ("nengxieA \ n ");

Bzero (& dst_sockaddr, len );
Dst_sockaddr.sun_family = AF_LOCAL;
N_write = strlen (argv [1]);
For (k = 0; k {
Ptr_smsg-> self_module_id = argv [1] [8 * k + 0];
Ptr_smsg-> dst_module_id = argv [1] [8 * k + 1];
Ptr_smsg-> length = argv [1] [8 * k + 2];
Ptr_smsg-> msg [0] = argv [1] [8 * k + 3];
Ptr_smsg-> msg [1] = argv [1] [8 * k + 4];
Ptr_smsg-> msg [2] = argv [1] [8 * k + 5];
Ptr_smsg-> msg [3] = argv [1] [8 * k + 6];
Ptr_smsg-> msg [4] = argv [1] [8 * k + 7];
If (ptr_smsg-> dst_module_id = 'B ')
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/B ");
Else
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/C ");
Sendto (self_sockfd, ptr_smsg, 8, 0, (struct sockaddr *) & dst_sockaddr, len );
Printf ("ClientA: char to Client % c = % c \ n", ptr_smsg-> dst_module_id, argv [1] [8 * k + 3], argv [1] [8 * k + 4], argv [1] [8 * k + 5], argv [1] [8 * k + 6], argv [1] [8 * k + 7]);
}
O = n_write % 8; // determines the number of bytes of an incomplete message.
If (o = 4)
{
Ptr_smsg-> self_module_id = argv [1] [8 * k + 0];
Ptr_smsg-> dst_module_id = argv [1] [8 * k + 1];
Ptr_smsg-> length = argv [1] [8 * k + 2];
Ptr_smsg-> msg [0] = argv [1] [8 * k + 3];
Ptr_smsg-> msg [1] = '0 ';
Ptr_smsg-> msg [2] = '0 ';
Ptr_smsg-> msg [3] = '0 ';
Ptr_smsg-> msg [4] = '0 ';
If (ptr_smsg-> dst_module_id = 'B ')
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/B ");
Else
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/C ");
Sendto (self_sockfd, ptr_smsg, 4,0, (struct sockaddr *) & dst_sockaddr, len );
Printf ("ClientA: char to Client % c = % c \ n", ptr_smsg-> dst_module_id, argv [1] [8 * k + 3]);
}
Else if (o = 5)
{
Ptr_smsg-> self_module_id = argv [1] [8 * k + 0];
Ptr_smsg-> dst_module_id = argv [1] [8 * k + 1];
Ptr_smsg-> length = argv [1] [8 * k + 2];
Ptr_smsg-> msg [0] = argv [1] [8 * k + 3];
Ptr_smsg-> msg [1] = argv [1] [8 * k + 4];
Ptr_smsg-> msg [2] = '0 ';
Ptr_smsg-> msg [3] = '0 ';
Ptr_smsg-> msg [4] = '0 ';
If (ptr_smsg-> dst_module_id = 'B ')
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/B ");
Else
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/C ");
Sendto (self_sockfd, ptr_smsg, 5, 0, (struct sockaddr *) & dst_sockaddr, len );
Printf ("ClientA: char to Client % c = % c \ n", ptr_smsg-> dst_module_id, argv [1] [8 * k + 3], argv [1] [8 * k + 4]);
}
Else if (o = 6)
{
Ptr_smsg-> self_module_id = argv [1] [8 * k + 0];
Ptr_smsg-> dst_module_id = argv [1] [8 * k + 1];
Ptr_smsg-> length = argv [1] [8 * k + 2];
Ptr_smsg-> msg [0] = argv [1] [8 * k + 3];
Ptr_smsg-> msg [1] = argv [1] [8 * k + 4];
Ptr_smsg-> msg [2] = argv [1] [8 * k + 5];
Ptr_smsg-> msg [3] = '0 ';
Ptr_smsg-> msg [4] = '0 ';
If (ptr_smsg-> dst_module_id = 'B ')
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/B ");
Else
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/C ");
Sendto (self_sockfd, ptr_smsg, 6,0, (struct sockaddr *) & dst_sockaddr, len );
Printf ("ClientA: char to Client % c = % c \ n", ptr_smsg-> dst_module_id, argv [1] [8 * k + 3], argv [1] [8 * k + 4], argv [1] [8 * k + 5]);
}
Else if (o = 7)
{
Ptr_smsg-> self_module_id = argv [1] [8 * k + 0];
Ptr_smsg-> dst_module_id = argv [1] [8 * k + 1];
Ptr_smsg-> length = argv [1] [8 * k + 2];
Ptr_smsg-> msg [0] = argv [1] [8 * k + 3];
Ptr_smsg-> msg [1] = argv [1] [8 * k + 4];
Ptr_smsg-> msg [2] = argv [1] [8 * k + 5];
Ptr_smsg-> msg [3] = argv [1] [8 * k + 6];
Ptr_smsg-> msg [4] = '0 ';
If (ptr_smsg-> dst_module_id = 'B ')
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/B ");
Else
Strcpy (dst_sockaddr.sun_path, "/home/wewe/code/temp/C ");
Sendto (self_sockfd, ptr_smsg, 7,0, (struct sockaddr *) & dst_sockaddr, len );
Printf ("ClientA: char to Client % c = % c \ n", ptr_smsg-> dst_module_id, argv [1] [8 * k + 3], argv [1] [8 * k + 4], argv [1] [8 * k + 5], argv [1] [8 * k + 6]);
}
Else
Printf ("send nothing \ n ");

Sleep (3 );

// Printf ("nengxie \ r \ n ");
}



}

// Continue;

}





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