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");
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??
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