0% found this document useful (0 votes)
11 views25 pages

47 Neelam Savardekar

The document outlines a Computer Networks Programming course at SNDT Women's University, detailing various programming assignments using TCP and UDP sockets in Java. It includes implementations for TCP Echo Server/Client, TCP Date Server/Client, TCP Chat Server/Client, UDP Echo Server/Client, and UDP Chat Server/Client, along with explanations of socket communication. Additionally, it lists shell scripting exercises related to arithmetic operations, file handling, and network checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

47 Neelam Savardekar

The document outlines a Computer Networks Programming course at SNDT Women's University, detailing various programming assignments using TCP and UDP sockets in Java. It includes implementations for TCP Echo Server/Client, TCP Date Server/Client, TCP Chat Server/Client, UDP Echo Server/Client, and UDP Chat Server/Client, along with explanations of socket communication. Additionally, it lists shell scripting exercises related to arithmetic operations, file handling, and network checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SNDT WOMENS’ UNIVERSITY PGCS

COMPUTER NETWORKS PROGRAMMING USING LINUX

NEELAM SAVARDEKAR
SEMESTER-1
ROLL NO. 100156
INDEX

Program No. Program Name


1 TCP Sockets
1a TCP Echo Server/Client
1b TCP Date Server/Client
1c TCP Chat Server/Client

2 UDP Sockets
2a UDP Echo Server/Client
2b UDP Chat Server/Client
TCP Sockets

A socket is an endpoint of a two-way communication link between two programs running on the
network. Socket is bound to a port number so that the TCP layer can identify the application that
data is destined to be sent. User-level process/services generally use port number value 1024.
TCP provides a reliable, point-to-point communication channel that client-server applications on
the Internet use to communicate with each other. Examples are FTP and Telnet. To communicate
over TCP, a client program and a server program establish a connection to one another. Each
program binds a socket to its end of the connection. A server runs on a specific computer and has
a socket that is bound to a specific port number. The server waits, listening to the socket for a
connection request from the client. On the client-side, the client knows the hostname of the
machine on which the server is running and the port number on which the server is listening. To
make a connection request, the client tries to make contact with the server on the server's
machine and port. The client also needs to identify itself to the server so it binds to a local port
number that it will use during this connection. If everything goes well, the server accepts the
connection. Upon acceptance, the server gets a new socket bound to the same local port and also
has its remote endpoint set to the address and port of the client. It needs a new socket so that it
can continue to listen to the original socket for connection requests while tending to the needs of
the connected client. On the client side, if the connection is accepted, a socket is successfully
created and the client can use the socket to communicate with the server. The client and server
can now communicate by writing to or reading through I/O streams from their sockets and
eventually close it. The two key classes from the java.net package used in creation of server and
client programs are: ServerSocket Socket
Program : TCPEcho Server
Aim To implement echo server and client in java using TCP sockets

import java.net.*;
import java.io.*;
public class tcpechoserver {
public static void main(String[] arg) throws IOException
{
ServerSocket sock = null;
BufferedReader fromClient = null;
OutputStreamWriter toClient = null;
Socket client = null;
try
{
sock = new ServerSocket(4000);
System.out.println("Server Ready");
client = sock.accept();
System.out.println("Client Connected");
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
toClient = new OutputStreamWriter(client.getOutputStream());
String line;
while (true)
{
line = fromClient.readLine();
if ( (line == null) || line.equals("bye"))
break;
System.out.println ("Client [ " + line + " ]");
toClient.write("Server [ "+ line +" ]\n");
toClient.flush();
}
fromClient.close();
toClient.close();
client.close();
sock.close();
System.out.println("Client Disconnected");
}
catch (IOException ioe)
{
System.err.println(ioe);
}
}
}
Clinet:
import java.net.*;
import java.io.*;
public class tcpechoclient{
public static void main(String[] args) throws IOException
{
BufferedReader fromServer = null, fromUser = null;
PrintWriter toServer = null;
Socket sock = null;
try
{
if (args.length == 0)
sock = new Socket(InetAddress.getLocalHost(), 4000);
else
sock = new Socket(InetAddress.getByName(args[0]), 4000);
fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
toServer = new PrintWriter(sock.getOutputStream(), true);
String Usrmsg, Srvmsg;
System.out.println("Type \"bye\" to quit");
while (true)
{
System.out.print("Enter msg to server : ");
Usrmsg = fromUser.readLine();
if (Usrmsg==null || Usrmsg.equals("bye"))
{
toServer.println("bye");
break;
}
else toServer.println(Usrmsg);
Srvmsg = fromServer.readLine();
System.out.println(Srvmsg);
}
fromUser.close();
fromServer.close();
toServer.close();
sock.close();
}
catch (IOException ioe)
{
System.err.println(ioe);
}
}
}
Program: TCP Date Server--tcpdateserver.java
Aim: To implement date server and client in java using TCP sockets
Server:
import java.net.*;
import java.io.*;
import java.util.*;
public class tcpechodateserver{
public static void main(String arg[])
{
ServerSocket ss =null;
Socket cs;
PrintStream ps;
BufferedReader dis;
String inet;
try
{
ss = new ServerSocket(4444);
System.out.println("Press Ctrl+C to quit");
while(true)
{
cs = ss.accept();
ps = new PrintStream(cs.getOutputStream());
Date d = new Date();
ps.println(d);
dis = new BufferedReader(new InputStreamReader(cs.getInputStream()));
inet = dis.readLine();
System.out.println("Client System/IP address is :" + inet);
ps.close();
dis.close();
}
}
catch(IOException e)
{
System.out.println("The exception is :" + e);
}
}
}

