• java--socket实现一个client像还有一个client发送信息


    首先建立接收方socket服务端

    package com.haier.socket.server;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    import org.apache.log4j.Logger;
    
    import com.haier.socket.PolicyServer;
    
    public class TVServer implements Runnable {
    	private Logger logger = Logger.getLogger(this.getClass().getName());
    
    	private Socket client;
    
    	public TVServer(Socket client) {
    		this.client = client;
    	}
    
    	public void run() {
    		logger.info("TV:client对象:" + client);
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(
    					client.getInputStream()));
    			String flag = "aa";
    			logger.info("TV:flag :" + flag);
    
    			// 获取TV端SOCKET连接,将数据传给TV端
    			PolicyServer.tvSocketMap.put(flag, client);
    		} catch (Exception e) {
    			logger.info("error in the close the socket!");
    			e.printStackTrace();
    		} finally {
    
    		}
    	}
    }
    

    再创建发送socket服务端

    package com.haier.socket.server;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import org.apache.log4j.Logger;
    
    import com.haier.bean.PolicyBean;
    import com.haier.json.PolicyJson;
    import com.haier.socket.PolicyServer;
    
    public class WEBServer implements Runnable {
    	private Logger logger = Logger.getLogger(this.getClass().getName()); 
    	
    	private Socket WebSocket;
    
    	public WEBServer(Socket client) {
    		this.WebSocket = client;
    	}
    
    	public void run() {
    		logger.info("web:client对象:"+ WebSocket);
    		Socket tvSocket = null;
    		try {
    			// web端
    			BufferedReader webIn = new BufferedReader(new InputStreamReader(
    					WebSocket.getInputStream()));
    			
    			while(true){
    	 			String webStr = webIn.readLine();
    	 			if("bye".equals(webStr)){
    	 				break;
    	 			}
    				PrintWriter webOut = new PrintWriter(WebSocket.getOutputStream());
    				webOut.println("收到信息...");
    				webOut.flush();
    				
    //				PolicyBean policyBean = PolicyJson.getMacJson(webStr);
    				String key = "aa";
    				logger.info("key: " + key + ",web:" + webStr);
    				
    				// 获取TV端SOCKET连接,将数据传给TV端
    				tvSocket = PolicyServer.tvSocketMap.get(key);
    				PrintWriter tvOut = new PrintWriter(tvSocket.getOutputStream());
    				tvOut.println(webStr);
    				tvOut.flush();
    			}
    			
    			tvSocket.close();
    			WebSocket.close();
    		} catch (Exception e) {
    			logger.info("error in the close the socket!");
    			e.printStackTrace();
    		} finally {
    
    		}
    	}
    }
    

    创建两个測试客户端,一个接受、一个发送client

    package com.test;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class ClientWEB {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    
    		Socket client = new Socket("localhost", 9001);
    
    		BufferedReader in = new BufferedReader(new InputStreamReader(
    				client.getInputStream()));
    		PrintWriter out = new PrintWriter(client.getOutputStream());
    
    		BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
    		while (true) {
    			String str = wt.readLine();
    			// String str = "jonathan";
    			out.println(str);
    			out.flush();
    			System.out.println("client2:" + in.readLine());
    		}
    	}
    
    }
    

    package com.test;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class ClientTV {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    
    		Socket client = new Socket("localhost", 9000);
    
    		BufferedReader in = new BufferedReader(new InputStreamReader(
    				client.getInputStream()));
    		PrintWriter out = new PrintWriter(client.getOutputStream());
    
    		while (true) {
    			String str = in.readLine();
    			System.out.println("client1:" + str);
    			// String str = "jonathan";
    		}
    	}
    
    }
    


  • 相关阅读:
    element表单中一个elformitem下多个formitem项校验(循环校验)
    vscode配置
    git push时提示错误 sign_and_send_pubkey: no mutual signature supported
    syncthing文件同步网盘配置
    MQTT服务搭建和简单使用
    python脚本避免被多次执行
    Hadoop集群对datanode宕机后的处理机制源码阅读
    工作冲突和低谷
    职场的帮助
    测试总体思想
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4519295.html
Copyright © 2020-2023  润新知