Questions about Java sockets

Source: Internet
Author: User
Problem
To do such a thing:

Client Access server IP and port, the server immediately send welcome message, then the client based on welcome message to determine whether the server and port to connect,

If so, start sending the data, including the start and end checksums. The server side sends the welcome message and begins to receive data until the end. It's a pretty simple thing.

CLIENT:

Package com.ces.mytest.socket;

Import Java.net.*;import java.io.*;

public class Clientthread extends Thread {private String ipstr = "";    private int port;        Public Clientthread (String ipstr, int port) {this.ipstr = Ipstr;    This.port = port; }

public void Run () {try {socket client = new socket (ipstr, port);            BufferedReader read = new BufferedReader (New InputStreamReader (Client.getinputstream ())); BufferedWriter write = new BufferedWriter (New OutputStreamWriter (Client.getoutputstream ()));

if ("Welcome". Equals (Read.readline ()) {System.out.println ("checksum is correct");

Write.write ("Start");                Write.newline ();                Write.write ("Data");                Write.newline ();                Write.write ("End");                Write.newline (); Write.flush ();

SYSTEM.OUT.PRINTLN ("Send Complete"); }

Write.close ();            Read.close ();        Client.close ();        catch (Unknownhostexception ex) {ex.printstacktrace ();        catch (IOException ex) {ex.printstacktrace (); }    }

public static void Main (string[] args) {Thread client = new Clientthread ("127.0.0.1", 10020);    Client.start (); }

}

SERVER:

Package com.ces.mytest.socket;

Import Java.net.*;import java.io.*;

public class Serverthread extends Thread {private int port;    public serverthread (int port) {this.port = port; }

Private Boolean stop = false;

    public void Run () {        try {             serversocket Server = new ServerSocket (port);             while (!stop) {                 Socket client = server.accept ();                 BufferedReader read = new BufferedReader (New InputStreamReader ( Client.getinputstream ());                 BufferedWriter write = new BufferedWriter (New OutputStreamWriter (Client.getoutputstream ());                                  System.out.println ("Send a welcome message");    & nbsp;                             Write.write ("Welcome");                 Write.flush ();

                String linestr = Read.readline ();//                if ("Start". Equals (LINESTR)) {//                     System.out.println ("Start accepting data!") ");////                     while (true) {//                         linestr = Read.readline ();//                         if ("End". Equals (LINESTR)) {//                             break;//                        } else {//                              System.out.println ("Data is" + linestr);//                        }//                    }//                }

Write.close ();                Read.close ();            Client.close (); }

catch (IOException ex) {ex.printstacktrace (); }    }

public static void Main (string[] args) {Thread server = new Serverthread (10020);    Server.start (); }

public Boolean isstop () {return stop; }

public void Setstop (Boolean stop) {this.stop = stop; }}

As long as I remove the annotation of the Serverthread class string linestr = Read.readline (), the program definitely goes wrong, what's the problem? Analysis found that serverthread output stream must wait for write.close () execution before sending, and Read.readline () program will stop there, the so-called block procedures, can be referenced on the Write.flush () can send Ah, Do not know how to do, so my handshake agreement how to do ah??


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.