TCP IP Socket Programming-Computer Networks and Programming-Lab Task, Exercises of Computer Networks

This lab report was submitted to Sir Rahul Das at Birla Institute of Technology and Science as lab assignment for Networking course. Its main points are: Markov, Process, Stochastic, FSM, Simple, Example, Weather, Raining

Typology: Exercises

2011/2012

Uploaded on 07/17/2012

padmalini
padmalini 🇮🇳

4.5

(14)

80 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
TCP/IP SOCKET PROGRAMMING IN LINUX SHELL
LAB TASK 1
Server program should be able to receive and store a text file in its local directory.
TO DO:
Server should be able to receive a file from client and store it in its local directory by the name specified by client.
PROCEDURE:
To implement the given task, following changes were made to the server and client.
Client was able to :
1) Open a file from local directory.
2) Read content from the local directory
3) Send the read content through socket interface
Server was able to :
1) Create and open a dummy file
2) Read and save data from socket to the dummy file
3) Rename and save the dummy file to a local directory
RESULTS:
SERVER
[se1032@localhost G4]$ ./server
The Server passive socket port number is 51864
Ready for a connection...
Got a connection - processing...
The active server port number is 51864
The active server IP ADDRESS is 127.0.0.1
The client port number is 54289
The client IP ADDRESS is 127.0.0.1
name is : pc.txt
command used to save file : cp welc.txt
./servdir/pc.txt
the file is saved in the path : /servdir/
Done with connection - closing
CLIENT
[se1032@localhost G4]$ ./client 127.0.0.1 51864
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download TCP IP Socket Programming-Computer Networks and Programming-Lab Task and more Exercises Computer Networks in PDF only on Docsity!

TCP/IP SOCKET PROGRAMMING IN LINUX SHELL

LAB TASK 1

Server program should be able to receive and store a text file in its local directory.

TO DO:

Server should be able to receive a file from client and store it in its local directory by the name specified by client.

PROCEDURE:

To implement the given task, following changes were made to the server and client.

Client was able to :

1) Open a file from local directory.

2) Read content from the local directory

3) Send the read content through socket interface

Server was able to :

1) Create and open a dummy file

2) Read and save data from socket to the dummy file

3) Rename and save the dummy file to a local directory

RESULTS:

SERVER

[se1032@localhost G4]$ ./server

The Server passive socket port number is 51864

Ready for a connection...

Got a connection - processing...

The active server port number is 51864

The active server IP ADDRESS is 127.0.0.

The client port number is 54289

The client IP ADDRESS is 127.0.0.

name is : pc.txt

command used to save file : cp welc.txt

./servdir/pc.txt

the file is saved in the path : /servdir/

Done with connection - closing

CLIENT

[se1032@localhost G4]$ ./client 127.0.0.1 51864

CODE:

Following is the code appended in the source files of source and client program.

CLIENT.C

// define a file pointer to open and read the file FILE *pc; int n; // define the name of file char *name="pc.txt"; // create buffer for sending data through socket char buffer[100]; . . . // open the file “pc.txt” pc=fopen("pc.txt","r"); // send file name write(sk,name,strlen(name)); // send the content of file do { n=fread(buffer,1,100,pc); write(sk,buffer,n); }while(n>0); // close the opened file fclose(pc);

SERVER.C

// create string to store filename char *name; char buff[100]; // create string to store command char *command; // allocate space for strings to avoid stackoverflow error command = (char *)malloc(100); name = (char *)malloc(7);

int n;

// create File pointer to open a file FILE *ptr; . . . // initialize command and name string as null for(i = 0;i < strlen(command);i=i+1){ command[i] = 0; } for(i=0;i<strlen(name);i=i+1){ name[i] = 0; } // read the name specified by client n=read(sd,name,6); printf("\r\n name is : %s",name); // create a command "cp welc.txt ./servdir/<filename.extension>" command = strncat(command,"cp welc.txt ./servdir/",strlen("cp welc.txt ./servdir/")); // printf("\r\n%s",command); command = strncat(command,name,6); printf("\r\n command used to save file : %s",command); // open a dummy file "welc.txt" ptr=fopen("welc.txt","w"); // write received data in welc.txt do { n=read(sd,buff,100); fwrite(buff,1,n,ptr); }while(n>0);

fclose(ptr); // copy the dummy file to the specified local folder with client desired name system(command); // free allocated memory free(command); free(name); // authenticate file saving process printf("\r\nthe file is saved in the path : /servdir/%s",name);

LAB TASK 3

Server program should be able to log its activity.

TO DO:

Server should save any action ,performed by, in a log file.

PROCEDURE:

Server was able to :

1) Create a .log file and save any data printed on screen or send to server through socket interface.

CODE:

