Socket read input stream

Source: Internet
Author: User
Tags flush

Today wrote a simple program, this machine and the server through the socket communication, this machine send native IP to the server, and then the server returned information to the machine, but the machine has not received, if the server that the program closed, this side can receive, really dizzy. Don't know what's going on.

Feel that your code should be fine, but Liang said he should have no problem there. Later sent a post to consult, said may be VB there is no add line break or no flush and so on, and then let Liang that the VB program plus line break, testing, hehe, really received.

The native code is as follows:

 public class PC {public static void main (string[] args) {String remote = ' 192.168.1.5 '; try {//Get native IP inetaddre SS addr = Inetaddress.getlocalhost (); String Localhostip = addr.gethostaddress (); Socket socket = new socket (remote,1011); Send native IP to remote host outputstream OS = Socket.getoutputstream (); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (OS)); Bw.write (Localhostip); Bw.flush (); Accept the instruction sent by the host inputstream is = Socket.getinputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (IS)); String line = null; while ((Line=br.readline ())!= null) {SYSTEM.OUT.PRINTLN ("the message Received is:" +line);/** * Below start the relevant steps according to the IR number, if the projector is playing the content, When the next IR trigger causes the projector to replay, a delay should be set. *//If VB send the message of the code does not add line breaks, then this side can still be obtained, the following methods//byte[] buf = new byte[1024]; Is.read (BUF); String msg = new String (buf, "gb2312"); Br.close (); Is.close (); Bw.close (); Os.close (); Socket.close (); catch (Exception e) {System.out.println ("main exception:" +e.getmessage ());} }

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.