Java 简单Socket聊天系统模型(二)

 
package com.lzx.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(10000);
		final Socket socket = server.accept();
		
		new Thread(){
			public void run() {
				//获得客户端发来的数据
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					while(true){
						System.out.println("Receive from client : " + reader.readLine());
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
		
		new Thread(){
			public void run() {
				try {
					PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
					BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
					String readline = in.readLine();
					System.out.println(readline);
					while(true){
						out.println(readline);
						System.out.println(" server send: " + readline);
						readline = in.readLine();
						if(readline.equals("bye"))
							break;
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

}

上一篇:阿里无线的雄心壮志


下一篇:Weex开源首月记