import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Servers extends Thread{
private ServerSocket serverSocket ;
private Socket socket ;
public Servers(){
try {
serverSocket = new ServerSocket(55555);
System.out.println(" server is Starting....... ");
socket = serverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
InputStream input = null;
BufferedReader reader = null;
OutputStream output = null;
PrintStream pw = null;
try {
//告诉客户端已经连接上
output = socket.getOutputStream();
pw = new PrintStream(output);
pw.println("欢迎你的到来:"+socket.getRemoteSocketAddress());
//接收客户端的说话
input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
boolean flag = false;
//监视客户端的说话
while( !flag ){
System.out.println(" 客户端说: "+line);
pw.println(line);
pw.flush();
line = reader.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Servers().start();
}
}