Java implementation of a simple echo server __java

Source: Internet
Author: User
Package midi.socket;

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.ServerSocket;
Import Java.net.Socket;
Import Java.util.Scanner;

public class EchoServer14 ... {

/** *//**
* @param args
*/
public static void Main (string[] args) ... {

Socket socket = NULL;
Try ... {
ServerSocket serversocket = new ServerSocket (8189);

System.out.println ("Server'll start ...");
Socket = Serversocket.accept ();

System.out.println ("Server Listening at 8189");

InputStream instream = Socket.getinputstream ();
OutputStream OutStream = Socket.getoutputstream ();

BufferedReader br = new BufferedReader (new InputStreamReader (instream));

PrintWriter out = new PrintWriter (OutStream);

Out.write ("Welcome to the Echo server of MIDI");
Out.write ("Exit service Please enter BYE");
Out.flush ();
Boolean done = false;
String line = null;
while (!done && (Line=br.readline ())!=null) ... {

Out.write ("Echo:" +line+ ");
Out.write ("Please enter the command ...)." ");
Out.flush ();
if (Line.trim (). Equals ("BYE"))
Done=true;
}

The following is the use of scanner in jdk1.5
/**//*
Scanner Scanner = new Scanner (instream);
PrintWriter out = new PrintWriter (OutStream);

Out.write ("Welcome to the Echo server of MIDI");
Out.write ("Exit service Please enter BYE");
Out.flush ();
Boolean done = false;

while (!done && scanner.hasnextline ()) {
String line = Scanner.nextline ();
Out.write ("Echo:" +line+ ");
Out.write ("Please enter the command ...)." ");
Out.flush ();
if (Line.trim (). Equals ("BYE"))
Done=true;
}
* */


catch (IOException e) ... {

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.