日期:2014-05-20 浏览次数:20851 次
public class ServerInput implements Runnable{
private ServerSocket server;
private Socket socket;
public ServerInput(Socket socket){
this.socket = socket;
}
public void run() {
try {
InputStream ins = socket.getInputStream();
while(true){
byte buffer [] = new byte [1024];
int length = 0;
String message = "客户端说:";
while((length = ins.read(buffer)) != -1){
message += new String(buffer,0,length,"UTF-8");
}
System.out.println(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ServerOutput implements Runnable{
private ServerSocket server;
private Socket socket;
public ServerOutput(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try{
OutputStream out = socket.getOutputStream();
while(true){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String word = br.readLine();
out.write(word.getBytes("UTF-8"));
out.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class Service {
public static void main(String args[]) throws Exception{
ServerSocket server = new ServerSocket(30000);
Socket socket = server.accept();
System.out.println("连接上了");
new ServerOutput(socket).run();
new ServerInput(socket).run();
}
}