Networking 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Shikshana Prasaraka Mandali’s

SIR PARASHURAMBHAU COLLEGE


(Autonomous)
Department of Computer Science
Lab-Book
M. Sc. - I
(Computer Science)
Lab Course – MCS12814
(From Academic year 2020-21)

Name: Aditya Chavande


College Name: Sir Parshurambhau College ,Pune Roll No. :
12054 Division:
Academic Year: 2020-2021

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 1
Shikshana Prasaraka Mandali’s
SIR PARASHURAMBHAU COLLEGE
(Autonomous)
TILAK ROAD, PUNE – 411 030

Department of Computer Science


Subject - Computer Science

Certificate
This is to certify that Aditya Chavande of M.Sc. (Computer
Science) – I has completed ____ practicals out of ____ in
the subject Computer Science Semester – I/II during the
academic year 20 - 20

Teacher In-Charge Head Date: Department of Computer


Science

Examiner 1 Examiner 2
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 2
Assignment Completion Sheet
Assignments based on Principles of Programming Language

Sr. Assignment Name Start Date End Date Marks


No.

1 Network Programming Assignment

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 3
Assignments based on Advanced Networking and Network Programming

Sr. Assignment Name Start Date End Date Marks


No.

Date: / /

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 4
Assignment No.:_2
Assignment Name: network programming

Que 1. write a simple server client program in c . accept user name at client side ,send it
to server side and display greet msg to user at server side.

Servgreet.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
int main()
{
int socket_fd;
int retVal;
struct sockaddr_in serverAddr;
int socklen = sizeof(struct sockaddr_in);
int new_socket;
char buff[1024];
// create a socket for endpoint communication
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= INADDR_ANY;
serverAddr.sin_port = 54584;
retVal = bind(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t)socklen);
if(retVal == -1)
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 5
{
// Error
printf("\n Not able to bind");
exit(0);
}
else
printf("\n Socket binded successfully");
// listen
retVal = listen(socket_fd, 3);
if(retVal == -1)
{
// Error
printf("\n Not able to listen");
exit(0);
}
else
printf("\n listen successfully");

while(1)
{
printf("\n Waiting for client");
new_socket = accept(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t
*)&socklen); if(new_socket < 0)
{

printf("\n not able to accept client request");


}
else
printf("\n client is connected");
recv(new_socket,buff,1024,0);
printf("\nwelcome %s",buff);
close(new_socket);
sleep(10);
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 6
}

// close the socket


close(socket_fd);
return 0;
}

Clientgreet.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>

int main()
{
int socket_fd;
int retVal;

struct sockaddr_in clientAddr;


int socklen = sizeof(struct sockaddr_in);
char *name;
// char *str =" " ;
printf("enter your name");
scanf("%s",name);
//str=&name;

//printf("%s",str);
// create a socket for endpoint communication
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{
// Error

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 7
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");

// Connect to a server
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr= INADDR_ANY;
clientAddr.sin_port = 54584;

retVal = connect(socket_fd,(struct sockaddr*)&clientAddr, (socklen_t)socklen);


if(retVal == -1)
{
// Error
printf("\n Not able to connect");
exit(0);
}
else
printf("\n Socket connected successfully");

// Send request / data to the server


send(socket_fd,name, strlen(name), 0);

// close the socket


//close(socket_fd);
return 0;
}
Output
Servgreet.c
Socket created successfully
Socket binded successfully
listen successfully

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 8
Waiting for client
client is connected
welcome kiran
clientgreet.c

enter your name kiran

Socket created successfully


Socket connected successfully
_____________________________________________________________________________

Que2. write a simple server client program in c display server date and time at client side.
Serverdatetime.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
#include<time.h>
#include<string.h>
int main()
{
int socket_fd;
int retVal;
time_t tm;
time(&tm);
char *str=ctime(&tm);
printf(str);
struct sockaddr_in serverAddr;
int socklen = sizeof(struct sockaddr_in);
int new_socket;
// create a socket for endpoint communication
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 9
{
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= INADDR_ANY;
serverAddr.sin_port = 54584;
retVal = bind(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t)socklen);
if(retVal == -1)
{
// Error
printf("\n Not able to bind");
exit(0);
}
else
printf("\n Socket binded successfully");
// listen
retVal = listen(socket_fd, 3);
if(retVal == -1)
{
// Error
printf("\n Not able to listen");
exit(0);
}
else
printf("\n listen successfully");
while(1)
{
//printf("\n Waiting for client");
new_socket = accept(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t
*)&socklen);
if(new_socket < 0)
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 10
{

printf("\n not able to accept client request");


}
else
printf("\n client is connected");
send(new_socket,str, strlen(str), 0);

close(new_socket);
sleep(10);
}

// close the socket


close(socket_fd);
return 0;
}