Client:
import java.net.*;
import java.io.*;
public class tcpechodateclient{
public static void main (String args[])
{
Socket soc;
BufferedReader dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia = InetAddress.getLocalHost();
if (args.length == 0)
soc = new Socket(InetAddress.getLocalHost(),4444);
else
soc = new Socket(InetAddress.getByName(args[0]), 4444);
dis = new BufferedReader(new InputStreamReader(soc.getInputStream()));
sdate=dis.readLine();
System.out.println("The date/time on server is : " + sdate);
ps = new PrintStream(soc.getOutputStream()); ps.println(ia);
ps.close();
}
catch(IOException e)
{
System.out.println("THE EXCEPTION is :" + e);
}
}
}
Program: TCP Chat Server--tcpchatserver.java import
Aim To implement a chat server and client in java using TCP sockets
Server:
import java.io.*;
import java.net.*;
class tcpchatserver{
public static void main(String args[])throws Exception
{
PrintWriter toClient; BufferedReader fromUser,fromClient;
try
{
ServerSocket Srv = new ServerSocket(5555);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept();
System.out.println("Client connected");
toClient = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())),
true);
fromClient = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg; while(true)
{
CltMsg= fromClient.readLine();
if(CltMsg.equals("end"))
break;
else
{
System.out.println("\nServer <<< " + CltMsg);
System.out.print("Message to Client : ");
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
Srv.close();
}
catch (Exception E)
{
System.out.println(E.getMessage());
}
}
}

Client:
import java.io.*;
import java.net.*;
class tcpchatclient{
public static void main(String args[])throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try
{
if (args.length > 1)
{
System.out.println("Usage: java hostipaddr");
System.exit(-1);
}
if (args.length == 0)
Clt = new Socket(InetAddress.getLocalHost(),5555);
else
Clt = new Socket(InetAddress.getByName(args[0]), 5555);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg; System.out.println("Type \"end\" to Quit"); while (true)
{
System.out.print("\nMessage to Server : ");
CltMsg = fromUser.readLine(); toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client <<< " + SrvMsg);
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
}
}
}
UDP Sockets
TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these
features are not required, since they do not come without performance costs, it would be better to
use a lighter transport protocol such as UDP (User Datagram Protocol). UDP is an unreliable
protocol, i.e., it does not include software mechanisms for retrying on transmission failures or
data corruption (unlike TCP), and has restrictions on message length (< 65536 bytes). Examples
are NFS, DNS, SNMP, Clock Server, Ping, VoIP, online games etc.

Unlike TCP there is no concept of a connection, UDP is a protocol that sends independent
packets of data, called datagrams, from one computer to another with no guarantees about arrival
and
sequencing. No packet has any knowledge of the preceding or following packet. The recipient
does not
acknowledge packets, thereby the sender does not know whether the transmission was
successful. The
format of datagram packet is

A program can use a single UDP socket to communicate with more than one host and port
number, but it is convenient for most UDP client programs to maintain the fiction that there is a
connection, by keeping a local record of each server host and port number. A UDP server does
not have to listen for and accept client connections, and a UDP client need not connect to a
server. Java supports datagram communication through the following classes: DatagramPacket
DatagramSocket The DatagramPacket object is the data container, while the DatagramSocket is
the mechanism used to send or receive the DatagramPackets.
UDP Echo Server/Client

Program: UDP Echo Server--UDPEchoServer.java


