Switch to full style
General Java code examples
Post a reply

echo client using Sockets

Thu Feb 07, 2013 8:18 pm

echo client Socket
java code
import java.io.*;
import java.net.*;

public class EchoClient {
public static void main(String[] args) throws IOException {

Socket echoSocket = null;
DataOutputStream out = null;
DataInputStream in = null;

try {
echoSocket = new Socket("campione", 7);
out = new DataOutputStream(echoSocket.getOutputStream());
in = new DataInputStream(echoSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Host not known.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't connect ");
System.exit(1);
}

DataInputStream stdIn = new DataInputStream(System.in);
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.writeBytes(userInput);
out.writeByte('\n');
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}




Post a reply
  Related Posts  to : echo client using Sockets
 apply SSL on Sockets     -  
 PHP echo command     -  
 Echo multiline string     -  
 Difference between PHP echo() and PHP print()?     -  
 echo command to output HTML     -  
 client server client     -  
 FTP Server and FTP Client     -  
 name and version of the client     -  
 UDP server and UDP client (J2SE)     -  
 MySQL client information in php     -  

Topic Tags

Java Networking