public void run(){ PrintWriter pw = null; try { InputStream in = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(in, "utf-8"); BufferedReader br = new BufferedReader(isr); OutputStream out = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8"); BufferedWriter bw = new BufferedWriter(osw); pw = new PrintWriter(bw, true); synchronized (Server.this){//限制自身 map.put(host,pw); pws = Arrays.copyOf(pws,pws.length+1); pws[pws.length-1] = pw; } send(host+"上线了,当前在线人数:"+pws.length);
时间: 2024-02-10 18:34:25 浏览: 171
这段代码是一个多线程聊天室服务器端的run()方法。首先,创建一个PrintWriter对象pw,用于向客户端发送消息。接着,通过socket.getInputStream()获取输入流,通过InputStreamReader将字节流转化为字符流,并指定字符编码为"utf-8"。再通过BufferedReader对字符流进行缓冲读取。同时,通过socket.getOutputStream()获取输出流,通过OutputStreamWriter将字符流转化为字节流,并指定字符编码为"utf-8"。再通过BufferedWriter对字节流进行缓冲写入。最后,通过PrintWriter将字符流转换为字节流,并自动刷新缓冲区,发送消息给客户端。在synchronized块中,将当前客户端的输出流pw存入map集合中,方便后面广播消息使用。同时,将pws数组扩容,并将当前客户端的输出流pw存入pws数组中。最后,向所有客户端发送上线消息及当前在线人数。
相关问题
利用Java1.实现daytime协议,从远程计算机上获取时间,并更新本机时间。 2.实现echo协议,向远程计算机发送信息,并获取返回信息。
1. 实现daytime协议:
```java
import java.io.*;
import java.net.*;
public class DaytimeClient {
public static void main(String[] args) {
try {
// 创建连接
Socket socket = new Socket("time.nist.gov", 13);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// 读取时间信息
String time = br.readLine();
// 解析时间信息
String[] tokens = time.split(" ");
String dateTime = tokens[2] + " " + tokens[3];
// 设置本机时间
Runtime.getRuntime().exec("cmd /c time " + dateTime);
// 关闭连接
br.close();
isr.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 实现echo协议:
```java
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) {
try {
// 创建连接
Socket socket = new Socket("localhost", 7777);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 发送消息并接收回复
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
System.out.println("Server response: " + in.readLine());
}
// 关闭连接
br.close();
pw.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
服务端代码:
```java
import java.io.*;
import java.net.*;
public class EchoServer {
public static void main(String[] args) {
try {
// 创建ServerSocket,监听端口7777
ServerSocket serverSocket = new ServerSocket(7777);
while (true) {
// 等待客户端连接
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 接收消息并回复
String line;
while ((line = in.readLine()) != null) {
System.out.println("Received message: " + line);
out.println("Echo: " + line);
}
// 关闭连接
in.close();
out.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文