Aim To implement echo server and client in java using UDP sockets.
Server:
import java.net.*;
class UDPEchoServer{
public static void main(String args[]) throws Exception
{
DatagramSocket SrvSoc = new DatagramSocket(7777);
byte[] SData = new byte[1024];
System.out.println("Server Ready\n");
while (true)
{
byte[] RData = new byte[1024];
DatagramPacket RPack = new DatagramPacket(RData, RData.length);
SrvSoc.receive(RPack);
String Text = new String(RPack.getData());
if (Text.trim().length() > 0)
{
System.out.println("From Client <<< " + Text);
InetAddress IPAddr = RPack.getAddress();
int Port = RPack.getPort();
SData = Text.toUpperCase() .getBytes();
DatagramPacket SPack = new DatagramPacket(SData,
SData.length, IPAddr, Port);
SrvSoc.send(SPack);
}
else
break;
}
System.out.println("\nClient Quits\n");
SrvSoc.close();
}
}

Client:
import java.net.*;
class UDPEchoServer{
public static void main(String args[]) throws Exception
{
DatagramSocket SrvSoc = new DatagramSocket(7777);
byte[] SData = new byte[1024];
System.out.println("Server Ready\n");
while (true)
{
byte[] RData = new byte[1024];
DatagramPacket RPack = new DatagramPacket(RData, RData.length);
SrvSoc.receive(RPack);
String Text = new String(RPack.getData());
if (Text.trim().length() > 0)
{
System.out.println("From Client <<< " + Text);
InetAddress IPAddr = RPack.getAddress();
int Port = RPack.getPort();
SData = Text.toUpperCase() .getBytes();
DatagramPacket SPack = new DatagramPacket(SData,
SData.length, IPAddr, Port);
SrvSoc.send(SPack);
}
else
break;
}
System.out.println("\nClient Quits\n");
SrvSoc.close();
}
}
Program: UDP Chat Server--udpchatserver.java
Aim: To implement a chat server and client in java using UDP sockets
Server
import java.io.*;
import java.net.*;
class udpchatserver{
public static int clientport = 8040,serverport = 8050;
public static void main(String args[]) throws Exception
{
DatagramSocket SrvSoc = new DatagramSocket(clientport);
byte[] SData = new byte[1024];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Server Ready");
while (true)
{
byte[] RData = new byte[1024];
DatagramPacket RPack = new DatagramPacket(RData, RData.length);
SrvSoc.receive(RPack);
String Text = new String(RPack.getData()); if (Text.trim().length() == 0)
break;
System.out.println("\nFrom Client <<< " + Text );
System.out.print("Msg to Client : " );
String srvmsg = br.readLine();
InetAddress IPAddr = RPack.getAddress();
SData = srvmsg.getBytes();
DatagramPacket SPack = new DatagramPacket(SData, SData.length, IPAddr, serverport);
SrvSoc.send(SPack);
}
System.out.println("\nClient Quits\n");
SrvSoc.close();
}
}

Client:
import java.io.*;
import java.net.*;
class udpchatclient{
public static int clientport = 8040,serverport = 8050;
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
DatagramSocket CliSoc = new DatagramSocket(serverport); InetAddress IPAddr;
String Text;
if (args.length == 0)
IPAddr = InetAddress.getLocalHost();
else
IPAddr = InetAddress.getByName(args[0]);
byte[] SData = new byte[1024];
System.out.println("Press Enter without text to quit");
while (true)
{
System.out.print("\nEnter text for server : ");
Text = br.readLine();
SData = Text.getBytes();
DatagramPacket SPack = new DatagramPacket(SData, SData.length, IPAddr, clientport );
CliSoc.send(SPack);
if (Text.trim().length() == 0)
break;
byte[] RData = new byte[1024];
DatagramPacket RPack = new DatagramPacket(RData, RData.length);
CliSoc.receive(RPack);
String Echo = new String(RPack.getData());
Echo = Echo.trim();
System.out.println("From Server <<< " + Echo);
}
CliSoc.close();
}
}
Shell Scripting Commands:
Questions:
1. Write a shell script that calculates the sum of integers from 1 to N using a loop.

2.Write a function in a shell script that calculates the factorial of a given numb
3.Write a script that generates a secure random password.

4.Create a script that searches for a specific word in a file and counts its occurrences.
5.How do you read lines from a file within a shell script?
6.Create a shell script that finds and lists all empty files in a directory.

7. Write a shell script that converts all filenames in a directory to lowercase


8.How can you use arithmetic operations within a shell script?
9.Create a script that checks if a network host is reachable

10.Write a Shell Script to Find the Greatest Element in an Array:


11.Write a script to calculate the sum of Elements in an Array.

You might also like