Client.java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("Localhost", 9999);
BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter w = new PrintWriter(s.getOutputStream(), true);
BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
String line;
do {
line = r.readLine();
if( line != null )
System.out.println(line);
line = con.readLine();
w.println(line);
}
while ( !line.trim().equals("bye") );
}
catch (Exception err) {
System.err.println(err);
}
}
}
Server.java
import java.io.*;
import java.net.*;
public class Server {
private ServerSocket server;
public Server(int portnum) {
try{
server = new ServerSocket(portnum);
}
catch (Exception err) {
System.out.println(err);
}
}
public void serve() {
try {
while (true) {
Socket client = server.accept();
BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter w = new PrintWriter(client.getOutputStream(), true);
w.println("Welcome to the Java EchoServer. Type 'bye' to close.");
String line;
do {
line = r.readLine();
if( line != null )
w.println("Got: "+ line);
}
while ( !line.trim().equals("bye") );
client.close();
}
}
catch (Exception err) {
System.err.println(err);
}
}
public static void main(String[] args){
Server s = new Server(9999);
s.serve();
}
}
Output
Welcome to the Java EchoServer. Type 'bye' to close.
hi
Got: hi
whats up
Got: whats up
f9
Got: f9
A blog provide anexample with sample code for swing , servlet, jdbc and odbc connection to database, jsp, servlets, taglib, sql server, cassandra and many more. we also provide Apache camel sample code for integration.
Saturday, October 20, 2012
Echoserver
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment