首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA socket编程范例

2012-07-01 
JAVA socket编程实例http://daoyongyu.iteye.com/blog/265677import java.net.*import java.io.*public

JAVA socket编程实例
http://daoyongyu.iteye.com/blog/265677


import java.net.*;import java.io.*;public class MultiUser extends Thread {private Socket client;public MultiUser(Socket c) {this.client = c;}public void run() {try {BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));PrintWriter out = new PrintWriter(client.getOutputStream());// Mutil User but can't parallelwhile (true) {String str = in.readLine();System.out.println(str);out.println("has receive....");out.flush();if (str.equals("end"))break;}client.close();} catch (IOException ex) {} finally {}}public static void main(String[] args) throws IOException {ServerSocket server = new ServerSocket(5678);while (true) {// transfer location change Single User or Multi UserMultiUser mu = new MultiUser(server.accept());mu.start();}}}


我的类直接从Thread类继承了下来.并且通过构造函数传递引用和客户Socket建立了联系.这样每个线程就有了.一个通讯管道.同样我们可以填写run方法.把之前的操作交给线程来完成.这样多客户并行的Socket就建立起来了.

热点排行