Server side: SERVER.C
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAXBUF 1024
int main (int argc, char **argv)
{
int SOCKFD, NEW_FD;
Socklen_t Len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport, lisnum;
Char buf[maxbuf + 1];
Fd_set RfDs;
struct Timeval TV;
int retval, maxfd =-1;
if (argv[2])
MyPort = Atoi (argv[2]);/ * Converts a command-line string to an integer. For Port * /
Else
MyPort = 8888;/ * Set the default port * /
if (Argv[3])
Lisnum = Atoi (argv[3]);/ * Listen for queue size * /
Else
Lisnum = 2;
if ((SOCKFD = socket (pf_inet, sock_stream, 0)) = = = 1) {/ * Create a Socket object * /
Perror ("socket");
Exit (Exit_failure);
}
Bzero (&my_addr, sizeof (MY_ADDR));
my_addr.sin_family = pf_inet;/ * Address protocol * /
My_addr.sin_port = htons (MyPort);/ * Address port * /
if (Argv[1])
MY_ADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Else
MY_ADDR.SIN_ADDR.S_ADDR = Inaddr_any;/ * Otherwise default native random address * /
if (bind (SOCKFD, struct sockaddr *) &my_addr, sizeof (struct sockaddr)) = =-1) {/ * Bind address information * /
Perror ("bind");
Exit (Exit_failure);
}
if (Listen (SOCKFD, lisnum) = =-1) {/* Monitor the network, the maximum number of queues is lisnum*/
Perror ("Listen");
Exit (Exit_failure);
}
while (1)
{
printf ("\ n----wait for new connect\n");
len = sizeof (struct sockaddr);
if (new_fd =accept (SOCKFD, (struct sockaddr *) &their_addr,&len) = = =-1) {
/ * block wait for connection. The connection successfully returns the new socket descriptor, THEIR_ADDR stores the client's sockaddr address information
Perror ("accept");
Exit (errno);
} else
printf ("Server:got connection from%s, port%d, socket%d\n",
Inet_ntoa (THEIR_ADDR.SIN_ADDR), Ntohs (Their_addr.sin_port), NEW_FD);
while (1)
{
Fd_zero (&rfds);/* Clear ZeroRfDs Collection*/
Fd_set (0, &rfds);/* Add the standard inputRfDs Collection*/
Fd_set (NEW_FD, &rfds); /*Join the new_fd .RfDs Collection*/
MAXFD = NEW_FD;
Tv.tv_sec = 1;
tv.tv_usec = 0;
retval = Select (Maxfd + 1, &rfds, NULL, NULL, &TV); /*MAXFD + 1 listening maximum plus one*/
if (retval = =-1)
{
Perror ("select");
Exit (Exit_failure);
} else if (retval = = 0) {
Continue
}
Else
{
if (Fd_isset (0, &rfds))/* Assumes standard input is readable */
{
Bzero (buf, maxbuf + 1);
Fgets (buf, Maxbuf, stdin);
if (!strncasecmp (buf, "Quit", 4)) {
printf ("I'll quit!\n");
Break
}
Len = Send (NEW_FD, buf, strlen (BUF)-1, 0);
if (len > 0)
printf ("Send successful,%d byte send!\n", Len);
else {
printf ("Send failure!");
Break
}
}
if (Fd_isset (NEW_FD, &rfds))/ * assumed to be new_fd readable * /
{
Bzero (buf, maxbuf + 1);
Len = recv (new_fd, buf, maxbuf, 0);
if (len > 0)
printf ("recv success: '%s ',%dbyte recv\n", buf, Len);
Else
{
if (Len < 0)
printf ("recv failure\n");
Else
{
printf ("The Ohter one end, quit\n");
Break
}
}
}
}
}
Close (NEW_FD);
printf ("Need othe CONNECDT (no->quit)");
Fflush (stdout);
Bzero (buf, maxbuf + 1);
Fgets (buf, Maxbuf, stdin);
if (!strncasecmp (buf, "no", 2))
{
printf ("quit!\n");
Break
}
}
Close (SOCKFD);
return 0;
}
Client program: CLIENT.C
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAXBUF 1024
int main (int argc, char **argv)
{
int SOCKFD, Len;
struct sockaddr_in dest;
Char buffer[maxbuf + 1];
Fd_set RfDs;
struct Timeval TV;
int retval, maxfd =-1;
if (argc! = 3)
{
printf ("argv format errno,pls:\n\t\t%s IP port\n", argv[0], argv[0]);
Exit (Exit_failure);
}
if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
{
Perror ("Socket");
Exit (Exit_failure);
}
Bzero (&dest, sizeof (dest));
dest.sin_family = af_inet;
Dest.sin_port = htons (Atoi (argv[2]));
if (Inet_aton (argv[1], (struct in_addr *) &dest.sin_addr.s_addr) = = 0)
{
Perror (argv[1]);
Exit (Exit_failure);
}
if (Connect (SOCKFD, (struct sockaddr *) &dest, sizeof (dest))! = 0)
{
Perror ("Connect");
Exit (Exit_failure);
}
printf ("\nget ready pls chat\n");
while (1)
{
Fd_zero (&rfds);
Fd_set (0, &rfds);
Fd_set (SOCKFD, &rfds);
MAXFD = SOCKFD;
Tv.tv_sec = 1;
tv.tv_usec = 0;
retval = Select (Maxfd + 1, &rfds, NULL, NULL, &TV);
if (retval = =-1)
{
printf ("Select%s", Strerror (errno));
Break
}
else if (retval = = 0)
Continue
Else
{
if (Fd_isset (SOCKFD, &rfds))
{
Bzero (buffer, maxbuf + 1);
Len = recv (sockfd, buffer, maxbuf, 0);
if (len > 0)
printf ("recv message: '%s ',%d byte recv\n", buffer, Len);
Else
{
if (Len < 0)
printf ("Message recv failure\n");
Else
{
printf ("The othe Quit, quit\n");
Break
}
}
}
if (Fd_isset (0, &rfds))
{
Bzero (buffer, maxbuf + 1);
Fgets (buffer, maxbuf, stdin);
if (!strncasecmp (buffer, "quit", 4)) {
printf ("I'll quit\n");
Break
}
Len = Send (sockfd, buffer, strlen (buffer)-1, 0);
if (Len < 0) {
printf ("Message send Failure");
Break
} else
Printf
("Send success,%d byte send\n", Len);
}
}
}
Close (SOCKFD);
return 0;
}
test methods such as the following:
gcc server.c-o Server
gcc client.c-o Client
./server 172.16.148.114 8888/* under one terminal. 172.16.148.114 is the IP address of my virtual machine * /
./client 172.16.148.114 8888/* Another terminal under * /
The Select function does not use this article: http://blog.csdn.net/qq_21792169/article/details/50450360
Linux Network Programming Chat program (TCP protocol Select)