First set up the receiver socket server
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 ());p rivate Socket client;public Tvserver (Socket client) {this.client = client;} public void Run () {Logger.info ("Tv:client object:" + client); try {bufferedreader in = new BufferedReader (New InputStreamReader (Client.getinputstream ())); String flag = "AA"; Logger.info ("Tv:flag:" + flag);//Get TV-end socket connection, transmit data to TV-side PolicyServer.tvSocketMap.put (flag, client) ;} catch (Exception e) {logger.info ("error in the close the socket!"); E.printstacktrace ();} Finally {}}}
Create a Send socket server again
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 object:" + WebSocket); Socket Tvsocket = null;try {//web-side 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 ("Receiving information ..."); Webout.flush ();// Policybean Policybean = Policyjson.getmacjson (WEBSTR); String key = "AA"; Logger.info ("Key:" + key + ", Web:" + webstr);//Get the TV-side socket connection and pass the data to the TV-side 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 {}}}
Create two test client, one accept, one send 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 {Socke T 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 ();//Stri ng 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 implement a client like there's a client sending a message