Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java, Web
Expertise: Intermediate
Oct 19, 1998

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 java.net.*;
import java.io.*;
import java.util.*;

class SocketEcho01{
	public static void main(String[] args){
		String server = "www.yourFavoriteServer.com";
		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.
Richard G.
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date