The implementation of the server and the client chat with each other, no order restrictions, can be carried out at the same time.
Service 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>
#define MAXBUF 1024
int main (int argc, char *argv[])
{
int pid;
int SOCKFD, NEW_FD;
Socklen_t Len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport, lisnum;
Char buf[maxbuf + 1];
if (argv[2])
MyPort = Atoi (argv[2]); /* Converts command line string to Integer, for Port/*
Else
MyPort = 7575; /* Set the default port * *
if (Argv[3])
Lisnum = Atoi (argv[3]); /* Listening Queue Size * *
Else
Lisnum = 5;
if ((SOCKFD = socket (af_inet, sock_stream, 0)) = = 1)/* Create socket Object * *
{
Perror ("socket");
Exit (Exit_failure);
}
Bzero (&my_addr, sizeof (MY_ADDR));
my_addr.sin_family = af_inet; /* Address Agreement * *
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, the default local machine arbitrary 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 Network * *
{
Perror ("Listen");
Exit (Exit_failure);
}
printf ("Wait for connect\n");
len = sizeof (struct sockaddr);
if ((New_fd =accept (SOCKFD, (struct sockaddr *) &their_addr,&len)) = = 1)
/* Blocking wait connection, the connection successfully returns the new socket descriptor, THEIR_ADDR store the client's sockaddr address information
{
Perror ("accept");
Exit (Exit_failure);
}
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);
if ( -1== (Pid=fork ()))/* Create a new process * *
{
Perror ("fork"); exit (Exit_failure);
}
else if (PID = = 0)/* subprocess is used to send message * *
{
while (1)
{
Bzero (buf, maxbuf + 1);
printf ("Input to send:");
Fgets (buf, Maxbuf, stdin);
if (!strncasecmp (buf, "Quit", 4))
{
printf ("I'll close the connect!\n");
Break
}
Len = Send (NEW_FD, buf, strlen (BUF)-1, 0);
if (Len < 0)
{
printf ("Message '%s ' send Failure!errno the code is%d,errno '%s ' \ n",
BUF, errno, Strerror (errno));
Break
}
}
}
else/* The parent process is used to receive the message * *
{
while (1)
{
Bzero (buf, maxbuf + 1);
Len = recv (new_fd, buf, maxbuf, 0);
if (len > 0)
printf ("Message recv successful: '%s ',%dbyte recv\n", buf, Len);
else if (Len < 0)
{
printf ("Recv failure!errno code is%d,errno '%s ' \ n",
errno, Strerror (errno));
Break
}
Else
{
printf ("The other one Close quit\n");
Break
}
}
}
Close (NEW_FD);
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>
#define MAXBUF 1024
int main (int argc, char **argv)
{
int SOCKFD, Len;
struct sockaddr_in dest;
Char buffer[maxbuf + 1];
if (argc!= 3)
{
printf ("Error format,it must be:\n\t\t%s IP port\n", argv[0]);
Exit (Exit_failure);
}
if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {/* Create socket Object/*
Perror ("Socket");
Exit (errno);
}
printf ("Socket created\n");
Bzero (&dest, sizeof (dest)); /* Clear 0 Operation * *
dest.sin_family = af_inet; /* Address Agreement * *
Dest.sin_port = htons (atoi (argv[2)); /* Address Port * *
if (Inet_aton (argv[1), (struct in_addr *) &dest.sin_addr.s_addr) = = 0)/* IP address/
{
Perror (argv[1]); Exit (errno);
}
if (Connect (SOCKFD, (struct sockaddr *) &dest, sizeof (dest)) ==-1)/* Initiate connection * *
{
Perror ("Connect");
Exit (errno);
}
printf ("Server connected\n");
pid_t pid;
if ( -1== (Pid=fork ()))/* Create a new process * *
{
Perror ("fork"); exit (Exit_failure);
}
else if (pid==0)/* subprocess is used to receive messages, do not understand the process of their own to Baidu?
{
while (1)
{
Bzero (buffer, maxbuf + 1);
Len = recv (sockfd, buffer, maxbuf, 0);
if (len > 0)
printf ("recv Successful: '%s ',%d byte recv\n", buffer, Len);
else if (Len < 0)
{
Perror ("recv");
Break
}
Else
{
printf ("The other one close, quit\n");
Break
}
}
}
else/* The parent process is used to send a message.
{
while (1)
{
Bzero (buffer, maxbuf + 1);
printf ("Pls send message to send:");
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)
{
Perror ("send");
Break
}
}
}
Close (SOCKFD);
return 0;
}
The test methods are as follows:
GCC Server.c-o Server
GCC Client.c-o Client
./server 172.16.148.114 8888/* A terminal, 172.16.148.114 is the IP address of my virtual machine * *
./client 172.16.148.114 8888/* * under another terminal
Add a knowledge point:
int shutdown (int sockfd,int how);
Shutdown can directly close the descriptor, regardless of the descriptor's reference number, you can choose to abort a direction of the connection
How there are three different ways to be
SHUT_RD (0): Turn off Read on SOCKFD, this option will not allow SOCKFD read operation.
SHUT_WR (1): Turn off the write feature of SOCKFD, this option will not allow the SOCKFD write operation.
Shut_rdwr (2): Turn off the read and write function of SOCKFD.