Following changes were made in the server source file:

#include <stdio.h> /* standard C i/o facilities / #include <unistd.h> / Unix System Calls / #include <stdlib.h> / standard library / #include <sys/types.h> / system data type definitions / #include <sys/socket.h> / socket specific definitions / #include <netinet/in.h> / INET constants and stuff / #include <arpa/inet.h> / IP address conversion stuff */ #include <string.h> #include <malloc.h>

/* Server main routine - this is an iterative server

  1. create a socket
  2. bind the socket and print out the port number assigned
  3. put the socket into passive mode (listen)
  4. do forever get next connection handle the connection enddo */

int main() { int ld,sd; struct sockaddr_in skaddr; struct sockaddr_in from;

int addrlen,length; char buff[1024]; char pdata[1024]; int n,m; FILE *ptr; FILE *dptr; ptr=fopen("./servdir/servdata.log","a");

fprintf(ptr,"\r\nNew Log session\r\n"); /* create a socket IP protocol family (PF_INET) TCP protocol (SOCK_STREAM) */

if ((ld = socket( PF_INET, SOCK_STREAM, 0 )) < 0) { perror("Problem creating socket\n"); exit(1);

/* establish our address address family is AF_INET our IP address is INADDR_ANY (any of our IP addresses) the port number is assigned by the kernel */

skaddr.sin_family = AF_INET; skaddr.sin_addr.s_addr = htonl(INADDR_ANY); skaddr.sin_port = htons(0);

if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) { perror("Problem binding\n"); exit(0); }

/* find out what port we were assigned and print it out */

length = sizeof( skaddr ); if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) { perror("Error getsockname\n"); exit(1); } printf("The Server passive socket port number is %d\n",ntohs(skaddr.sin_port));

/* put the socket into passive mode (waiting for connections) */

if (listen(ld,5) < 0 ) { perror("Error calling listen\n"); fprintf(ptr,"Error calling listen\n"); exit(1); }

/* now process incoming connections forever ... / fclose(ptr); while (1) { ptr=fopen("./servdir/servdata.log","a"); fprintf(ptr,"Ready for a connection...\n"); addrlen=sizeof(skaddr); if ( (sd = accept( ld, (struct sockaddr) &from, &addrlen)) < 0) { perror("Problem with accept call\n"); fprintf(ptr,"Problem with accept call\n");

exit(1); }

fprintf(ptr,"Got a connection - processing...\n");

/* Determine and print out the address of the new server socket */

length = sizeof( skaddr ); if (getsockname(sd, (struct sockaddr ) &skaddr, &length)<0) { perror("Error getsockname\n"); fprintf(ptr,"Error getsockname\n"); exit(1); } fprintf(ptr,"The active server port number is %d\n",ntohs(skaddr.sin_port)); fprintf(ptr,"The active server IP ADDRESS is %s\n",inet_ntoa(skaddr.sin_addr)); / print out the address of the client */

fprintf(ptr,"The client port number is %d\n",ntohs(from.sin_port)); fprintf(ptr,"The client IP ADDRESS is %s\n",inet_ntoa(from.sin_addr));

/* read and send to stdout until the client closes the connection*/ do { m = read(sd,pdata,1024); if(m >0){ //printf("\r\nSize : Read from socket :%d :%s",m,pdata); system("date > data.log"); dptr = fopen("data.log","r"); n = fread(buff,1,1024,dptr); //printf("Size : Current Date : %d : %s",n,buff); fwrite(buff,1,n,ptr); fclose(dptr); } fwrite(pdata,1,m,ptr); fwrite("\r\n",1,strlen("\r\n"),ptr); }while(m>0);

fprintf(ptr,"\r\nDone with connection - closing\n\n\n"); fclose(ptr); close(sd); } }

RESULTS:

CLIENT

[se1032@localhost G4]$ ./client 127.0.0.1 34800 hassan [se1032@localhost G4]$ ./client 127.0.0.1 34800 saleem [se1032@localhost G4]$ ./client 127.0.0.1 34800 sammer^H [se1032@localhost G4]$ ./client 127.0.0.1 34800 farooq [se1032@localhost G4]$ ./client 127.0.0.1 34800 shahzad [se1032@localhost G4]$ ./client 127.0.0.1 34800 afzal [se1032@localhost G4]$ ./client 127.0.0.1 34800 zaeem [se1032@localhost G4]$ ./client 127.0.0.1 34800 kumail [se1032@localhost G4]$ ./client 127.0.0.1 34800 hassan_shahifd [se1032@localhost G4]$

LAB TASK 4

Server should display Waiting for a client, current time is hh:mm:ss, dd/mm/yyyy" after every five minutes.

PROCEDURE:

To accomplish this task, following changes were made to the source file:

