建立简单的服务器端程序
2015-06-17 10:25
337 查看
参考自core java
使用线程实现多个客户端同时访问:
In this program, we spawn a separate thread for each connection. This approach is not satisfactory for highperformance servers. You can achieve greater server throughput by using features of the java.nio package. See www.ibm.com/developerworks/java/library/j-javaio
for more information.
package com.lujinhong.corejava; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class EchoServer { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8019); Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); Scanner sc = new Scanner(is); Boolean flag = false; String line = null; String exitString = "bye"; while(!flag && sc.hasNextLine()){ pw.println("Hello, type " + exitString + " to exit!"); line = sc.nextLine(); if(line.trim().equals(exitString)){ flag = true; }else{ pw.println("Hello, "+line); } } pw.close(); sc.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
使用线程实现多个客户端同时访问:
package com.lujinhong.corejava; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class MultiEchoServer { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8189); while (true) { Socket socket = serverSocket.accept(); Runnable r = new ThreadedEchoHandler(socket); Thread thread = new Thread(r); thread.start(); } } catch (IOException e) { e.printStackTrace(); } } } class ThreadedEchoHandler implements Runnable { private Socket s = null; public ThreadedEchoHandler(Socket socket) { s = socket; } @Override public void run() { try { InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); PrintWriter pw = new PrintWriter(os); Scanner sc = new Scanner(is); Boolean flag = false; String line = null; String exitString = "bye"; while (!flag && sc.hasNextLine()) { pw.println("Hello, type " + exitString + " to exit!"); line = sc.nextLine(); if (line.trim().equals(exitString)) { flag = true; } else { pw.println("Hello, " + line); } } sc.close(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } }
In this program, we spawn a separate thread for each connection. This approach is not satisfactory for highperformance servers. You can achieve greater server throughput by using features of the java.nio package. See www.ibm.com/developerworks/java/library/j-javaio
for more information.
相关文章推荐
- Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)
- 如何防止博客文章被窃取
- PowerDesigner使用
- 日经社説 20150617 企業は株の長期保有をいかに促すべきか
- 《学习OpenCV》第三章课后题2
- how to install the chrome in the Ubuntu platform
- 英语单词解辞集
- 编程面试的10大算法概念汇总
- 读取并分析wgetrc文件
- 使用lstat()判断文件类型
- 使用Subversion进行版本控制 附录A
- 使用C语言读取properties文件V1.0
- oracle之Flash Recovery Area全面介绍
- 连接数据库时提示归档器失败 ORA-00257: archiver error
- GoldenGate系列一:快速搭建单向同步GoldenGate环境
- GoldenGate系统二:快速搭建三方单向通信GoldenGate环境
- GoldenGate系统三:trouble shooting and log
- sqlplus环境配置(login.sql)
- Oracle从软件安装到运行的全流程
- 【转载】linux中误删除oracle数据文件的恢复操作