使用java实现Server和Client(TCP)
2014-09-11 16:52
633 查看
Server.java
Client.java:
import java.io.*; import java.net.*; public class Server { public static final int PORT=8888; public void Server() throws IOException { ServerSocket ss = new ServerSocket(PORT); InetAddress ia = InetAddress.getByName(null); System.out.println("Server@"+ia+" start!"); try{ while(true){ Socket s = ss.accept();// listen PORT; try{ new ServerOne(s); } catch (IOException e) { s.close(); } } }finally{ ss.close(); System.out.println("Server stop!"); } } } class ServerOne extends Thread { private Socket s; private BufferedReader in; private PrintWriter out; public ServerOne(Socket s) throws IOException { this.s = s; in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(new BufferedWriter (new OutputStreamWriter(s.getOutputStream())), true); start(); } public void run(){ try { while(true) { String str = in.readLine(); if(str.equals("end")) break; System.out.println("Server: receive information"+str); out.println("Echo: "+str); } System.out.println("closing...."); } catch (IOException e){ } finally { try{ s.close(); }catch(IOException e){} } } }
Client.java:
import java.io.*; import java.net.*; public class Client extends Thread { static final int MAX_THREADS=25; private static int id = 0; private static int threadCount = 0; private Socket s; private BufferedReader in; private PrintWriter out; public static int getThreadCount(){ return threadCount; } public Client(InetAddress ia){ threadCount++; id++; System.out.println("Making client: " + id); try{ s = new Socket(ia, Server.PORT); } catch (IOException e) {} try{ in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(new BufferedWriter (new OutputStreamWriter(s.getOutputStream())), true); start(); }catch(IOException e1){ try{ s.close(); }catch(IOException e2){ System.out.println("Error in Client\n"); } } } public void run() { try{ String str; for(int i = 0; i < 5; i++) { out.println("Client #"+id+":"+i); str=in.readLine(); System.out.println("Client: send message#"+id+":"+i+"\n" +"Server reply: "+str); } out.println("end"); }catch(IOException e){ }finally{ try { s.close(); } catch (IOException e){} } } public static void main(String args[]) throws IOException, InterruptedException { InetAddress ia = InetAddress.getByName(null); //null mean localhost while(true){ if(getThreadCount() < MAX_THREADS) new Client(ia); else break; Thread.currentThread().sleep(10); } } }
相关文章推荐
- 使用java实现Server和Client(TCP)
- 使用java实现Server和Client(TCP)
- 使用Java实现简单的server/client回显功能的方法介绍
- java用ServerSocket类结合io流实现一对一聊天(java实现简单的TCP聊天程序只能用telnet连接后方可使用)
- 使用Java实现简单的server/client回显功能的方法介绍
- 使用thrift实现了Javaserver和nodejsclient之间的跨平台通信
- java使用socket上传文件,实现server端和client端
- TCP/UDP client/server library for Java, 最好的java语言tcp udp 服务器客户端实现库
- TCP的Server和Client通信—Java实现
- 使用Java实现简单的server/client回显功能的方法介绍
- JAVA中使用FTPClient实现文件上传下载
- Thrift使用入门(2) - 用Thrift实现一个简单的Server/Client应用程序
- Java实现简单的server/client回显功能
- 1ActiveMQ使用 2ActiveMQC#Client实现 3ActiveMQC++Client实现通讯 4 ActiveMQJavaClient实现
- JAVA中使用FTPClient实现文件上传下载
- 使用JAVA实现Socket通信,TCP、UDP简析。
- 【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl与Jetty(Server)实现手机网游Http通信框架(内含解决curl.h头文件找不到问题)
- Java分别实现了Server和Client,把Server和Client run起来后,在客户端输入,
- 【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl与Jetty(Server)实现手机网游Http通信框架(内含解决curl.h头文件找不到问题)
- Java 使用FTPClient实现上传下载