集册 Java实例教程 使用套接字创建套接字客户端

使用套接字创建套接字客户端

欢马劈雪     最近更新时间:2020-01-02 10:19:05

397
使用套接字创建套接字客户端
/**
N o w J a v a . c o m 提供 
**/

import java.io.IOException;

import java.io.PrintWriter;

import java.net.InetAddress;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.ArrayList;

import java.util.Scanner;


public class Main {

  public static void main(String[] args) {

    int port = 1234;


    System.out.println("Welcome to the Bart Client\n");


    Socket s = getSocket(port);
/**来 自 n o w j a   v  a . c o m - 时  代  Java**/

    try {

      System.out.println("Connected on port " + port);


      Scanner in = new Scanner(s.getInputStream());

      PrintWriter out;

      out = new PrintWriter(s.getOutputStream(), true);


      // discard the welcome message

      in.nextLine();


      // discard the exit instructions

      in.nextLine();


      // get a quote

      out.println("get");

      String quote = in.nextLine();


      // disconnect from the server

      out.println("bye");

      s.close();


      // write the quote on the chalkboard

      for (int i = 0; i < 20; i++)

        System.out.println(quote);

    } catch (Exception e) {

      e.printStackTrace();

    }

  }


  private static Socket getSocket(int port) {

    Socket s;

    String host;

    InetAddress ip;


    Scanner sc = new Scanner(System.in);


    while (true) {

      System.out.print("What server do you want to connect to?");

      host = sc.nextLine();

      try {

        ip = InetAddress.getByName(host);

        s = new Socket(ip, port);

        return s;

      } catch (UnknownHostException e) {

        System.out.println("The host is unknown.");

      } catch (IOException e) {

        System.out.println("Network error.");

      }

    }

  }

}


class Message {

  
展开阅读全文