Java Network Programming (ECHO case column)

Source: Internet
Author: User

Server-side


Package org.tcp;



Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.PrintStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;


public class Echoserver {
public static void Main (string[] args) throws exception{
ServerSocket server = null;
Socket client = null;
PrintStream out = null;
BufferedReader buf = null;
Server = new ServerSocket (8888);
Boolean F = true;
while (f) {
SYSTEM.OUT.PRINTLN ("The server is running, wait for the client link.") ");
Client = Server.accept ();
BUF = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
out = new PrintStream (Client.getoutputstream ());
Boolean flag = true;
while (flag) {
String str = buf.readline ();
if (str = = null| | "". Equals (str)) {
Flag = false;
}else{
if ("Bye". Equals (str)) {
Flag = false;
}else{
Out.println ("ECHO:" +str);
}
}
}
Out.close ();
Client.close ();
}
Server.close ();
}

}


Program Run Result: server running, waiting for client link.



Client


Package org.tcp;


Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.PrintStream;
Import Java.net.Socket;


public class Echoclient {
public static void Main (string[] args) throws exception{
Socket client = null;
Client = new Socket ("localhost", 8888);
BufferedReader buf = null;
PrintStream out = null;
BufferedReader input = null;
input = new BufferedReader (new InputStreamReader (system.in));
out = new PrintStream (Client.getoutputstream ());
BUF = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
Boolean flag = true;
while (flag) {
System.out.print ("Input information:");
String str = input.readline ();
Out.println (str);
if ("Bye". Equals (str)) {
Flag = false;
}else{
String echo = Buf.readline ();
System.out.println (Echo);
}
}
Client.close ();
Buf.close ();
}
}


Program Run Result: input information: Chen Fei Peng
ECHO: Chen Fei Peng
Input information: Chenfeipeng
Echo:chenfeipeng
Input information: Hello
Echo:hello
Input information: bye

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.