Unidirectional Data transfer

 

Write a network program to setup a uni-directional data transfer between a client and a server using TCP

 

Server.java


import java.io.*;
import java.net.*;

class tcp_server
{
	public static void main(String []args) throws Exception
	{
		String str;
		ServerSocket ss=new ServerSocket(7632);
		System.out.println("The Server is up and is waiting for the
client to connect.");
		Socket s=ss.accept();
		System.out.println("The client says: ");
		while(true)
		{
			BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));
			str=br.readLine();
			if(str.equals("`"))
				break;
			System.out.println(str);
		}
		s.close();
	}
}

Client.java


import java.io.*;
import java.net.*;

class tcp_client
{
	public static void main(String args[]) throws Exception
	{
		String str;
		Socket s= new Socket("localhost",7632);
		System.out.println("Enter data to be send to the Server");
		while(true)
		{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
			str=br.readLine();
			if(str.equals("`"))
				break;
			PrintWriter pr= new PrintWriter(s.getOutputStream());
			pr.println(str);
			pr.flush();
			System.out.println("Data Send");
		}
		s.close();
	}
}

Output

 

Terminal 1 Terminal 2
>java Server>The Server is up and is waiting for the client to connect.>The client says:

 

 

>Hello

>World

 

 

>java Client

>Enter data to be send to the Server:

>Hello

>World

 

Leave a Reply

Your email address will not be published. Required fields are marked *