echoclientexample.java

来自「java 完全探索的随书源码」· Java 代码 · 共 52 行

JAVA
52
字号
import java.net.*;import java.io.*;public class EchoClientExample{  public static final String HOST = "www.gatech.edu";  public static final int PORT = 7;  // Default Constructor  public EchoClientExample()  {    super();  }  public void testEcho()  {    try    {      // Get connected to an echo server somewhere, they are usually on      // port 7 of a well known server      Socket socket = new Socket( HOST, PORT );      // Create a reader and stream for reading the data from the echo server      BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream()));      // Create a writer and stream for writing to the echo server      PrintWriter output = new PrintWriter( socket.getOutputStream(), true);      // Send some text to the echo server      System.out.println( "Sending Hello to the echo server" );      output.println( "Hello" );      // Get the message from the echo server      System.out.println( input.readLine() + " from the echo server at " + HOST );      // Close the socket      socket.close();    }    catch(UnknownHostException ex)    {      System.err.println("Unknown host: " + ex);      System.exit(1);    }    catch(IOException excpt)    {      System.err.println("Failed I/O: " + excpt);      System.exit(1);    }  }  public static void main(String[] args)  {    EchoClientExample example = new EchoClientExample();    example.testEcho();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?