socket多线程

直接上代码把。没啥好说的。

客户端

package util.thread_socket;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class ThreadClient {

public ThreadClient(){
try {
int no;
InetAddress address = InetAddress.getByName("localhost");
for(no=0;no<5;no++){
new ThreadClientCode(address);
}
} catch (UnknownHostException e) {
e.printStackTrace();
}

}

public static void main(String[] args) {

new ThreadClient();
}
}
class ThreadClientCode extends Thread {

private int port = 9999;

private Socket socket = null;

private static int count = 0;
private int id = count++;

private BufferedReader in = null;
private PrintWriter out = null;

public ThreadClientCode(InetAddress address){
try {
socket = new Socket(address ,port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
start();
} catch (IOException e) {
e.printStackTrace();
}
}

public void run(){
try {
out.println(" id " + id);
String str = in.readLine();
System.out.println(str);
out.println("bye");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}


服务端

package util.thread_socket;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;


public class ThreadServer {

private int port = 9999;

private ServerSocket server = null;

private static AtomicInteger count = new AtomicInteger(0);

public ThreadServer() throws IOException{

server = new ServerSocket(port);
System.out.println(" server start at port: " + port);
try {
while(true){
Socket socket = server.accept();
count.addAndGet(1);
new ThreadServerCode(socket);
}
} finally {
server.close();
}
}

public static void main(String[] args) throws IOException {

new ThreadServer();
}

class ThreadServerCode extends Thread {

private Socket socket = null;

private BufferedReader in = null;
private PrintWriter out = null;

public ThreadServerCode(){
}

public ThreadServerCode(Socket socket){
try {
this.socket = socket;
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true);
start();
} catch (IOException e) {
e.printStackTrace();
}
}

public void run(){
try {
while(true){
String str = in.readLine();
if(str==null || "bye".equals(str)){
count.decrementAndGet();
break;
}
System.out.println(str);
out.println("\n 当前连接数: " + count.get() + " 时间 : " + 
new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(new Date()).toString() +
" 接受信息info :" +
str);
}
System.out.println("close server");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值