码迷,mamicode.com
首页 > 编程语言 > 详细

Java网络编程(tcp在服务器上应用多线程)

时间:2014-08-16 09:48:20      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   java   os   io   ar   art   line   new   

package org.tcp;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;


public class EchoThread implements Runnable {
private Socket client = null;
public EchoThread(Socket client){
this.client = client;
}
@Override
public void run() {
// TODO 自动生成的方法存根
PrintStream out = null;
BufferedReader buf = null;
try{
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintStream(client.getOutputStream());
boolean flag = true;
while(flag){
String str = buf.readLine();
if(str == null||"".equals(str)){
flag = false;
}else{
if("bye".equals(str)){
flag = false;
}else{
out.println("ECHO:"+str);
}
}
}
out.close();
client.close();
}catch(Exception e){
}
}


}


package org.tcp;


import java.net.ServerSocket;
import java.net.Socket;


public class EchoThreadSocket {
public static void main(String[] args) throws Exception{
ServerSocket server = null;
Socket client = null;
server = new ServerSocket(8888);
boolean f = true;
while(f){
System.out.println("服务器运行,等待客户端链接。");
client = server.accept();
new Thread(new EchoThread(client)).start();
}
server.close();
}
}

Java网络编程(tcp在服务器上应用多线程),布布扣,bubuko.com

Java网络编程(tcp在服务器上应用多线程)

标签:style   java   os   io   ar   art   line   new   

原文地址:http://blog.csdn.net/u012927417/article/details/38601653

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!