Source was able to :

1) Create a new concurrent process that displays message after 5 minutes

2) Run the routine server task in separate process

Fork command was used to create a child process that handles the delay and display the message “Waiting for

Client”

For the transfer of data between the two processes , a file was created “connection.txt” that save the variable data

required by child process for evaluation.

CODE :

Following changes were made to the source file:

#include <stdio.h> /* standard C i/o facilities */

#include <unistd.h> /* Unix System Calls */

#include <stdlib.h> /* standard library */

#include <sys/types.h> /* system data type

definitions */

#include <sys/socket.h> /* socket specific definitions */

#include <netinet/in.h> /* INET constants and stuff */

#include <arpa/inet.h> /* IP address conversion stuff

#include <string.h>

#include <malloc.h>

/* Server main routine - this is an iterative server

1. create a socket

2. bind the socket and print out the port number assigned

3. put the socket into passive mode (listen)

4. do forever

get next connection

handle the connection

enddo

int main() {

int ld,sd;

struct sockaddr_in skaddr;

struct sockaddr_in from;

char connection;

int addrlen,length;

char buff[1024];

char pdata[1024];

int n,m;

FILE *ptr;

FILE *dptr;

FILE *conn;

connection = '0';

conn = fopen("conenction.txt","w");

fwrite(&connection,1,1,conn);

fclose(conn);

/* create a socket

IP protocol family (PF_INET)

TCP protocol (SOCK_STREAM)

if ((ld = socket( PF_INET, SOCK_STREAM, 0 )) < 0) {

perror("Problem creating socket\n");

exit(1);

/* establish our address

address family is AF_INET

our IP address is INADDR_ANY (any of our IP

addresses)

the port number is assigned by the kernel

skaddr.sin_family = AF_INET;

skaddr.sin_addr.s_addr = htonl(INADDR_ANY);

skaddr.sin_port = htons(0);

if (bind(ld, (struct sockaddr *) &skaddr,

sizeof(skaddr))<0) {

perror("Problem binding\n");

exit(0);

/* find out what port we were assigned and print it out */

length = sizeof( skaddr );

if (getsockname(ld, (struct sockaddr *) &skaddr,

&length)<0) {

perror("Error getsockname\n");

exit(1);

printf("The Server passive socket port number is

%d\n",ntohs(skaddr.sin_port));

/* put the socket into passive mode (waiting for

connections) */

if (listen(ld,5) < 0 ) {

perror("Error calling listen\n");

exit(1);

/* now process incoming connections forever ... */

pid_t PID = fork();

// Child Process

if (PID == 0){

/* Child*/

while(1){

conn = fopen("connection.txt","r");

n = fread(&connection,1,1,conn);

fclose(conn);

/* printf("connection =

%c\r\n",connection);*/

if(connection == '0'){

system("date +""%r,%e/%m/%Y""

printf("Waiting for client...\r\n");

system("sleep 5s");

// Parent Process

else{

while (1) {

printf("Ready for a connection...\n");

addrlen=sizeof(skaddr);

if ( (sd = accept( ld, (struct sockaddr*) &from,

&addrlen)) < 0) {

perror("Problem with accept call\n");

exit(1);

connection = '1';

conn = fopen("connection.txt","w");

fwrite(&connection,1,1,conn);

fclose(conn);

printf("Got a connection - processing...\n");

/* Determine and print out the address of the new

server socket */

length = sizeof( skaddr );

if (getsockname(sd, (struct sockaddr *) &skaddr,

&length)<0) {

perror("Error getsockname\n");

exit(1);

printf("The active server port number is

%d\n",ntohs(skaddr.sin_port));

printf("The active server IP ADDRESS is

%s\n",inet_ntoa(skaddr.sin_addr));

/* print out the address of the client */

printf("The client port number is

%d\n",ntohs(from.sin_port));

printf("The client IP ADDRESS is

%s\n",inet_ntoa(from.sin_addr));

/* read and send to stdout until the client closes the

connection*/

ptr=fopen("./servdir/servdata.log","a");

do {

m = read(sd,pdata,1024);

if(m >0){

printf("\r\nSize : Read from socket :%d

:%s",m,pdata);

system("date > data.log");

dptr = fopen("data.log","r");

n = fread(buff,1,1024,dptr);

printf("Size : Current Date : %d : %s",n,buff);

fwrite(buff,1,n,ptr);

fclose(dptr);

fwrite(pdata,1,m,ptr);

fwrite("\r\n",1,strlen("\r\n"),ptr);

}while(m>0);

fclose(ptr);

printf("\r\nDone with connection - closing\n\n\n");

conn = fopen("connection.txt","w");

connection = '0';

fwrite(&connection,1,1,conn);

fclose(conn);

close(sd);