You are on page 1of 12

COMPUTER NETWORKS LAB.

AV341

R.SREEDHAR SC10B098 Roll No. 41 6th Sem Avionics

INDEX
No. Date Topic Remarks

LAB 1 TCP CONNECTION

OBJECTIVE:
To Create a simple Echo Server and Client using Linux TCP Socket APIs and to modify the Echo Server with a String Reverse Service

TCP ECHO SERVER CODE :


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define SERV_TCP_PORT 7990 #define MAXLINE 512 char *pname; /* * Example of server using TCP protocol. */ main(argc, argv) int argc; char *argv[]; { int sockfd, newsockfd, clilen, childpid; struct sockaddr_in cli_addr, serv_addr; pname = argv[0]; /* * Open a TCP socket (an Internet stream socket). */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("server: can't open stream socket"); /* * Bind our local address so that the client can send to us.*/ bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("server: can't bind local address"); listen(sockfd, 5); for ( ; ; ) { /* * Wait for a connection from a client process. * This is an example of a concurrent*/

clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) perror("server: accept error"); if ( (childpid = fork()) < 0) perror("server: fork error"); else if (childpid == 0) { /* child process */ close(sockfd); /* close original socket */ str_echo(newsockfd); /* process the request */ exit(0); } close(newsockfd); /* parent process */ } } /* * Read a stream socket one line at a time, and write each line back * to the sender. * * Return when the connection is terminated. */ str_echo(sockfd) int sockfd; { int n; char line[MAXLINE]; char c; int i; for ( ; ; ) { n = recv(sockfd, line, MAXLINE, 0); /*String is received from the client*/ if (n == 0) return; /* connection terminated */ else if (n < 0) perror("str_echo: readline error"); /*This loop will do the string reversal*/ else if (send(sockfd, line, n,0) != n) /*Reversed String is sent back to client*/ perror("str_echo: writen error"); } }

TCP ECHO CLIENT CODE:


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

#define SERV_TCP_PORT 7990 #define SERV_HOST_ADDR "127.0.0.1" #define MAXLINE 512

char *pname; /* * Example of client using TCP protocol. */ main(argc, argv) int argc; char *argv[]; { int sockfd; struct sockaddr_in serv_addr; // char SERVER_IP_ADDR[25]; /* host addr for server:pname = argv[0]; /* * Fill in the structure "serv_addr" with the address of the * server that we want to connect with. */ // strcpy(SERVER_IP_ADDR,"localhost"); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); serv_addr.sin_port = htons(SERV_TCP_PORT); /* * Open a TCP socket (an Internet stream socket). */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("client: can't open stream socket"); /* * Connect to the server. */ if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("client: can't connect to server"); str_cli(stdin, sockfd); /* do it all */ close(sockfd); exit(0); } /* * Read the contents of the FILE *fp, write each line to the * stream socket (to the server process), then read a line back from * the socket and write it to the standard output. * * Return to caller when an EOF is encountered on the input file. */ str_cli(fp, sockfd) register FILE *fp; register int sockfd; { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; printf("Enter the string to be sent to the server\n"); while (fgets(sendline, MAXLINE, fp) != NULL) { n = strlen(sendline); printf("Sending %d bytes to server at %s \n",n,SERV_HOST_ADDR); if (send(sockfd, sendline, n,0) != n) perror("str_cli: writen error on socket"); /* * Now read a line from the socket and write it to * our standard output. */

n = recv(sockfd, recvline, MAXLINE,0); if (n < 0) perror("str_cli: readline error"); recvline[n] = 0; /* null terminate */ printf("Received From Server: "); fputs(recvline, stdout); } if (ferror(fp)) perror("str_cli: error reading file"); }

TCP ECHO SERVER CODE WITH REVERSAL OF THE STRING PROGRAM :


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define SERV_TCP_PORT 7981 #define MAXLINE 512 char *pname; /* * Example of server using TCP protocol. */ main(argc, argv) int argc; char *argv[]; { int sockfd, newsockfd, clilen, childpid; struct sockaddr_in cli_addr, serv_addr; pname = argv[0]; /* * Open a TCP socket (an Internet stream socket). */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("server: can't open stream socket"); /* * Bind our local address so that the client can send to us.*/ bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("server: can't bind local address"); listen(sockfd, 5); for ( ; ; ) { /* * Wait for a connection from a client process. * This is an example of a concurrent*/ clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0)

