Practical Lab File Based ON Network Programming: Shiv Kumar Chaudhary Kurmi Mr. Vivek Soni
Practical Lab File Based ON Network Programming: Shiv Kumar Chaudhary Kurmi Mr. Vivek Soni
ON
NETWORK PROGRAMMING
PRACTICAL WORK
Submitted By
Shiv Kumar Chaudhary Kurmi
Under the guidance of
Mr. Vivek Soni
In partial fulfillment for the award of the degree
Of
B.TECH IN COMPUTER SCIENCE
KALINGA UNIVERSITY
Village Kotni, Near Mantralaya Atal Nagar Raipur (C.G)
Session July – Dec -2020
1. Write an echo program with client and iterative server using TCP.
An iterative server handles both the connection request and the transaction involved in
the call itself. Iterative servers are fairly simple and are suitable for transactions that do
not last long.
Output - server.java
$ java server.java
Server Started and listening to the port 25000 Message received from client is
Saurav
Message sent to the client is Hello Saurav !!
Output - client.java
$ java client.java
Message sent to the server : Saurav
2. Write an echo program with client and concurrent server using TCP.
A concurrent server -
}
}
Output - server.java
$ java server.java message =
hello from client1 message =
hello from client2 message
= bye message
= bye
Output - client1
$ java client.java
hello from client1 bye
Output - client
$ java client.java
hello from client2 bye
3. Write an echo program with client and concurrent server using UDP.
Output - server.java
$ sudo java server.java
Received from: /127.0.0.1:52391
Received from: /127.0.0.1:46293
Output - client1
$ java client.java hello
from client1
Output - client2
$ java client.java hello
from client2
readThread.setPriority(Thread.MAX_PRIORITY);
readThread.start();
}
public void createWriteThread(){
Thread writeThread = new Thread(){
public void run() {
while(socket.isConnected()){
try{
BufferedReader inputReader = new
BufferedReader(new
InputStreamReader(System.in));
sleep(100);
String typedMessage = inputReader.readLine();
if (typedMessage != null && typedMessage.length() > 0){
synchronized (socket){
outStream.write(typedMessage.getBytes("UTF-8"));
sleep(100);
}
};
}
catch (IOException i){
i.printStackTrace();
}
catch(InterruptedException ie){ ie.printStackTrace();
}
}
}
};
writeThread.setPriority(Thread.MAX_PRIORITY);
writeThread.start();
}
public static void main(String[] args){
ChatSocketServer chatServer = new ChatSocketServer();
chatServer.createSocket();
}
}
outStream.write(typedMessage.getBytes("UTF-8")); sleep(100);
}
};
}
catch(IOException i){
i.printStackTrace();
} catch(InterruptedException
ie){ ie.printStackTrace(); }
}
} };
writeThread.setPriority(Thread.MAX_PRIORITY);
writeThread.start();
} public static void main(String[] args)throws
Exception{
ChatSocketClient myChatClient = new ChatSocketClient();
myChatClient.createSocket(); }
}
Output - server.java
$ java server.java
Connected Client
:hi hello Client :how are you? i'm
fine. what about you? Client :i’m
also fine
Output - client.java
$ java client.java
Connected hi Server
:hello how are you?
Server :I'm fine. what about you? i’m
also fine
5. Write a program to retrieve date and time using TCP.
/** Tcp day client **/ import java.net.*; import java.io.*; public
class Tcpdayclient { public static void main(String arg[])throws
IOException{ try{
Socket s=new Socket("localhost",6432);
DataInputStream dis=new
DataInputStream(s.getInputStream()); String msg;
msg= dis.readUTF();
System.out.println(msg);
dis.close(); s.close(); } catch(Exception
e){
System.out.println(e); }
}
}
Output - server.java
$ java server.java
Output - client.java
$ java client.java
Thu Oct 08 07:08:02 UTC 2020
socket.close ();
byte[] data = packet.getData (); int length
= packet.getLength ();
System.out.println (new String (data)); }
}
Output - server.java
$ sudo java server.java
Received from: /127.0.0.1:36208
Output - client.java
$ java client.java
Thu Oct 08 07:20:13 UTC 2020
Output - server.java
$ java server.java i'm a server and i'm waiting for new connection
and buffer select... Connection Accepted: /127.0.0.1:1111
i'm a server and i'm waiting for new connection and buffer select... Message
received: Facebook
i'm a server and i'm waiting for new connection and buffer select... Message
received: Twitter
i'm a server and i'm waiting for new connection and buffer select... Message
received: IBM
i'm a server and i'm waiting for new connection and buffer select... Message
received: Google
i'm a server and i'm waiting for new connection and buffer select... Message
received: Crunchify
Output - client.java
$ java client.java
Connecting to Server on port 1111...
sending: Facebook sending: Twitter
sending: IBM sending: Google
sending: Crunchify
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<unistd.h>
#define MAXLINE 20 #define SERV_PORT 7134 main(int
argc,char **argv){ int
i,j,maxi,maxfd,listenfd,connfd,sockfd;
int nread,client[FD_SETSIZE];
ssize_t n; fd_set rset,allset;
char line[MAXLINE];
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,1); maxfd=listenfd; maxi=-1;
for(i=0;i<FD_SETSIZE;i++) client[i]=-1; FD_ZERO(&allset);
FD_SET(listenfd,&allset); for(; ;){ rset=allset;
nread=select(maxfd+1,&rset,NULL,NULL,NULL);
if(FD_ISSET(listenfd,&rset)){ clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct
sockaddr*)&cliaddr,&clilen); for(i=0;i<FD_SETSIZE;i++)
if(client[i]<0) { client[i]=connfd;
break; }
if(i==FD_SETSIZE){ printf("Too
many clients"); exit(0);
}
FD_SET(connfd,&allset); if(connfd>maxfd)
maxfd=connfd; if(i>maxi) maxi=i; if(--
nread<=0) continue;
} for(i=0;i<=maxi;i++)
{ if((sockfd=client[i])<0)
continue; if(FD_ISSET(sockfd,&rset))
{ if((n=read(sockfd,line,MAXLINE))==0){
close(sockfd);
FD_CLR(sockfd,&allset); client[i]=-1;
} else{ printf("Line recieved from the client
:%s\n",line);
for(j=0;line[j]!='\0';j++)
line[j]=toupper(line[j]);
write(sockfd,line,MAXLINE);
} if(--nread<=0)
break;
}
}
}
}
#include<netinet/in.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<unistd.h>
#define MAXLINE 20 #define
SERV_PORT 7134 main(int
argc,char **argv){ int
maxfdp1; fd_set rset;
char sendline[MAXLINE],recvline[MAXLINE]; int sockfd;
struct sockaddr_in servaddr; if(argc!=2)
{ printf("usage tcpcli <ipaddress>");
return;
} sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("\n Enter data to be send : ");
while(fgets(sendline,MAXLINE,stdin)!=NULL){
write(sockfd,sendline,MAXLINE);
printf("\n Line send to server is : %s",sendline);
read(sockfd,recvline,MAXLINE);
printf("Line recieved from the server : %s",recvline); }
exit(0);
}
Output - server.c
$ gcc server.c -o server
$ ./server
Line recieved from the client :hi
Line recieved from the client :hello Line recieved from the client :i'm saurav
Output - client.c
$ gcc client.c -o client
$ ./client 2
9. Write an echo client and server program using Unix domain stream
socket.
sockaddr)) == -1){
perror("Connect");
exit(1);
}
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
printf("\nReceived data = %s\n " , recv_data);
//send(sock,send_data,strlen(send_data), 0);
close(sock); return 0;
}
Output - server.c
$ gcc server.c -o server
$ ./server
Output - client.c
$ gcc client.c -o client
$ ./client
Received data = Hello User!!
10. Write an echo client and server program using Unix domain
Datagram socket.
perror("Socket"); exit(1);
} server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr =
INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr))
== -1){ perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("\nUDPServer Waiting for client on port 5000"); fflush(stdout);
bytes_read = recvfrom(sock,recv_data,1024,0,(struct sockaddr
*)&client_addr, &addr_len); recv_data[bytes_read]
= '\0'; printf("\n(%s , %d) said :
",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port))
; printf("%s\n", recv_data); fflush(stdout); return 0; }
Client side - client.c
Output - server.c
$ gcc server.c -o server
$ ./server
Output - client.c
$ gcc client.c -o client
$ ./client
Type Something :hi
11. Write a client and server program to implement file transfer.
Server side - server.java
import java.io.BufferedInputStream;
import java.io.File; import
java.io.FileInputStream; import
java.io.IOException; import
java.io.OutputStream; import
java.net.ServerSocket; import
java.net.Socket;
public class TCPFileTransferServer { public static void main(String[]
args) throws IOException {
ServerSocket servsock = new ServerSocket(12345);
File myFile = new File("./Study.txt"); while
(true) {
System.out.println("Waiting for client"); Socket
sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending file data to client");
os.write(mybytearray, 0, mybytearray.length);
System.out.println("Sent file successfully to
client"); os.flush(); sock.close(); }
}
}
Output - server.java
$ java server.java
Waiting for client
Sending file data to client
Sent file successfully to client
Waiting for client
Output - client.java
$ java client.java
Connected to server
Receiving file
File Copied Successfully
12. Write a client and server program to implement the remote command
execution
Output - server.java
$ java server.java
Server Started and listening to the port 25000
Command received from client is : $ ls
Command output : $ client.java server.java
Output - client.java
$ java client.java
Command sent to the server : ls
client.java server.java
Command output received from the server :
null
13. Write a client program that gets a number from the user and sends
the number to the server for conversion into hexadecimal and gets
the result from the server.
String str=(String)dis.readUTF();
Integer n=Integer.parseInt(str);
String hex1=Integer.toHexString(n);
System.out.println("hexadecimal value of user input = "+
hex1); ss.close(); }
catch(Exception e)
{
System.out.println(e);
}
}
}
Output - server.java
$ java server.java hexadecimal value
of user input = 20 $ java
server.java hexadecimal value of
user input = 23
Output - client.java
$ java
client.java 32 $
java client.java
35