socket programming multiple client to one server
来源:互联网 发布:linux 添加用户组权限 编辑:程序博客网 时间:2024/05/16 18:24
public class ThreadedEchoServer { static final int PORT = 1978; public static void main(String args[]) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(PORT); } catch (IOException e) { e.printStackTrace(); } while (true) { try { socket = serverSocket.accept(); } catch (IOException e) { System.out.println("I/O error: " + e); } // new threa for a client new EchoThread(socket).start(); } }}
public class EchoThread extends Thread { protected Socket socket; public EchoThread(Socket clientSocket) { this.socket = clientSocket; } public void run() { InputStream inp = null; BufferedReader brinp = null; DataOutputStream out = null; try { inp = socket.getInputStream(); brinp = new BufferedReader(new InputStreamReader(inp)); out = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { return; } String line; while (true) { try { line = brinp.readLine(); if ((line == null) || line.equalsIgnoreCase("QUIT")) { socket.close(); return; } else { out.writeBytes(line + "\n\r"); out.flush(); } } catch (IOException e) { e.printStackTrace(); return; } } }}
Client
// A simple Client Server Protocol .. Client for Echo Serverimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;public class NetworkClient {public static void main(String args[]) throws IOException{ InetAddress address=InetAddress.getLocalHost(); Socket s1=null; String line=null; BufferedReader br=null; BufferedReader is=null; PrintWriter os=null; try { s1=new Socket(address, 4445); // You can use static final constant PORT_NUM br= new BufferedReader(new InputStreamReader(System.in)); is=new BufferedReader(new InputStreamReader(s1.getInputStream())); os= new PrintWriter(s1.getOutputStream()); } catch (IOException e){ e.printStackTrace(); System.err.print("IO Exception"); } System.out.println("Client Address : "+address); System.out.println("Enter Data to echo Server ( Enter QUIT to end):"); String response=null; try{ line=br.readLine(); while(line.compareTo("QUIT")!=0){ os.println(line); os.flush(); response=is.readLine(); System.out.println("Server Response : "+response); line=br.readLine(); } } catch(IOException e){ e.printStackTrace(); System.out.println("Socket read Error"); } finally{ is.close();os.close();br.close();s1.close(); System.out.println("Connection Closed"); }}}
0 0
- socket programming multiple client to one server
- C Socket Programming server client
- [Linux] Enable ssh key authentication from one user on client to multiple users on server
- How to Run Multiple Tomcat Instances on One Server?
- Socket Programming : How to Integrate Socket.io java Client in your Android App without Maven
- java socket client to python server with utf-8
- Socket Server Client Sample
- python socket-server-client
- epoll - socket-server-client
- socket ---> server and client
- socket Client&Server
- TCP Socket Server Client
- Socket (Client & Server)
- socket:server-client实例
- Java Socket Programming in Client/Server Applications - 转自 http://www.developer.com/
- C Socket Programming for Linux with a Server and Client Example Code
- java socket client-server-client小例子
- c# socket(server and client)
- Python图表绘制:matplotlib绘图库入门
- Andriod跳转小记
- C++中的容器类详解
- SET专题
- layer父子页面交互
- socket programming multiple client to one server
- MongoDB 并发机制
- Android系统启动流程二之linux kernel
- java排序之冒泡排序
- 在 Android Studio 2.2 中愉快地使用 C/C++
- 百度一面
- Hololens 学习摘要及翻译记录 七 Spatial mapping
- BIEE中应用echarts代码
- [学习笔记] - Ubuntu 下安装MongoDB