Server:
import java.net.*;
import java.io.*;
public class Server extends ServerSocket {
private Socket socket;
private static final int SERVER_PORT = 6789;
public Server() throws IOException
{
super(SERVER_PORT);
try
{
while(true)
{
socket = accept();
new CreateServerThread(socket);
}
}
catch(IOException e)
{}
finally
{
close();
}
}
class CreateServerThread extends Thread
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public CreateServerThread(Socket s) throws IOException
{
client = s;
in = new BufferedReader(new InputStreamReader(client.getInputStream(),"GB2312"));
out = new PrintWriter(client.getOutputStream(),true);
out.println("--Welcome--");
start();
}
public void run()
{
try
{
String line = in.readLine();
while(!line.equals("bye"))
{
String msg = CreateMsg(line);
out.println(msg);
System.out.println(msg);
out.flush();
line = in.readLine();
}
sleep(300);
out.println("--see you,bye--");
client.close();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
private String CreateMsg(String line)
{
return "hello " + line;
}
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new Server();
}
}
Client:
same as the single client