给你一个小例子吧:
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(12345);
System.out.println("服务器启动,绑定12345端口");
while(true) {
//创建Socket
final Socket socket = server.accept();
//创建线程
new Thread() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
//读取客户端发来的信息
String line = br.readLine();
//将字符进行翻转
line = new StringBuilder(line).reverse().toString(); //将客户端写过来的字符串反转
//写数字会客户端
ps.println(line);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
|