Java communication software design2(updating...)
来源:互联网 发布:linux more 查找内容 编辑:程序博客网 时间:2024/06/13 21:18
Client Server Broadcasting String and Track the Client
Provider.java:
import java.io.*;import java.net.*;public class Provider{ ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; ObjectInputStream in; String message; Provider(){} void run() { try{ //1. creating a server socket providerSocket = new ServerSocket(2004, 10); //2. Wait for connection System.out.println("Waiting for connection"); connection = providerSocket.accept(); System.out.println("Connection received from " + connection.getInetAddress().getHostName()); //3. get Input and Output streams out = new ObjectOutputStream(connection.getOutputStream()); out.flush(); in = new ObjectInputStream(connection.getInputStream()); sendMessage("Connection successful"); //4. The two parts communicate via the input and output streams do{ try{ message = (String)in.readObject(); System.out.println("client>" + message); if (message.equals("bye")) sendMessage("bye"); } catch(ClassNotFoundException classnot){ System.err.println("Data received in unknown format"); } }while(!message.equals("bye")); } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); providerSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); System.out.println("server>" + msg); } catch(IOException ioException){ ioException.printStackTrace(); } } public static void main(String args[]) { Provider server = new Provider(); while(true){ server.run(); } }}
Requester.java:
import java.io.*;import java.net.*;public class Requester{ Socket requestSocket; ObjectOutputStream out; ObjectInputStream in; String message; Requester(){} void run() { try{ //1. creating a socket to connect to the server requestSocket = new Socket("localhost", 2004); System.out.println("Connected to localhost in port 2004"); //2. get Input and Output streams out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); in = new ObjectInputStream(requestSocket.getInputStream()); //3: Communicating with the server do{ try{ message = (String)in.readObject(); System.out.println("server>" + message); sendMessage("Hi my server, I am your Client"); sendMessage("I want to Send you some Data"); sendMessage("Please accept my Data through the Socket"); sendMessage("Thank you"); message = "bye"; sendMessage(message); } catch(ClassNotFoundException classNot){ System.err.println("data received in unknown format"); } }while(!message.equals("bye")); } catch(UnknownHostException unknownHost){ System.err.println("You are trying to connect to an unknown host!"); } catch(IOException ioException){ ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); requestSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); System.out.println("client>" + msg); } catch(IOException ioException){ ioException.printStackTrace(); } } public static void main(String args[]) { Requester client = new Requester(); client.run(); }}
the testing result's printscreen:
- Java communication software design2(updating...)
- Java communication software design 3(updating....)
- Java Communication design(Updating)
- Software Deployment, Updating, and Patching
- Communication on Agile Software Projects
- OpenSourse Software 不使用盗版软件 updating...
- Communication skill in the software test
- Java-to-Javascript Communication
- java process communication
- Communication
- Communication
- Communication
- flash communication with java server
- serial port communication by java
- java之Secure communication terminology
- Java Software Websites
- Java Basic (J2SE) tips keep updating
- Sum up - Java Thread Interview Questions (Updating)
- 黑马老学员的建议
- 手工安装 grunt.js/phantomJs/grunt-jasmine-runner 一些经验
- Android Color 定义方法
- 为动态创建的控件添加事件绑定
- Access数据库注入高级玩法
- Java communication software design2(updating...)
- 开源java swing外观
- 使用X-UA-Compatible来设置IE浏览器兼容模式
- Android之TabHost组件美化
- Lucene 基础理论
- 常用正则表达式
- django 自定义 admin的list_filter
- SetWindowsHookEx详解
- mongoDB 入门指南、示例(推荐--01)