本站首页    管理页面    写新日志    退出


«November 2025»
1
2345678
9101112131415
16171819202122
23242526272829
30


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7654570
建立时间:2006年5月29日




[J2SE]A multi-threaded socket-based server
软件技术

lhwork 发表于 2006/8/7 10:14:01

The problem is old - How to implement a multi-threaded, socket-based server that will let you read and write to the client (for example a telnet terminal. There are several problems with constructing such a server: You have to use threads because otherwise clients will be queued up waiting for a connection.Many of the publically available examples (even some in some books) do not work for I/O servers. They are mostly developed around reading from a socket not writing to the socket as well. In our example we give a working read/write code example.You need to avoid race conditions when starting up a thread at times when connections are coming through thick and fast. The code below will implement just such a server. It is intended for a head start, so is a template rather than a tutorial on how to write a server in Java. We have decided on port 4444 to listen on in the example, but you need to decide on a suitable port yourself. The server will run, establishing connections from the port and echoing input received until it receives a line of input that is a full stop only ".". Have fun! The Kieser.net team. package sample_server; import java.io.*; import java.net.*; import java.security.*; /**  * Title:        Sample Server  * Description:  This utility will accept input from a socket, posting back to the socket before closing the link.  * It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net  * Copyright:    Copyright (c) 2002  * Company:      Kieser.net  * @author B. Kieser  * @version 1.0  */ public class sample_server {   private static int port=4444, maxConnections=0;   // Listen for incoming connections and handle them   public static void main(String[] args) {     int i=0;     try{       ServerSocket listener = new ServerSocket(port);       Socket server;       while((i++ < maxConnections) || (maxConnections == 0)){         doComms connection;         server = listener.accept();         doComms conn_c= new doComms(server);         Thread t = new Thread(conn_c);         t.start();       }     } catch (IOException ioe) {       System.out.println("IOException on socket listen: " + ioe);       ioe.printStackTrace();     }   } } class doComms implements Runnable {     private Socket server;     private String line,input;     doComms(Socket server) {       this.server=server;     }     public void run () {       input="";       try {         // Get input from the client         DataInputStream in = new DataInputStream (server.getInputStream());         PrintStream out = new PrintStream(server.getOutputStream());         while((line = in.readLine()) != null && !line.equals(".")) {           input=input + line;           out.println("I got:" + line);         }         // Now write to the client         System.out.println("Overall message is:" + input);         out.println("Overall message is:" + input);         server.close();       } catch (IOException ioe) {         System.out.println("IOException on socket listen: " + ioe);         ioe.printStackTrace();       }     } }


阅读全文(1631) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 1.041 second(s), page refreshed 144795838 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号