Network Programming, TCP/IP Echo Client

Network Programming, TCP/IP Echo Client

When network testing, it is useful to have a client program that connects to a TCP/IP server and gets an echo of a text string that you send to the server:

 import*;import*;import java.util.*;class SocketEcho01{	public static void main(String[] args){		String server = "";		int port = 7;    	try{			Socket socket = new Socket(server,port);//Get socket            			//Get input and output streams from socket.			BufferedReader inputStream = 					new BufferedReader(new InputStreamReader(							socket.getInputStream()));			PrintWriter outputStream = 					new PrintWriter(new OutputStreamWriter(							socket.getOutputStream()),true);			outputStream.println("This is an echo test");//send      		System.out.println(inputStream.readLine());//receive        			socket.close();		}//end try		catch(UnknownHostException e){		System.out.println(e);			System.out.println(					"Connection Failure. Are you online?");		}//end catch UnknownHostException		catch(IOException e){System.out.println(e);}	}//end main}//end class SocketEcho01

Edit your favorite Web server name into the String named server, and the program should display:

 This is an echo test.  

If you receive another message, you should try another server.


Share the Post: