Programs Using TCP Sockets (Like Date and Time Server & Client, Echo Server & Client, E.T.C
Programs Using TCP Sockets (Like Date and Time Server & Client, Echo Server & Client, E.T.C
Programs Using TCP Sockets (Like Date and Time Server & Client, Echo Server & Client, E.T.C
Programs using TCP Sockets (like date and time server & client,
echo server & client, e.t.c
EX.NO: 1a. PROGRAM USING TCP SOCKETS DATE AND TIME SERVER
AIM:
To implement date and time display from client to server using TCP Sockets
DESCRIPTION:
TCP Server gets the system date and time and opens the server socket to read the
client details. Clients end its address to the server. Then client receives the date and time from
server to display. TCP socket server client connection is opened for communication. After the
date time is displayed the server client connection is closed with its respective streams to be
closed.
ALGORITHM:
Server
Client
PROGRAM:
OUTPUT
Server:
$ javac tcpdateserver.java
$ java tcpdateserver
Press Ctrl+C to quit
Client System/IP address is : localhost.localdomain/127.0.0.1
Client System/IP address is : localhost.localdomain/127.0.0.1
Client:
$ javac tcpdateclient.java
$ java tcpdateclient
The date/time on server is: Wed Jul 06 07:12:03 GMT 2011
Every time when a client connects to the server, server’s date/time will be returned to
the client for synchronization.
RESULT
Thus the program for implementing to display date and time from client to server
using TCP Sockets was executed successfully and output verified using various samples.
EX.NO: 1b. IMPLEMENTATION OF CLIENT-SERVER COMMUNICATION
USING TCP
AIM:
DESCRIPTION:
TCP Clients sends request to server and server will receives the request and response
with acknowledgement. Every time client communicates with server and receives response
from it.
ALGORITHM:
Server
Client
PROGRAM:
//Server.java
import java.io.*;
import java.net.*;
class Server {
public static void main(string arg[]) {
string = "Networks Lab";
try { ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
//Client.java
import java.io.*;
import java.net.*;
class Client{
public static void main(String args[]) {
try{
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine());
System.out.print("'\n");
n.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
OUTPUT
Server:
$ javac Server.java
$ java Server
Server started
Client connected
Cilent
$ javac Client.java
$ java Client
RESULT
Thus both the client and server exchange data using TCP socket programming.
EX.NO: 1c. IMPLEMENTATION OF TCP/IP ECHO
AIM:
DESCRIPTION:
TCP Server gets the message and opens the server socket to read the client details.
Client send its address to the server. Then client receives the message from the server to
display
ALGORITHM:
Server
Client
1. Create a client socket and connect it to the server‟s port number.
2. Get input from user.
3. If equal to bye or null, then go to step 7.
4. Send user data to the server.
5. Display the data echoed by the server.
6. Repeat steps 2-4.
7. Close the input and output streams.
8. Close the client socket.
9. Stop
PROGRAM:
OUTPUT
Server:
$ javac tcpechoserver.java
$ java tcpechoserver
Server Ready Client Connected Client [ hello ]
Client [ how are you ] Client [ i am fine ] Client [ ok ]
Client Disconnected
Client:
$ javac tcpechoclient.java
$ java tcpechoclient
Type "bye" to quit
Enter msg to server : hello
Server [ hello ]
Enter msg to server : how are you
Server [ how are you ]
Enter msg to server : i am fine
Server [ i am fine ]
Enter msg to server : ok
Server [ ok ]
Enter msg to server : bye
RESULT
Thus data from client to server is echoed back to the client to check reliability/noise level of
the channel
2. Programs using UDP Sockets
AIM
To implement a chat server and client in java using UDP sockets.
DESCRIPTION:
UDP is a connectionless protocol and the socket is created for client and
server to transfer the data. Socket connection is achieved using the port number.
Domain Name System is the naming convention that divides the Internet into logical
domains identified in Internet Protocol version 4 (IPv4) as a 32-bit portion of the total
address.
ALGORITHM:
Server
1. Create two ports, server port and client port.
2. Create a datagram socket and bind it to client port.
3. Create a datagram packet to receive client message.
4. Wait for client's data and accept it.
5. Read Client's message.
6. Get data from user.
7. Create a datagram packet and send message through server
port.
8. Repeat steps 3-7 until the client has something to send.
9. Close the server socket.
10. Stop.
Client
1. Create two ports, server port and client port.
2. Create a datagram socket and bind it to server port.
3. Get data from user.
4. Create a datagram packet and send data with server ip addr
and client port.
5. Create a datagram packet to receive server message.
6. Read server's response and display it.
7. Repeat steps 3-6 until there is some text to send.
8. Close the client socket.
9. Stop.
PROGRAM
OUTPUT
Server
$ javac udpchatserver.java
$ java udpchatserver
Server Ready
From Client <<< are u the SERVER
Msg to Cleint : yes
From Client <<< what do u have to serve
Msg to Cleint : no eatables
Client Quits
Client
$ javac udpchatclient.java$ java udpchatclient
Press Enter without text to quit
Enter text for server : are u the SERVER
From Server <<< yes
Enter text for server : what do u have to serve
From Server <<< no eatables
Enter text for server : Ok
RESULT
Thus both the client and server exchange data using UDP sockets.
Ex.No:2.b DNS SERVER TO RESOLVE A GIVEN HOST NAME
AIM:
To develop a client that contacts a given DNS server to resolve a given hostname.
DESCRIPTION:
Get the host name to be resolve using gethostname()
Check the host name using nslookup
Print the IP address, host name, Address length and Address type.
List the addresses stored in lookup
ALGORITHM
PROGRAM
#include<stdio.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<netinet/in.h>
int main(int argc,char**argv)
{
char h_name;
int h_type;
struct hostent *host;
struct in_addr h_addr;
if(argc!=2)
{
fprintf(stderr,"USAGE:nslookup\n");
}
if((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"(mini)nslookup failed on %s\n",argv[1]);
}
h_addr.s_addr=*((unsigned long*)host->h_addr_list[0]);
printf("\n IP ADDRESS=%s\n",inet_ntoa(h_addr));
printf("\n HOST NAME=%s\n",host->h_name);
printf("\nADDRESS LENGTH =%d\n",host->h_length);
printf("\nADDRESS TYPE=%d\n",host->h_addrtype);
printf("\nLIST OF ADDRESS=%s\n",inet_ntoa(h_addr_list[0]));
}
OUTPUT
[it28@localhost ~]$ vi dns.c
[it28@localhost ~]$ cc dns.c
[it28@localhost ~]$ ./a.out 90.0.0.36
IP ADDRESS=90.0.0.36
HOST NAME=90.0.0.36
ADDRESS LENGTH =4
ADDRESS TYPE=2
LIST OF ADDRESS=90.0.0.36
RESULT
Hence the program to develop a client that contacts a given DNS server to resolve a
given host name is executed successfully
EX NO: 2.c UDP DNS SERVER/CLIENT
AIM:
DESCRIPTION
DNS stands for domain name system. unique name of the host is identified with its IP
address through server client communication.
ALGORITHM:
Server
Client
PROGRAM
import java.io.*;
import java.net.*;
public class udpdnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
Senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,
ipaddress,portaddr);
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close(); }}
OUTPUT
Server
$ javac udpdnsserver.java
$ java udpdnsserve
Press Ctrl + C to Quit
Request for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com
Client
$ javac udpdnsclient.java
$ java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
$ java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140
$ java udpdnsclient
Enter the hostname : youtube.com
IP Address: Host Not Found
RESULT
Thus domain name requests by the client are resolved into their respective logical
address using lookup method