Clientdatetime.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
int main()
{
int socket_fd;
int retVal,n;
char buff[1024];
struct sockaddr_in clientAddr;
int socklen = sizeof(struct sockaddr_in);

// create a socket for endpoint communication


socket_fd = socket(AF_INET, SOCK_STREAM, 0);

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 11
if(socket_fd < 0)
{
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");

// Connect to a server
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr= INADDR_ANY;
clientAddr.sin_port = 54584;

retVal = connect(socket_fd,(struct sockaddr*)&clientAddr, (socklen_t)socklen);


if(retVal == -1)
{
// Error
printf("\n Not able to connect");
exit(0);
}
else
printf("\n Socket connected successfully");
// Send request / data to the server
recv(socket_fd,buff,1024,0);
printf("\n date and time %s",buff);
// close the socket
//close(socket_fd);
return 0;
}
Output
Serverdatetime.c
Fri Aug 13 14:42:38 2021
Socket created successfully

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 12
Socket binded successfully
listen successfully
clientdatetime.c
Socket created successfully
Socket connected successfully
date and time Fri Aug 13 14:42:38 2021
___________________________________________________________________________

Que3. Write a simple server client chat program in c


Serverchat.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
#include<string.h>

int main()
{
int socket_fd;
int retVal;
int read;
struct sockaddr_in serverAddr,clientAddr;
int socklen = sizeof(struct sockaddr_in);
int new_socket;

char buff[1024];
memset(&serverAddr,0,sizeof(serverAddr));
memset(&clientAddr,0,sizeof(clientAddr));

// create a socket for endpoint communication


socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 13
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");

serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= INADDR_ANY;
serverAddr.sin_port = 54584;

retVal = bind(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t)socklen);


if(retVal == -1)
{
// Error
printf("\n Not able to bind");
exit(0);
}
else
printf("\n Socket binded successfully");

// listen
retVal = listen(socket_fd, 3);
if(retVal == -1)
{
// Error
printf("\n Not able to listen");
exit(0);
}
else
{
printf("\n listen successfully");
}

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 14

printf("\n Waiting for client");


// while(1)
// {

new_socket = accept(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t


*)&socklen); if(new_socket < 0)
{

printf("\n not able to accept client request");


}
else
{
printf("\n Socket connected successfully");
}

//}
while(1)
{
read=recv(new_socket,buff,1024,0);
if(read==-1)
{
printf("txt not recieved\n");
exit(0);
}
printf("\nmsg:\n%s\n",buff);
printf("\nenter msg\t\t\t");
fgets(buff,1024,stdin);
if(strncmp(buff,"end",3)==0)
break;

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 15
read=send(new_socket,buff, strlen(buff), 0);
if(read==-1)
{
printf("sending error\n");
exit(0);
}
}

// close the socket


close(socket_fd);
return 0;
}

Clientchat.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
int main()
{
int socket_fd;
int retVal,n;
struct sockaddr_in clientAddr;
memset(&clientAddr,0,sizeof(clientAddr));
int socklen = sizeof(struct sockaddr_in);
char buff[1024];
// create a socket for endpoint communication
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{
// Error
printf("\n Not able to create socket");

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 16
exit(0);
}
else
printf("\n Socket created successfully");

// Connect to a server
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr= INADDR_ANY;
clientAddr.sin_port = 54584;

retVal = connect(socket_fd,(struct sockaddr*)&clientAddr, (socklen_t)socklen);


if(retVal == -1)
{
// Error
printf("\n Not able to connect");
exit(0);
}

else
printf("\n Socket connected successfully");

while(1)
{
//bzero(buff,0);
printf("\n enter the massage\n");
fgets(buff,1024,stdin);
if(strncmp(buff,"end",3)==0)
break;
n=send(socket_fd,buff, strlen(buff), 0);
if(n==-1)
{
printf("text not send! something went wrong!!"); exit(0);
}

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 17
n=recv(socket_fd,buff,1024,0);
if(n==-1)
{
printf("text not received! something went wrong!!"); exit(0);
}
printf("\n%s\n",buff);

}
close(socket_fd);
exit(0);

// Send request / data to the server

// close the socket


//close(socket_fd);
return 0;
}

Output
Serverchat.c
Socket created successfully
Socket binded successfully
listen successfully
Waiting for client
Socket connected successfully
msg:
hii
z□□N=-□

