Linux network multi-thread programming example

Source: Internet
Author: User
Tags htons

UDP is used.
The server can accept ten clients at the same time, and each client can communicate with each other point-to-point.
The client broadcast of the server. It can also communicate with the server. The server can also broadcast.
You must first understandSource codeCommand in: "/W" broadcast; "/s n" to a client; "/SV" to the server;

The command is the part in quotation marks. Pay attention to spaces.

ServerCode:

# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <pthread. h>
# Define Port 8889
# Deprecision max_size 256
//////////////////////////////////////// /////////////////
Int sockfd, sockfd2 [10], RET, I = 0, J, flag = 0;
Struct sockaddr_in ADDR;
Struct sockaddr_in addr2 [10];
Int unsigned addr_len = sizeof (struct sockaddr_in );
Char inbuffer [256];
Char outbuffer [256];
// Char tempbuffer [256];
// Int name [1];
Void threads1 ();
Void threads2 ();
//////////////////////////////////////// ///////////////
Int main ()
{
If (sockfd = socket (af_inet, sock_dgram, 0) <0)
{Perror ("socket ");
Exit (1 );
}
Bzero (& ADDR, sizeof (ADDR ));
ADDR. sin_family = af_inet;
ADDR. sin_port = htons (port );
ADDR. sin_addr.s_addr = htonl (inaddr_any );
If (BIND (sockfd, (struct sockaddr *) & ADDR, sizeof (ADDR) <0)
{
Perror ("Connect ");
Exit (1 );
}
Pthread_t ids1;
Ret = pthread_create (& ids1, null, (void *) & threads1, null );
If (Ret! = 0)
{
Printf ("cross-region slow drag \ n ");
Exit (1 );
}

Pthread_t ids2;
Ret = pthread_create (& ids2, null, (void *) & threads2, null );
If (Ret! = 0)
{
Printf ("cross-region slow drag \ n ");
Exit (1 );
}
Pthread_join (ids1, null );
Pthread_join (ids2, null );

Close (sockfd );
Return 1;
}
//////////////////////////////////////// ////////////////////
Void threads1 (void)
{
For (;
{
Bzero (outbuffer, sizeof (outbuffer ));
Read (stdin_fileno, outbuffer, sizeof (outbuffer ));
If (strncmp (outbuffer, "/W", 3) = 0) //
{
Strcat (outbuffer, "server said :");
If (addr2 [0]. sin_port! = 0)
{
For (j = 0; j <= 9; j ++)
{
If (addr2 [J]. sin_port! = 0) // forward to another client
{
Sendto (sockfd2 [J], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [J], addr_len );
Perror ("sendto ");
}
}
} Else {
Printf ("No clent \ n ");
}
}
Else if (strncmp (outbuffer, "/s", 3) = 0) //// // to a client
{
If (strncmp (outbuffer, "/S 0", 5) = 0)
{

Sendto (sockfd2 [0], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [0], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 1", 5) = 0)
{

Sendto (sockfd2 [1], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [1], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 2", 5) = 0)
{

Sendto (sockfd2 [2], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [2], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 3", 5) = 0)
{

Sendto (sockfd2 [3], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [3], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 4", 5) = 0)
{

Sendto (sockfd2 [4], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [4], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 5", 5) = 0)
{

Sendto (sockfd2 [5], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [5], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 6", 5) = 0)
{

Sendto (sockfd2 [6], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [6], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 7", 5) = 0)
{

Sendto (sockfd2 [7], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [7], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 8", 5) = 0)
{

Sendto (sockfd2 [8], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [8], addr_len );
Perror ("sendto ");
}
Else if (strncmp (outbuffer, "/S 9", 5) = 0)
{

Sendto (sockfd2 [9], outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & addr2 [9], addr_len );
Perror ("sendto ");
}
}
Else {
Printf ("error message! \ N ");
}
}
}
//////////////////////////////////////// ////////////////////
Void threads2 (void)
{
For (j = 0; j <= 9; j ++)
{
Addr2 [J]. sin_family = af_inet;
Addr2 [J]. sin_addr.s_addr = 0;
Addr2 [J]. sin_port = 0;
}
For (;{
Bzero (inbuffer, sizeof (inbuffer ));
Recvfrom (sockfd, inbuffer, sizeof (inbuffer), 0, (struct sockaddr *) & ADDR, & addr_len );

Printf ("receive from clent --> % s", inbuffer );
For (j = 0; j <= 9; j ++)
{
If (ADDR. sin_port = addr2 [J]. sin_port) Flag = Flag + 1; // determine whether the client is logged on

}
If (flag = 0) // if not, record
{
Sockfd2 [I] = sockfd;
Addr2 [I]. sin_port = ADDR. sin_port;
Addr2 [I]. sin_addr.s_addr = ADDR. sin_addr.s_addr;
If (I = 9) I = 0;
I = I + 1;
}
Flag = 0;
/// // Before sending
If (strncmp (inbuffer, "/W", 3) = 0) //
{
Strcat (inbuffer, "A clent said to everone ");
If (addr2 [0]. sin_port! = 0)
{
For (j = 0; j <= 9; j ++)
{
If (addr2 [J]. sin_port! = 0 & addr2 [J]. sin_port! = ADDR. sin_port) // forward to another client
{
Sendto (sockfd2 [J], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [J], addr_len );
Perror ("sendto ");
}
}
}
}
Else if (strncmp (inbuffer, "/s", 3) = 0) //// // to a client
{
If (strncmp (inbuffer, "/S 0", 5) = 0)
{

Sendto (sockfd2 [0], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [0], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 1", 5) = 0)
{

Sendto (sockfd2 [1], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [1], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 2", 5) = 0)
{

Sendto (sockfd2 [2], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [2], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 3", 5) = 0)
{

Sendto (sockfd2 [3], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [3], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 4", 5) = 0)
{

Sendto (sockfd2 [4], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [4], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 5", 5) = 0)
{

Sendto (sockfd2 [5], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [5], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 6", 5) = 0)
{

Sendto (sockfd2 [6], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [6], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 7", 5) = 0)
{

Sendto (sockfd2 [7], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [7], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 8", 5) = 0)
{

Sendto (sockfd2 [8], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [8], addr_len );
Perror ("sendto ");
}
Else if (strncmp (inbuffer, "/S 9", 5) = 0)
{
Sendto (sockfd2 [9], inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & addr2 [9], addr_len );
Perror ("sendto ");
}
}
Else if (strncmp (inbuffer, "/SV", 4) = 0) //
{
Printf ("clent to server \ n ");
}
Else {
Bzero (inbuffer, sizeof (inbuffer ));
Strcpy (inbuffer, "The message error! ");
Sendto (sockfd, inbuffer, strlen (inbuffer), 0, (struct sockaddr *) & ADDR, addr_len); // notify the client if the message format is incorrect
Perror ("sendto ");
}
}
}

Bytes ----------------------------------------------------------------------------------------------------------------
Client code:

# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/Wait. H>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Define Port 8889
# Deprecision max_size 256
# Define server_ip "127.0.0.1"
//////////////////////////////////////// /////////
Int sockfd;
Struct sockaddr_in ADDR;
Int unsigned addr_len = sizeof (struct sockaddr_in );
Char inbuffer [256];
Char outbuffer [256];

//////////////////////////////////////// ///////////
Void threads1 ();
Void threads2 ();

Int main (void)
{
If (sockfd = socket (af_inet, sock_dgram, 0) <0)
{
Perror ("socket ");
Exit (1 );
}
Bzero (& ADDR, sizeof (ADDR ));
ADDR. sin_family = af_inet;
ADDR. sin_port = htons (port );
ADDR. sin_addr.s_addr = inet_addr (server_ip );

Pthread_t ids1;
Pthread_t ids2;
If (pthread_create (& ids1, null, (void *) & threads1, null )! = 0)
{
Printf ("thread creation error \ n ");
Exit (1 );
}

// Pthread_mutex_init (& mutex, null );
If (pthread_create (& ids2, null, (void *) & threads2, null )! = 0)
{
Printf ("thread creation error \ n ");
Exit (1 );
}
Pthread_join (ids1, null );
Pthread_join (ids2, null );
Close (sockfd );
Return 1;
}

//////////////////////////////////////// ///////////////////

Void threads1 ()
{

For (;;){
Bzero (outbuffer, sizeof (outbuffer ));
Fgets (outbuffer, max_size, stdin );
Sendto (sockfd, outbuffer, strlen (outbuffer), 0, (struct sockaddr *) & ADDR, addr_len );
}
}

Void threads2 ()
{
For (;;){
Bzero (inbuffer, sizeof (inbuffer ));
Recvfrom (sockfd, inbuffer, sizeof (inbuffer), 0, (struct sockaddr *) & ADDR, & addr_len );
Printf ("receive from server -->: % s \ n", inbuffer );
}
}

Be careful that the symbols in the source code may be used as escape characters.
If a compilation error occurs, add your own symbols.

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.