Home > other >  The socket communication result in abnormal respect to multithreading
The socket communication result in abnormal respect to multithreading

Time:09-30

Function description: create a client thread at a time, in the process of the client and server to establish a connection, if there is a new client thread creation, will be put before the socket connection to disconnect and exit the thread, the problem is the client, when closing the socket, will cause the main process, related information is caused by the SIGPIPE process exits, please give guidance, thank you

The client code:
Void * TL_SynOnlineThreadProcess (void * args)
{
StruAudioProcess * pstAudioObj=(struAudioProcess * args;
Int sockfd;
int len;
Int RevData;
Struct sockaddr_un address;
Int result;
Char buff [100].


/* Create a socket for a client. */

Sockfd=socket (AF_UNIX SOCK_STREAM, 0).

/* Name the socket, as agreed with the server. */

Address. Sun_family=AF_UNIX;
Strcpy (address. Sun_path UNIX_SOCKET_FILE);
Len=sizeof (address);

/* Now connect our socket to the server 's socket. */

Result=connect (sockfd, (struct sockaddr *) & amp; Address, len);

If (result==1) {
//printf (" Unix connect fail % s % d \ n ", a __FILE__ and __LINE__);
Sprintf (buff, "connect failure pid % d, tid % d.", getpid (), gettid ());
TL_GetMsgLog (buff, 1111);
Close (sockfd);
Return NULL;
}


Sprintf (buff, "dar TL_SynOnlineThread pid % d, % d", getpid (), gettid ());
TL_GetMsgLog (buff, 9999);

Pthread_mutex_lock (& amp; Process_mutex);
G_Ttid=gettid ();
Pthread_mutex_unlock (& amp; Process_mutex);


While (1)
{

If (g_Ttid!=gettid ())
{


Close (sockfd);

Pthread_exit (0);


}
The else
{

The recv (sockfd, & amp; RevData, sizeof (int), 0);
If (RevData=1) https://bbs.csdn.net/topics/=
{
PstAudioObj - & gt; IPrmSet=1;
//printf (" TLTL line_ pid_ tid_ % d % d % d % s \ n ", gettid (), getpid () and __LINE__, __FUNCTION__);
}
If (RevData & gt;
=100){
PstAudioObj - & gt; IPrmSet=2;
G_Vol=RevData - 100;
}
}


}


//return NULL;
}


The server thread:
Void * TL_MsgProcess (void * arg)
{
Int server_sockfd client_sockfd;
Int server_len client_len;
Struct sockaddr_un server_address;
Struct sockaddr_un client_address;
Int ret.

Pthread_mutex_init (& amp; Update_mutex, NULL);


/* Remove any old socket and create an unnamed socket for the server. */

The unlink (UNIX_SOCKET_FILE);
Server_sockfd=socket (AF_UNIX SOCK_STREAM, 0).

/* the Name of the socket. */

Server_address. Sun_family=AF_UNIX;
Strcpy (server_address. Sun_path UNIX_SOCKET_FILE);
Server_len=sizeof (server_address);
Ret=bind (server_sockfd, (struct sockaddr *) & amp; Server_address server_len);
If (ret==1)
{
Close (server_sockfd);
The unlink (UNIX_SOCKET_FILE);

Return NULL;
}

/* Create a connection queue and wait for the clients. */

Ret=listen (server_sockfd MAX_LISTEN_NUM_UNIX_SOCKET);
If (ret==1)
{

Close (server_sockfd);
The unlink (UNIX_SOCKET_FILE);
}


While (1)
{

//printf (" server waiting \ n ");

Accept a connection/*. */

Client_len=sizeof (client_address);
Client_sockfd=accept (server_sockfd, (struct sockaddr *) & amp; Client_address, & amp; Client_len);
If (client_sockfd & lt; 0)
{

continue;
}

//printf (" accecpt a connection: % d \ r \ n ", client_sockfd);

Creat a thread/* */
Pthread_t id;
Pthread_attr_t attr;
Pthread_attr_init (& amp; Attr);
Pthread_attr_setdetachstate (& amp; Attr, PTHREAD_CREATE_DETACHED);


Pthread_create (& amp; Id, & amp; Attr (void *) TL_MsgClientThreadProcess, (void *) (& amp; Client_sockfd));


}
//close (client_sockfd);
//close (server_sockfd);


Void * TL_MsgClientThreadProcess (void * PFD)
{
Int client_sockfd;
The int data;
Int send_flag;
Int send_fifo_flag;
Char buff [100].
Int heatbeat=0 xa5a5a5a5;

Client_sockfd=* (PFD) (int *);
Send_flag=0;
Send_fifo_flag=0;

Pthread_mutex_lock (& amp; Update_mutex);
G_Ttid=gettid ();
Pthread_mutex_unlock (& amp; Update_mutex);

While (1)
{

if (! Semaphore_p ())
{
G_err_msg_code=errno;
Printf (" TLTL line_ % d % d % s % s \ n ", __LINE__, __FUNCTION__, errno, strerror (errno));
//goto _end_msg_client;
}

If (g_PCSendToDev_OK==1)
{
If (send_flag==0)
{
Data=https://bbs.csdn.net/topics/1;
//write (client_sockfd, & amp; The data, sizeof (data));
Send (client_sockfd, & amp; Data, sizeof (data), 0);
Send_flag=1;
}
}
The else
{
Send_flag=0;
//write (client_sockfd, & amp; Heatbeat, sizeof (heatbeat));
//sleep (10);
}



If (g_FIFO_OK==1)
{
If (send_fifo_flag==0)
{

Data=https://bbs.csdn.net/topics/g_read_val + 100;//make sure data>=100
//write (client_sockfd, & amp; The data, sizeof (data));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related