示例:
创建ServerSocker过程:
//创建ServerSocket并注册端口号8090
ServerSocket server = new ServerSocket(8090);
while (true) {//循环
final Socket socket = server.accept();//获取数据
System.out.println("ClientSocket:" + client.getPort());
new Thread(new Runnable() {//如果获取到数据则开辟一个线程去处理
@Override
public void run() {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String text = null;
while ((text = bufferedReader.readLine()) != null) {
System.out.println(text);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}