Linux socket chat program (thread version pthread)

Source: Internet
Author: User

/***************** Server. c *************/

# Include <stdio. h>
# Include <strings. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>

# Define Port 1234
# Define backlog 1
# Define maxdatasalize 100

Struct ARG
{
Int connfd;
Struct sockaddr_in client;
};
Void * func (void * Arg );
Void main ()
{
Pthread_t tid;
Int listenfd, connectfd;
Struct sockaddr_in server;
Struct sockaddr_in client;
Socklen_t addrlen;
Char Buf [maxdatasize];
Struct Arg * Arg, in;
Arg = & in;
 
If (listenfd = socket (af_inet, sock_stream, 0) =-1)
{
Perror ("socket () error .");
Exit (1 );
}
 
Int opt = 1;
Setsockopt (listenfd, sol_socket, so_reuseaddr, & OPT, sizeof (OPT ));
 
Bzero (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (port );
Server. sin_addr.s_addr = htonl (inaddr_any );
If (BIND (listenfd, (struct sockaddr *) & server, sizeof (server) =-1)
{
Perror ("BIND () error .");
Exit (1 );
}
If (Listen (listenfd, backlog) =-1)
{
Perror ("Listen () error .");
Exit (1 );
}
 
Addrlen = sizeof (client );
While (1)
{
If (connectfd = accept (listenfd, (struct sockaddr *) & client, & addrlen) =-1)
{
Perror ("accept () error .");
Exit (1 );
}
Printf ("You got a connection from Client IP is: % s, port is: % d/N"
, Inet_ntoa (client. sin_addr), htons (client. sin_port ));

// Arg = (struct Arg *) malloc (sizeof (struct Arg ));
Arg-> connfd = connectfd;
Arg-> client = client;
 
If (pthread_create (& tid, null, func, (void *) Arg ))
{
Perror ("error pthread_create ");
Exit (1 );
}
While (1)
{
Recv (connectfd, Buf, maxdatasize, 0 );
Printf ("the client says: % s/n", Buf );
}
Close (connectfd );
Close (listenfd );
}
}

Void * func (void * Arg)
{
Char Buf [maxdatasize];
Struct Arg * Info;
Info = (struct Arg *) ARG;
While (1)
{

Fgets (BUF, maxdatasize, stdin );
Send (Info-> connfd, Buf, maxdatasize, 0 );
 
}

}

 

 

// The following is the client

/// // Client. C /////////////////////////////////////// ////

 

 

 

 

# Include <stdio. h>
# Include <unistd. h>
# Include <strings. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <netdb. h>

# Define Port 1234
# Define maxdatasalize 100

Void * func (void * Arg );
Int main (INT argc, char * argv [])
{
Pthread_t tid;
Int sockfd, num;
Charbuf [maxdatasize] = "123456789 ";
Struct hostent * He;
Struct sockaddr_in server;

If (argc! = 2)
{
Printf ("usage % S <IP address>/N", argv [0]);
Exit (1 );
}
If (He = gethostbyname (argv [1]) = NULL)
{
Printf ("gethostbyname error/N ");
Exit (1 );
}
If (sockfd = socket (af_inet, sock_stream, 0) =-1)
{
Printf ("socket () Error/N ");
Exit (1 );
}
Bzero (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (port );
Server. sin_addr = * (struct in_addr *) He-> h_addr );
If (connect (sockfd, (struct sockaddr *) & server, sizeof (server) =-1)
{
Printf ("connetc () Error/N ");
Exit (1 );
}
 
If (pthread_create (& tid, null, func, (void *) & sockfd ))
{
Perror ("error pthread_create ");
Return 2;

}
While (1)
{

Num = Recv (sockfd, Buf, maxdatasize, 0 );
Printf ("Server Message: % s/n", Buf );
}
Close (sockfd );
}

Void * func (void * Arg)
{
Int * sockfd;
Char Buf [maxdatasize];
Sockfd = (int *) ARG;
While (1)
{
Fgets (BUF, maxdatasize, stdin );
Send (* sockfd, Buf, maxdatasize, 0 );
}
}

 

 

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.