Professional Documents
Culture Documents
Networking 2
Networking 2
Networking 2
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
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
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
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
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)
{
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;
//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;
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
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
{
close(new_socket);
sleep(10);
}
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);
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;
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
___________________________________________________________________________
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));
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;
// 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
//}
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);
}
}
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;
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);
Output
Serverchat.c
Socket created successfully
Socket binded successfully
listen successfully
Waiting for client
Socket connected successfully
msg:
hii
z□□N=-□
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 18
msg:
good morning
msg:
bye
morning
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
--------------------------------
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;
M.Sc. (CS) - I, Computer Science Lab book, Dept. of Computer Science, S. P. College, Pune 20
char buff[MAX];
// 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;
// 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')
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;
// 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;
}
}
Output
Server5.c
Socket created
successfully Socket binded
successfully Listen
successfully
Client Connected
From Client: hii
To Client: hlo
To Client: bye
From Client: hii
To Client: hlo
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
To Server: exit
Exit...
Client5.c (2)
Socket created successfully
Socket connected successfully
To Server: hii
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