client.java

来自「一个小小的客户端程序代码」· Java 代码 · 共 44 行

JAVA
44
字号
//MiniClient.java - simple client for MiniServer  客户端
import java.io.*;
import java.net.*;

class Telnet{
  public static void main (String args[])
         throws java.io.IOException
  { byte b[]=new byte[10];
    if (args.length != 2) {
      System.out.println("提示: " +"使用 Telnet IP PortNum 来测试你的程序 如 Telnet  127.0.0.1  8080");
      System.exit(0);
    }
   Socket sock= null;
    int portnum = Integer.valueOf(args[1]).intValue();
    try {
   sock = new Socket(args[0], portnum);
        }
   catch (IOException e) {
      System.out.println("不能连接: "+args[0]+":"+portnum + ", " + e);
      System.exit(1);
    }
    BufferedReader input = new BufferedReader(
      new InputStreamReader(sock.getInputStream()));
    PrintWriter output = 
      new PrintWriter(sock.getOutputStream());
    System.out.println("连接已经建立");
    System.out.println("请输入讯息" +
        " 并按回车:");
    System.in.read(b); 
    String line = new String(b);
    while (line != null) {
      output.println(line);
      output.flush();
      line = input.readLine();
      System.out.println("服务器返回讯息:" + line);
      System.out.println("请输入讯息 " +
          "并按回车:");
      System.in.read(b); 
      line = new String(b);
    }
  }
}

⌨️ 快捷键说明

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