enter msg hlo

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 18
msg:
good morning

enter msg good morning

msg:
bye
morning

enter msg bye

msg:
bye

enter msg

clientchat.c
Socket created successfully
Socket connected successfully
enter the massage
hii

hlo
enter the massage
good morning

good morning

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 19
enter the massage
bye

bye

enter the massage


end

--------------------------------
Process exited after 73.52 seconds with return value 0
Press any key to continue . . .
_____________________________________________________________________________

Que4. write a client server chat program in c using fork() system call.
Server5.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
# define MAX 1024
int main()
{
int socket_fd;
int retVal,n;
pid_t pid;

struct sockaddr_in serverAddr;


int socklen = sizeof(struct sockaddr_in);
int new_socket;

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 20
char buff[MAX];

// create a socket for endpoint communication


socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");

// bind a socket
// int bind(int socket, const struct sockaddr *address, socklen_t address_len)

serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= INADDR_ANY;
serverAddr.sin_port = 54584;

retVal = bind(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t)socklen);


if(retVal == -1)
{
// Error
printf("\n Not able to bind");
exit(0);
}
else
printf("\n Socket binded successfully");

// listen
retVal = listen(socket_fd, 3);

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 21
if(retVal == -1)
{
// Error
printf("\n Not able to listen");
exit(0);
}
else
printf("\n Listen successfully");

while(1)
{
bzero(buff, sizeof(buff));
//printf("\n Waiting for client");
new_socket = accept(socket_fd,(struct sockaddr*)&serverAddr, (socklen_t
*)&socklen); //printf("\n new_socket : %d ", new_socket);

if(new_socket < 0)
{
// Error
printf("\n Not able to accept client request");
}
else
printf("\n Client Connected");
if( (pid = fork()) == 0)
{
// close the socket
close(socket_fd);

while (1)
{
// read data from client
recv(new_socket, buff, sizeof(buff), 0);
printf("\n From Client: %s",buff);

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 22
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0)
{
printf("Exit...\n");
break;
}
bzero(buff, sizeof(buff));
printf("\n\t To Client: ");
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')

// Send data to the client


send(new_socket, buff, sizeof(buff), 0);
if (strncmp("exit", buff, 4) == 0)
{
printf("Exit...\n");
break;
}
}
}
// close the socket
close(new_socket);
//sleep(10);
}

// close the socket


close(socket_fd);
return 0;
}

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 23
Client5.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include<sys/unistd.h>
# define MAX 1024

int main()
{
int socket_fd;
int retVal,n;

struct sockaddr_in clientAddr;


int socklen = sizeof(struct

sockaddr_in); char buff[MAX];

// create a socket for endpoint communication


socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0)
{
// Error
printf("\n Not able to create socket");
exit(0);
}
else
printf("\n Socket created successfully");

// Connect to a server
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr= INADDR_ANY;
clientAddr.sin_port = 54584;

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 24
retVal = connect(socket_fd,(struct sockaddr*)&clientAddr, (socklen_t)socklen);
if(retVal == -1)
{
// Error
printf("\n Not able to connect");
exit(0);
}
else
printf("\n Socket connected successfully");

while (1)
{
bzero(buff, sizeof(buff));
printf("\n\t To Server: ");
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// Send data to the server
send(socket_fd, buff, sizeof(buff), 0);
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0)
{
printf("Exit...\n");
break;
}

bzero(buff, sizeof(buff));
// read data from client
recv(socket_fd, buff, sizeof(buff), 0);
printf("\n From Server: %s",buff);
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0)

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 25
{
printf("Exit...\n");
break;
}
}

// close the socket


close(socket_fd);
return 0;
}

Output
Server5.c
Socket created
successfully Socket binded
successfully Listen
successfully
Client Connected
From Client: hii
To Client: hlo

From Client: good night

To Client: bye
From Client: hii

To Client: hlo

From Client: good

night To Client: bye

Client5.c (1)
Socket created successfully

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 26
Socket connected successfully
To Server: hii

From Server: hlo

To Server: good night

From Server: bye

To Server: exit
Exit...
Client5.c (2)
Socket created successfully
Socket connected successfully
To Server: hii

From Server: hlo


To Server: good night

From Server: bye

To Server: exit
Exit...
--------------------------------
Process exited after 46 seconds with return value 0
Press any key to continue . . .
_____________________________________________________________________________

M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 27

Signature of the instructor Date: ____/____/_______ Assignment Evaluation

0: Not done 4: Complete 2: Late Complete

1: Incomplete 3: Needs improvement 5: Well Done


M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 28

You might also like