Socket Programming 1-echoclient

Source: Internet
Author: User

The talk () method continuously reads the string that the user entered from the console, sends it to echoserver, and prints the string returned by Echoserver to the console. If the user enters a string of "bye", it ends the communication with Echoserver and closes the socket connection.

Package Chapter1;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.Socket;

public class Echoclient {
String host = "localhost";
int port = 8000;
Socket socket;

    public static void Main (string[] args) {
        try {
            new Echoclient (). talk ();
       } catch (Exception e) {
             System.out.println ("Access exception");
       }
   }

Public Echoclient () {
try {
Socket = new Socket (host, port);
SYSTEM.OUT.PRINTLN ("Client program start Access");
SYSTEM.OUT.PRINTLN ("Client Talk, Port number:" + socket.getlocalport ());
} catch (IOException e) {
System.out.println ("Unable to access specified port number" + port);
}
}

public void Talk () {
try {
if (socket! = NULL) {
System.out.println ("----------------\ n" + "Please Enter transmission information:");
Read to keyboard input characters via input stream
InputStreamReader sysin = new InputStreamReader (system.in);
BufferedReader localreader = new BufferedReader (sysin);
String msg = Localreader.readline ();
Using the output stream to the server-side socket
PrintWriter pw = getwriter (socket);
PW.PRINTLN (msg);
Reads the contents returned by the server side through the input stream
BufferedReader br = getreader (socket);
If the server side returns over, the socket is closed, and if you do not return over, continue talk ()
String Response=br.readline ();
if (Response.equals ("Over") | | Response.equals ("Echobye")) {
Socket.close ();
SYSTEM.OUT.PRINTLN ("Receive server-side return \" Over\ ", client closes socket");
} else {
SYSTEM.OUT.PRINTLN ("Get Server-side return:" + response);
Talk ();
}
}
} catch (IOException e) {
System.out.println ("Talk Method exception");
}
}

Private BufferedReader Getreader (socket socket) throws IOException {
InputStream socketin = Socket.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (Socketin));
return BR;
}

Private PrintWriter getwriter (socket socket) throws IOException {
OutputStream socketout = Socket.getoutputstream ();
PrintWriter pw = new PrintWriter (Socketout, true);
return PW;
}
}

Socket Programming 1-echoclient

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.