Practical Lab File Based ON Network Programming: B.Tech in Computer Science
Practical Lab File Based ON Network Programming: B.Tech in Computer Science
ON
NETWORK PROGRAMMING
PRACTICAL WORK
Submitted By
Shiv Kumar Chaudhary Kurmi
Under the guidance of
Mr. Bhavendra Sinha
In partial fulfillment for the award of the degree
Of
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
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.
Output - server.java
$ java server.java
Output - client.java
$ java client.java
Thu Oct 08 07:08:02 UTC 2020
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
package crunchify.com.tutorials;
import java.io.IOException; import
java.net.InetSocketAddress; import
java.nio.ByteBuffer;
import java.nio.channels.SocketChannel; import
java.util.ArrayList;
/**
* @author Crunchify.com
* */
public class CrunchifyNIOClient { public static void
main(String[] args) throws IOException,
InterruptedException {
InetSocketAddress crunchifyAddr = new
InetSocketAddress("localhost", 1111);
SocketChannel crunchifyClient =
SocketChannel.open(crunchifyAddr); log("Connecting to
Server on port 1111...");
ArrayList<String> companyDetails = new ArrayList<String>();
// create a ArrayList with companyName list
companyDetails.add("Facebook");
companyDetails.add("Twitter");
companyDetails.add("IBM"); companyDetails.add("Google");
companyDetails.add("Crunchify");
for (String companyName : companyDetails) { byte[] message
= new String(companyName).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(message);
crunchifyClient.write(buffer); log("sending:
" + companyName);
buffer.clear();
// wait for 2 seconds before sending next message
Thread.sleep(2000);
}
crunchifyClient.close();
}
private static void log(String str) {
System.out.println(str); }
}
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!!
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
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream; import
java.net.Socket;
public class TCPFileTransferClient { public static void
main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 12345);
System.out.println("Connected to server"); byte[]
mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("./Readtext.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead;
System.out.println("Receiving file ");
while ((bytesRead = is.read(mybytearray)) != -1) { bos.write(mybytearray,
0, bytesRead);
}
System.out.println("File Copied Successfully");
bos.close(); 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
$ 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