Professional Documents
Culture Documents
NETWORK PROGRAMMING
LAB
BE CSE 7th Semester
Unix
1. Write an echo program with client and iterative server using TCP. [By Teacher]
2. Write an echo program with client and iterative server using UDP. [By Teacher]
3. Write a program to retrieve date and time using TCP. [By Teacher]
4. Write a program to retrieve date and time using UDP. [By Teacher]
5. Write a client program that gets a number from the user and sends the number to server for
conversion in to hexadecimal and gets the result from the server in TCP. [Assignment to Student]
6. Write a client program that gets a number from the user and sends the number to server for
conversion in to hexadecimal and gets the result from the server in UDP. [Assignment to Student]
Java
7. Write an echo program with client and iterative server using TCP. [By Teacher]
8. Write an echo program with client and iterative server using UDP. [Assignment to Student]
9. Write an echo program with client and concurrent server using TCP. [By Teacher]
10. Write an echo program with client and concurrent server using UDP. [Assignment to Student]
12. Write a client and server program to implement file transfer. [By Teacher]
13. Write a client and server program to implement the remote command execution. [By Teacher]
14. Write a client program that gets a number from the user and sends the number to server for
converting it into words gets the result from the server in TCP. (for example if we enter 86 then
server will return Eighty Six)[Assignment to Student]
15. Write a Client Server Program to exchange keys and generate shared secret key using diffi-
hellmen algorithm.
Experiment No. 01
Write an echo program with client and iterative server using TCP.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step3: Listen on specified port.
Step4: Wait for client connection.
Step5: Accept connection.
Step6: Send the message to the client.
Step7: Close client connection.
Step 8: Close server socket.
Step 9: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main()
{
int sock, connected, bytes_recieved , true =1;
char send_data [1024]={"Hello User!!"}, recv_data[1024];
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1){
perror("Setsockopt");
exit(1);
}
server_addr.sin_family = AF_INET;
Sonu Agrawal, Associate Professor, CSE, SSTC Bhilai Page 3
Shri Shankaracharya Technical Campus
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if(listen(sock,5)==-1){
perror("Listen");
exit(1);
}
Client:
Algorithm:-
Step1: Start the program.
Step2: Create socket.
Step3: connect to server.
Step4: get data from server.
Step5: Close socket.
Step 6: Stop the program.
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
host = gethostbyname("127.0.0.1");
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
exit(1);
}
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved]='\0';
printf("\nRecieved data = %s\n ", recv_data);
//send(sock,send_data,strlen(send_data), 0);
close(sock);
return0;
}
Output:
1. Server:
2. Client:
a@a-ThinkCentre-A58:~/SS$ gcc tcpserver1.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
Experiment No. 02
Write an echo program with client and iterative server using UDP.
Server
Algorithm:-
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int addr_len, bytes_read;
char recv_data[1024];
struct sockaddr_in server_addr , client_addr;
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);
recv_data[bytes_read]='\0';
return0;
}
Client:
Algorithm:-
Step1: Start the program.
Step2: Create socket.
Step 3: get data from server.
Step 4: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
Output
Server
Client
Experiment No. 03
Write a program to retrieve date and time using TCP
Server
Algorithm:-
Step 1: Start the program.
Step 2: Create server socket.
Step 3: Listen on specified port.
Step 4: Wait for client connection.
Step 5: Accept connection.
Step 6: Send the date to the client.
Step 7: Close client connection.
Step 8: Close server socket.
Step 9: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
int main()
{
int sock, connected, bytes_recieved , true =1;
char send_data [1024]={"Hello User!!"}, recv_data[1024];
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1){
perror("Setsockopt");
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(listen(sock,5)==-1){
perror("Listen");
exit(1);
}
send(connected, c_time_string,strlen(c_time_string),0);
close(connected);
close(sock);
return0;
}
Client:
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: connect to server.
Step 4: get date from server.
Step 5: Close socket.
Step 6: Stop the program.
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
host = gethostbyname("127.0.0.1");
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved]='\0';
printf("\nRecieved data = %s ", recv_data);
close(sock);
return0;
}
Output
1. Server
a@a-ThinkCentre-A58:~/SS$ gcc tcpdateserver.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
2. Client
Experiment No. 04
Write a program to retrieve date and time using UDP
Serve
Algorithm:-
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int addr_len, bytes_read;
char recv_data[1024];
char send_data[1024]="Hello";
struct sockaddr_in server_addr , client_addr;
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);
perror("Bind");
exit(1);
}
recv_data[bytes_read]='\0';
return0;
}
Client:
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: send data to server.
Step 4: get date from server.
Step 5: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024]={"Please send me current date and time"};
char recv_data[1024];
int addr_len, bytes_read;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
fflush(stdout);
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&server_addr,&addr_len);
recv_data[bytes_read]='\0';
Output
Server
a@a-ThinkCentre-A58:~/SS$ gcc udpdatentimeserver.c
udpdatentimeserver.c: In function ‘main’:
udpdatentimeserver.c:57: warning: assignment makes pointer from integer without a cast
a@a-ThinkCentre-A58:~/SS$ ./a.out
Client
a@a-ThinkCentre-A58:~/SS$ gcc udpdatentimeclient.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
a@a-ThinkCentre-A58:~/SS$
Experiment No. 05
Write a client program that gets a number from the user and sends the number to server for conversion in
to hexadecimal and gets the result from the server in TCP.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step3: Listen on specified port.
Step4: Wait for client connection.
Step5: Accept connection.
Step6: Get a integer from client.
Step7: Send the hexadecimal of received integer to the client.
Step8: Close client connection.
Step 9: Close server socket.
Step 10: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int sock, connected, bytes_recieved , true =1;
char send_data [1024], recv_data[1024];
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1){
perror("Setsockopt");
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(listen(sock,5)==-1){
perror("Listen");
exit(1);
}
send(connected, send_data,strlen(send_data),0);
close(connected);
close(sock);
return0;
}
Client:
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: connect to server.
Step 4: Send an integer to server.
Step 4: Get Hexadecimal value of corresponding integer from server.
Step 5: Close socket.
Step 6: Stop the program.
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
int main()
host = gethostbyname("127.0.0.1");
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
{
perror("Connect");
exit(1);
}
printf("Sending %d to conver to hex\n ",number);
//itoa(number, send_data, 10);
sprintf(send_data,"%d", number);
send(sock, send_data,strlen(send_data),0);
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved]='\0';
printf("\nRecieved data = %s\n ", recv_data);
close(sock);
return0;
}
Output
Server
Recieved data = 10
received 10 to int 10 Converted to hex a
a@a-ThinkCentre-A58:~/SS$
Client
a@a-ThinkCentre-A58:~/SS$ gcc tcphexclient.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
Sending 10 to conver to hex
Recieved data = a
a@a-ThinkCentre-A58:~/SS$
Experiment No. 06
Write a client program that gets a number from the user and sends the number to server for conversion in
to hexadecimal and gets the result from the server in UDP.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step3: Get the integer from client.
Step4: Send the hexadecimal value of corresponding integer.
Step5: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int addr_len, bytes_read;
char recv_data[1024];
char send_data[1024]="Hello";
struct sockaddr_in server_addr , client_addr;
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);
{
perror("Bind");
exit(1);
}
recv_data[bytes_read]='\0';
return0;
}
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: send integer value to server.
Step 4: get corresponding hexadecimal value from server.
Step 5: Stop the program.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024]={"Please send me current date and time"};
int number;
char recv_data[1024];
int addr_len, bytes_read;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
printf("Enter a number");
scanf("%d",&number);
sprintf(send_data,"%d", number);
fflush(stdout);
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&server_addr,&addr_len);
recv_data[bytes_read]='\0';
Output
Server
a@a-ThinkCentre-A58:~/SS$ gcc udphexserver.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
Client
a@a-ThinkCentre-A58:~/SS$ gcc udphexserver.c
a@a-ThinkCentre-A58:~/SS$ ./a.out
Experiment No. 07
Write an echo program with client and iterative server using TCP.
Server
Algorithm:-
import java.net.*;
import java.io.*;
publicclass TcpEchoServer {
}catch(Exception e){
e.printStackTrace();
}finally{
try{
socket.close();
}catch(Exception e){
}
}
}
}
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send message to server.
Step 4: Get message from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.net.*;
publicclass TcpEchoClient {
Output
Server
Server Started and listening to the port 25000
Message received from client is John
Message sent to the client is Hello John !!
Client
Message sent to the server : John
Experiment No. 08
Write an echo program with client and iterative server using UDP.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step4: Get Message from server.
Step3: Send message to client.
Step4: Close the connection.
Step5: Stop the program.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
publicclass UDPEchoServer {
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send message to server.
Step 4: Get message from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
publicclass UDPEchoClient1 {
publicstaticvoid main(String args[])throws Exception {
System.out.println("Write some content to send to server\n");
BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
//creates a DatagramSocket:
DatagramSocket clientSocket =new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
Output
Server
Waiting for client
Received from client: Hello
Client
Write some content to send to server
Hello
Experiment No. 09
Write an echo program with client and concurrent server using TCP
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step3: accept client Connection.
Step4: Get an integer value from client.
Step3: Send square of received integer value to client.
Step4: Close the connection.
Step5: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
publicclass TCPConcurrentServer {
String returnMessage;
try{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat *2;
returnMessage = String.valueOf(returnValue)+"\n";
}catch(NumberFormatException e){
//Input was not a number. Sending proper message back to client.
returnMessage ="Please send a proper number\n";
}
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send an integer value to server.
Step 4: Get square of sent integer from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
publicclass TCPEchoClient1 {
Output
Server
Server Started and listening to the port 25000
Message received from client is 2
Message sent to the client is 4
Client
Message sent to the server : 2
Experiment No.10
Write an echo program with client and concurrent server using UDP.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step4: Get Message from server.
Step3: Send message to client.
Step4: Close the connection.
Step5: Stop the program.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
publicclass UDPConcurrentServer {
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send message to server.
Step 4: Get message from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
publicclass UDPEchoClient1 {
publicstaticvoid main(String args[])throws Exception {
System.out.println("Write some content to send to server\n");
BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
//creates a DatagramSocket:
DatagramSocket clientSocket =new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
Output
Server
Waiting for client
Received data from client : Hello Friends
Client
Write some content to send to server
Hello Friends
Experiment No.12
Write a client and server program to implement file transfer.
Server
Algorithm:-
Step1: Start the program.
Step2: Create server socket.
Step3: accept client Connection.
Step 4: Send file to client.
Step5: Close the connection.
Step6: Stop the program.
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;
publicclass TCPFileTransferServer {
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Receive file from server.
Step 4: Close socket.
Step 5: Stop the program.
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
publicclass TCPFileTransferClient {
Output
Server
Waiting for client
Sending file data to client
Sent file successfully to client
Waiting for client
Client
Connected to server
Receiving file
File Copied Successfully
Experiment No.13
Write a client and server program to implement the remote command execution.
Server
Algorithm:-
Step 1: Start the program.
Step 2: Create server socket.
Step 3: accept client Connection.
Step 4: Get command from client.
Step 5: Return output of command to client.
Step6: Close the socket.
Step7: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
publicclass TCPRemoteCommandServer {
}catch(IOException e1){
}catch(InterruptedException e2){
}
return output.toString();
}
}
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send command to server.
Step 4: Receive output of command from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
publicclass TCPRemoteCommandClient {
//line = br.readLine();
while((line = br.readLine())!=null){
System.out.println(line);
}
System.out.println("Command output received from the server :\n "+ line);
}catch(Exception exception){
exception.printStackTrace();
}finally{
//Closing the socket
try{
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Output
Server
Server Started and listening to the port 25000
Command received from client is cmd /c ver
Command output
Microsoft Windows [Version 6.3.9600]
Client
Command sent to the server : cmd /c ver
Experiment No.14
Write a client program that gets a number from the user and sends the number to server for converting it
into words gets the result from the server in TCP. (For example if we enter 86 then server will return
Eighty Six).
Server
Algorithm:-
Step 1: Start the program.
Step 2: Create server socket.
Step 3: accept client Connection.
Step 4: Get number from client.
Step 5: Return in words to client.
Step6: Close the socket.
Step7: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
publicclass TCPWordConverterServer {
EnglishNumberToWords.java
import java.text.DecimalFormat;
import java.text.DecimalFormat;
publicclass EnglishNumberToWords {
};
public EnglishNumberToWords(){
}
if(number %100<20){
soFar = numNames[number %100];
number /=100;
}else{
soFar = numNames[number %10];
number /=10;
// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions;
switch(billions){
case0:
tradBillions ="";
break;
case1:
tradBillions = convertLessThanOneThousand(billions)
+" billion ";
break;
default:
tradBillions = convertLessThanOneThousand(billions)
+" billion ";
}
String result = tradBillions;
String tradMillions;
switch(millions){
case0:
tradMillions ="";
break;
case1:
tradMillions = convertLessThanOneThousand(millions)
+" million ";
break;
default:
tradMillions = convertLessThanOneThousand(millions)
+" million ";
}
result = result + tradMillions;
String tradHundredThousands;
switch(hundredThousands){
case0:
tradHundredThousands ="";
break;
case1:
tradHundredThousands ="one thousand ";
break;
default:
tradHundredThousands = convertLessThanOneThousand(hundredThousands)
+" thousand ";
}
result = result + tradHundredThousands;
String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;
Client
Algorithm:-
Step 1: Start the program.
Step 2: Create socket.
Step 3: Send number to server.
Step 4: Receive in word of number from server.
Step 5: Close socket.
Step 6: Stop the program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
publicclass TCPWordClient {
privatestatic Socket socket;
}catch(Exception exception){
exception.printStackTrace();
}finally{
//Closing the socket
try{
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Output
Server
Server Started and listening to the port 25001
Number received from client is 253
In words sent to the client is two hundred fifty three
Client
Message sent to the server : 253