📄 threadclient.java
字号:
package tcp;
import java.net.*;
import java.io.*;
class ClientThreadCode extends Thread {
// 客户端的socket
private Socket socket;
// 线程统计数,用来给线程编号
private static int cnt = 0;
private int clientId = cnt++;
private BufferedReader in;
private PrintWriter out;
// 构造函数
public ClientThreadCode(InetAddress addr) {
try {
socket = new Socket(addr, 3333);
} catch (IOException e) {
e.printStackTrace();
}
// 实例化IO对象
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
// 开启线程
start();
} catch (IOException e) {
// 出现异常,关闭socket
try {
socket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
// 线程主体方法
public void run() {
try {
out.println("Hello Server,My id is " + clientId);
String str = in.readLine();
System.out.println(str);
out.println("byebye");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class ThreadClient {
public static void main(String[] args) throws IOException,
InterruptedException {
int threadNo = 0;
InetAddress addr = InetAddress.getByName("localhost");
for (threadNo = 0; threadNo < 3; threadNo++) {
new ClientThreadCode(addr);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -