0% found this document useful (0 votes)
18 views

Lab 9

Uploaded by

Arnab Pramanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 9

Uploaded by

Arnab Pramanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Code for a simple client

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

void error(const char *msg)

perror(msg);

exit(0);

int main()

int sockfd, portno, n;

struct sockaddr_in serv_addr;

struct hostent *server;

char buffer[256],hostname[20],portnumber[10];

printf("\nClient requires the following to operate");

printf("\nPlease provide server hostname: ");

scanf("%s",hostname);

printf("\nPlease provide server port num: ");

scanf("%s",portnumber);

getchar();

portno = atoi(portnumber);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)

error("ERROR opening socket");

server = gethostbyname(hostname);

if (server == NULL) {

fprintf(stderr,"ERROR, no such host\n");

exit(0);}
bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);

serv_addr.sin_port = htons(portno);

if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)

error("ERROR connecting");

printf("\nConnected to server 'localhost' at port num '%s'",portnumber);

printf("\nReady for communication ...");

printf("\n");

printf("\nPlease enter the message: ");

bzero(buffer,sizeof(buffer));

fgets(buffer,sizeof(buffer),stdin);

printf("\nWriting message to socket ...");

if (write(sockfd,buffer,sizeof(buffer)) < 0)

error("ERROR writing to socket");

printf("\nMessage sent/written to socket");

printf("\nClient listening for server ACK ...");

printf("\n");

bzero(buffer,256);

if (read(sockfd,buffer,sizeof(buffer)) < 0)

error("ERROR reading from socket");

printf("\nRead successful for server ACK");

printf("\nServer sent this ACK : %s",buffer);

printf("\nClosing the socket ...\n\n");

close(sockfd);

return 0;

}
Code for a simple server

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

void error(const char *msg)

perror(msg);

exit(1);

int main()

int sockfd, newsockfd, portno;


char buffer[256],portnumber[10];

struct sockaddr_in serv_addr, cli_addr;

socklen_t clilen;

/* Ask user to provide a port number */

printf("\nServer requires a port number to operate.");

printf("\nPlease provide a port number: ");

scanf("%s",portnumber);

/* Open socket from server end */

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)

error("ERROR opening socket");

bzero((char *) &serv_addr, sizeof(serv_addr));

portno = atoi(portnumber);

/* Bind socket to the server */

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = INADDR_ANY;

serv_addr.sin_port = htons(portno);

if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)

error("ERROR on binding");

/* Listen to the socket for client connection */

printf("\nServer created\thostname : 'localhost'\n\t\tport num : '%s'",portnumber);

printf("\nCreate a client and send connection request");

printf("\nServer listening for client connection ...");

printf("\n");

listen(sockfd,5);

/* Accept client connection on request */

clilen = sizeof(cli_addr);

newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);


if (newsockfd < 0)

error("ERROR on accept");

bzero(buffer,sizeof(buffer));

/* Listen to the socket for client message */

printf("\nClient connected");

printf("\nServer listening for client message ...");

printf("\n");

/* Read client message from socket */

if (read(newsockfd,buffer,sizeof(buffer)) < 0)

error("ERROR reading from socket");

printf("\nRead successful for client message");

printf("\nClient sent this message : %s",buffer);

/* Send ACK for client message */

if (write(newsockfd,"Thanks, I got your message",sizeof(buffer)) < 0)

error("ERROR writing to socket");

printf("\nACK sent in response to client message");

/* Close the socket and end communication */

printf("\nClosing the socket ...\n\n");

close(newsockfd);

close(sockfd);

return 0;

You might also like