Professional Documents
Culture Documents
• The client machine (or the client process) makes the request
for some resource or service, and the server machine (the
server process) handles the request and sends the response
(result) back to the client.
#include <stdio.h>
#include <sys/types.h> // def. of data types used in system calls
#include <sys/socket.h> //def. of structures needed for sockets
#include <netinet/in.h> // constants and structures for Internet
// domain addresses
void error(char *msg) // func. Is called when system calls fail
{
perror(msg);
exit(1);
}
Server Code
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen; // sockfd, newsockfd=>file
// descriptor, portno stores the port number on which the server
// accepts connections, clilen=> size of the address of the client, n=>
no. of char to read or write
char buffer[256];
struct sockaddr_in serv_addr, cli_addr; //sockaddr_in => structure
containing Internet address
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}