Java--socket implements a client that sends information like another client

Source: Internet
Author: User

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 clients, 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 implements a client that sends information like another client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.