首页 > 代码库 > java--socket实现一个客户端像另一个客户端发送信息
java--socket实现一个客户端像另一个客户端发送信息
首先建立接收方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"; } } }
java--socket实现一个客户端像另一个客户端发送信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。