perror("server: accept error"); if ( (childpid = fork()) < 0) perror("server: fork error"); else if (childpid == 0) { /* child process */ close(sockfd); /* close original socket */ str_echo(newsockfd); /* process the request */ exit(0); } close(newsockfd); /* parent process */ } } /* * Read a stream socket one line at a time, and write each line back * to the sender. * * Return when the connection is terminated. */ str_echo(sockfd) int sockfd; { int n; char line[MAXLINE]; char c; int i; for ( ; ; ) { n = recv(sockfd, line, MAXLINE, 0); /*String is received from the client*/ if (n == 0) return; /* connection terminated */ else if (n < 0) perror("str_echo: readline error"); for (i=0;i<(n/2);i++) { c= line[i]; line[i]=line[n-i-1]; line[n-i-1]=c; } /*This loop will do the string reversal*/ if (send(sockfd, line, n,0) != n) /*Reversed String is sent back to client*/ perror("str_echo: writen error"); } }

COMMANDS USED IN TERMINAL:


SERVER: Ifconfig // get the ip address of server Gedit sree.c //opens up the file with name sree.c Copy and paste the server code in above file. gcc sree.c //gcc compiler compliles the code in file ./a.out // runs the complied code

CLIENT:
Gedit client.c //opens up the file with name client.c Copy and paste the client code in above file and edit the host address with ip address of server. gcc client.c //gcc compiler compliles the code in file Ping 172.20.111.67 //checks the connection status with the server 127.0.0.1 //is used when both server and client are on the same system. ./a.out // runs the complied code

OUTPUTS: SERVER:

CLIENT:

This is a handshake process. Connection is setup between client and server. So whatever message sent from client is processed in server and sent back to client.

SERVER WITH REVERSAL PROGRAM:

Address already in use: if the terminal is already open for the same program with same port number, then we get this error. It can get overcome by changing the port number. CLIENT:

The string sent by client is processed in the server and the reversal of the string is sent back to the client.

TCP connection flow


The following sequence shows the flow of a TCP connection: 1. The server creates the listener socket that is waiting for remote clients to connect. 2. The client issues the connect( ) socket function to start the TCP handshake (SYN, SYN/ACK, ACK). The server issues the accept( ) socket function to accept the connection request. 3. The client and server issue the read( ) and write( ) socket functions to exchange data over the socket. Note: There are several SSL APIs that you can use to send and receive data other than the read() and write() socket functions. 4. Either the server or the client decides to close the socket. This causes the TCP closure sequence (FINs and ACKs) to occur. 5. The server either closes the listener socket or repeats beginning with step 2 to accept another connection from a remote client. Note: Normally after the accept() socket function ends, the server divides into two processes (or threads). The first process handles the connection with the client and the second process issues the next accept() socket function.

INFERENCE: For user to interact with network ,API(Application program interface) is needed sometimes called as socket APIs.We need to have sequence of tasks to establish a connection between client and server.Here connection-oriented data transfer is focused (TCP).In TCP streams of data is sent (byte stream).Echo server means the data client sends to server is reflected back to client on terminal (with or without reversal).We use following functions to open and set up TCP connection :

SOCKET APIS: Int socket (int domain, int type, int protocol) included both in client & server domain specifies a communication family selects protocol family type is type of communication,end to end semantics eg: SOCK-STREAM, SOCK-DGRAM, SOCK-RAM, SOCK-RDM. protocol defines protocol to be used in communication

Int bind (int socket-id, struct sock addr, int addr-less) Unique to server bind ( ) socket call binds or attaches new socket with local address addr. socket id is socket identifier/file descriptor struct sock addr is a structure contain address

int listen( int socket-fd,int backing) unique to server side sockets listen( ) API specifies in coming connection & a queue limit in incoming connection on a newly created socket int socket-fd specifies socket int backing define max length of queue of pending connection.

int accept ( int socket-fd, structure sockaddr,sock len-taddrlen) used with connection based socket it extracts a first connection and creates a new socket it returns file descriptive of new socket new socket is not in listening state int struct-fd is original socket struct sockaddr is a pointer to sock addr structure this structure is filled with address of peer socket that is accepted to communication sessions

int connect(int socket-fd, construct sock addr, serv addrs) requests connection using referred socket by file descriptor to address specified by serv-addr send( ), send to ( ) , send msg => transmit a message to another socket

recv( ) , recvfrom( ), recv msg( ) =>it can be blocking but is normally used as a connected socket with known remake addr int shutdown (int socket-fd,int how) => how determines communication after shutdown

CONCLUSION: Initially connection between server and client was established using the ip
address of the server. Whatever messages send from the client to server has been received back by the client again. Program for string reversal in C code is included in the server code. When server receives a string from the client, the string gets reversed as per the C code and the reversed string is sent back to client.

You might also like