The client code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PortNumber 3333
int main (int argc, char *argv[])
{
int sockfd;
Char buffer[1024];
struct sockaddr_in server_addr;
struct Hostent *host;
/* Use hostname to query the host name */
if (argc!=2)
{
fprintf (stderr, "usage:%s hostname \a\n", argv[0]);
Exit (1);
}
if ((Host=gethostbyname (argv[1))) ==null)
{
fprintf (stderr, "GetHostName error\n");
Exit (1);
}
/* Client starts to build SOCKFD descriptor */
if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)//af_inet:internet; Sock_stream:tcp
{
fprintf (stderr, "Socket error:%s\a\n", Strerror (errno));
Exit (1);
}
/* Client program fills the data on the server */
Bzero (&server_addr,sizeof (SERVER_ADDR)); Initialize, set 0
Server_addr.sin_family=af_inet; IPV4
Server_addr.sin_port=htons (PortNumber); (Convert short data on this machine to short data on the network) port number
server_addr.sin_addr=* (struct in_addr *) host->h_addr); IP Address
/* Client Initiates connection request */
if (Connect (SOCKFD, (struct sockaddr *) (&SERVER_ADDR), sizeof (struct sockaddr)) ==-1)
{
fprintf (stderr, "Connect error:%s\a\n", Strerror (errno));
Exit (1);
}
/* Connection succeeded */
printf ("Please input char:\n");
/* Send data */
Fgets (Buffer,1024,stdin);
Send (Sockfd,buffer,strlen (buffer), 0);
/* End Communication */
Close (SOCKFD);
Exit (0);
}
The server code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORTNUMBER The port number of the 3333//server and the port number of the client must be equal
int main (int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
int nbytes;
Char buffer[1024];
/* Start building the SOCKFD descriptor on server side */
if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)//Af_inet:ipv4; Sock_stream:tcp
{
fprintf (stderr, "Socket error:%s\n\a", Strerror (errno));
Exit (1);
}
/* Server-side populated sockaddr structure */
Bzero (&server_addr,sizeof (struct sockaddr_in)); Initialize, set 0
Server_addr.sin_family=af_inet; Internet
Server_addr.sin_addr.s_addr=htonl (Inaddr_any); (Converting Long data on this machine to long data on the network) and any host communication//inaddr_any means that data that can receive any IP address is bound to all IP
Server_addr.sin_port=htons (PortNumber); (Convert short data on this machine to short data on the network) port number
/* Bundle SOCKFD Descriptor to IP address */
if (bind (SOCKFD, struct sockaddr *) (&SERVER_ADDR), sizeof (struct sockaddr)) ==-1)
{
fprintf (stderr, "Bind error:%s\n\a", Strerror (errno));
Exit (1);
}
/* Set the maximum number of clients allowed to connect */
if (Listen (sockfd,5) ==-1)
{
fprintf (stderr, "Listen error:%s\n\a", Strerror (errno));
exit (1);
}
while (1)
{
/* Server blocked, Until the client program establishes a connection */
sin_size=sizeof (struct sockaddr_in);
if (New_fd=accept (SOCKFD, (struct sockaddr *) (&CLIENT_ADDR), &sin_size) ==-1)
{
fprintf (stderr, "Accept error:%s\n\a", Strerror (errno));
exit (1);
}
fprintf (stderr, "Server get connection from%s\n", Inet_ntoa (Client_ ADDR.SIN_ADDR)); Converts a network address into a. String
if ((Nbytes=read (new_fd,buffer,1024)) ==-1)//Here can be written as if ((Nbytes=recv (new_fd,buffer,1024,0)) ==-1)
if ((Nbytes=recv (new_fd,buffer,1024,0)) ==-1)
{
fprintf (stderr, "Read error:%s\n", Strerror (errno));
Exit (1);
}
buffer[nbytes]= ' + ';
printf ("Server received%s\n", buffer);
/* This communication is over */
Close (NEW_FD);
/* Cycle the next one */
}
/* End Communication */
Close (SOCKFD);
Exit (0);
}
Implementation of TCP server and client in Linux under C language learning