java tcp 编程实例_java网络编程之TCP实例

packageSocket;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.InetAddress;importjava.net.Socket;class JabberClientThread extendsThread {privateSocket socket;privateBufferedReader in;privatePrintWriter out;private static int counter = 0;private int id = counter++;private static int threadcount = 0;public static intthreadCount() {returnthreadcount;

}publicJabberClientThread(InetAddress addr) {

System.out.println("Making client " +id);

threadcount++;try{

socket= newSocket(addr, MultiJabberServer.PORT);

}catch(IOException e) {

}try{

in= new BufferedReader(newInputStreamReader(

socket.getInputStream()));

out= new PrintWriter(new BufferedWriter(newOutputStreamWriter(

socket.getOutputStream())),true);

start();

}catch(IOException e) {try{

socket.close();

}catch(IOException e2) {

}

}

}public voidrun() {try{for (int i = 0; i < 25; i++) {

out.println("Client " + id + ":" +i);

String str=in.readLine();

System.out.println(str);

}

out.println("END");

}catch(IOException e) {

}finally{try{

socket.close();

}catch(IOException e2) {

threadcount--;

}

}

}

}public classMultiJabberClient {static final int MAX_THREADS = 40;public static void main(String[] args) throwsIOException,

InterruptedException {

InetAddress addr= InetAddress.getByName(null);while (true) {if (JabberClientThread.threadCount()

Thread.currentThread().sleep(100);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值