思路:服务端接到请求,分发给多线程任务,返回时间给客户端
Bioserver服务端
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class BioServer { /** * 端口号 */ private static final int PORT = 8080; public static void main(String[] args) throws IOException { ServerSocket server = null; try { server = new ServerSocket(PORT); System.out.println("the time server is start in port : " + PORT); Socket socket = null; while (true){ socket = server.accept(); new Thread(new TimeServerHandler(socket)).start(); } }catch (Exception e){ e.printStackTrace(); }finally { if(server!=null){ System.out.println("the time server close"); server.close(); } } } }
多线程时间任务
import java.io.*; import java.net.Socket; import java.util.Date; public class TimeServerHandler implements Runnable { private Socket socket; public TimeServerHandler(Socket socket){ this.socket = socket; } @Override public void run() { BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())) ; out = new PrintWriter(this.socket.getOutputStream(), true); String body = null; while ((body = in.readLine())!=null && body.length()!=0){ System.out.println("the time server receive msg :" + body); out.println(new Date().toString()); } }catch (Exception e){ e.printStackTrace(); }finally { if(in!=null){ try { in.close(); }catch (Exception e){ e.printStackTrace(); } } if(out!=null){ try { out.close(); }catch (Exception e){ e.printStackTrace(); } } if(this.socket != null){ try{ this.socket.close(); }catch (Exception e){ e.printStackTrace(); } } } } }
BioCenlit客户端
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class BioClient { private static final int PORT = 8080; private static final String HOST = "127.0.0.1"; public static void main(String[] args){ Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket(HOST, PORT); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); out.println("I am Client"); String resp = in.readLine(); System.out.println("当前服务器时间是:"+resp); }catch (Exception e){ e.printStackTrace(); }finally { if(in != null){ try { in.close(); }catch (Exception e){ e.printStackTrace(); } } if(out != null){ try { out.close(); }catch (Exception e){ e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
相关推荐
基于C#开放式TCP通信建立与西门子PLC的socket通信示例
Android应用与framework的socket通信示例
Java BIO Socke示例,从简单socket到采用线程池处理socket
android 与 PC socket 通信,本人亲测可以发送及接收,注意如果使用广域网的端口映射。
JAVA Socket通信示例,包含单工、半双工、全双工通信示例.zip
socket的基本应用,针对初学者,老鸟们可以掠过。本示例采用Delphi2010编写
这是android socket通信的实例程序,具体请参考: http://blog.csdn.net/htttw/article/details/7574372
聊天室的网络通信,socket实现,源码中采用127.0.0.1:9000,用时先开服务端,再开客户端
针对初学者简单的API SOCKET通讯
android studio Socket客户端服务器通信 TCP/IP通信android studio Socket客户端服务器通信 TCP/IP通信android studio Socket客户端服务器通信 TCP/IP通信
Android中socket通信简单实现 socket通信需要有一个服务器和客户端,可以把同一个APP作为服务器跟客户端,也可以分开成两个APP。 先上个图: 这里以一个APP作为服务器跟客户端为示例 1、添加网络访问权限 2、写...
C# socket同步和异步通信示例,vs2008 有同步的服务器和客户端 有一部的服务器和客户端
Android Wifi热点数据传输Socket 通信示例源码
在计算机通信领域,socket 被翻译为“套接字”,它是计算机之间进行通信的一种约定或一种方式。通过 socket 这种约定,一台计算机可以接收其他计算机的数据,也可以向其他计算机发送数据。 socket 的典型应用就是 ...
Java中Socket通信-服务端和客户端双向传输字符串实现示例代码。在计算机通信领域,socket 被翻译为“套接字”,它是计算机之间进行通信的一种约定或一种方式。通过 socket 这种约定,一台计算机可以接收其他计算机的...
QT的SOCKET通信实例,基于TCP通信协议,关于具体的实现机制请参考:http://blog.csdn.net/bailang_zhizun/article/details/78327974
delphi socket通信编程示例,有客户端和服务端
这是android socket通信的实例程序,具体请参考: http://blog.csdn.net/htttw/article/details/7574409
模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo模拟binder通信的demo...
基于C#与WPF开发的简单Socket通信示例