Linux C server and client simple communication

Source: Internet
Author: User
Tags socket error htons

Server.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

int main (int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
Char hello[1000]= "hello! Is you fine?\n ";

if (argc!=2)
{
fprintf (stderr, "usage:%s portnumber\a\n", argv[0]);
Exit (1);
}

if ((Portnumber=atoi (argv[1))) <0)
{
fprintf (stderr, "usage:%s portnumber\a\n", argv[0]);
Exit (1);
}

/* Start building the socket descriptor on server side */
if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)
{
fprintf (stderr, "Socket error:%s\n\a", Strerror (errno));
Exit (1);
}

/* Server-side populated sockaddr structure */
Bzero (&server_addr,sizeof (struct sockaddr_in));
Server_addr.sin_family=af_inet;
Server_addr.sin_addr.s_addr=htonl (Inaddr_any);
Server_addr.sin_port=htons (PortNumber);

/* Bundle SOCKFD Descriptor */
if (bind (SOCKFD, struct sockaddr *) (&AMP;SERVER_ADDR), sizeof (struct sockaddr)) ==-1)
{
fprintf (stderr, "Bind error:%s\n\a", Strerror (errno));
Exit (1);
}

/* Listen for SOCKFD descriptors */
if (Listen (sockfd,5) ==-1)
{
fprintf (stderr, "Listen error:%s\n\a", Strerror (errno));
Exit (1);
}

while (1)
{
/* Server blocked until client program establishes a connection */
sin_size=sizeof (struct sockaddr_in);
if ((New_fd=accept (SOCKFD, (struct sockaddr *) (&AMP;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));

FSCANF (stdin, "%s", hello);
if (Write (New_fd,hello,strlen (hello)) ==-1)
{
fprintf (stderr, "Write error:%s\n", Strerror (errno));
Exit (1);
}
/* This communication is over */
Close (NEW_FD);
/* Cycle the next one */
}
Close (SOCKFD);
Exit (0);
}

Client.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

int main (int argc, char *argv[])
{
int sockfd;
Char buffer[1024];
struct sockaddr_in server_addr;
struct Hostent *host;
int portnumber,nbytes;

if (argc!=3)
{
fprintf (stderr, "usage:%s hostname portnumber\a\n", argv[0]);
Exit (1);
}

if ((Host=gethostbyname (argv[1))) ==null)
{
fprintf (stderr, "GetHostName error\n");
Exit (1);
}

if ((Portnumber=atoi (argv[2))) <0)
{
fprintf (stderr, "usage:%s hostname portnumber\a\n", argv[0]);
Exit (1);
}

/* Client starts to build SOCKFD descriptor */
if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)
{
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));
Server_addr.sin_family=af_inet;
Server_addr.sin_port=htons (PortNumber);
server_addr.sin_addr=* (struct in_addr *) host->h_addr);

/* Client Initiates connection request */
if (Connect (SOCKFD, (struct sockaddr *) (&AMP;SERVER_ADDR), sizeof (struct sockaddr)) ==-1)
{
fprintf (stderr, "Connect error:%s\a\n", Strerror (errno));
Exit (1);
}

/* Connection succeeded */
if ((Nbytes=read (sockfd,buffer,1024)) ==-1)
{
fprintf (stderr, "Read error:%s\n", Strerror (errno));
Exit (1);
}
buffer[nbytes]= ' + ';
printf ("I have received:%s\n", buffer);
/* End Communication */
Close (SOCKFD);
Exit (0);
}

Makefile

All:server Client
Server:server.c
GCC $^-o [email protected]
Client:client.c
GCC $^-o [email protected]



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux C server and client simple communication

Related Article

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.