标签: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 { } } }
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 { } } }
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实现一个客户端像另一个客户端发送信息
标签:socket
原文地址:http://blog.csdn.net/gaopeng0071/article/details/40108267