集册 Java实例教程 从带有套接字的服务器读取

从带有套接字的服务器读取

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

393
从带有套接字的服务器读取

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
/*
时   代     Java  公  众  号 - nowjava.com 提供
*/

import java.io.PrintStream;

import java.net.Socket;


public class Main {

  public static void main(String[] arguments) {

    try (Socket digit = new Socket("127.0.0.1", 79);

        BufferedReader in = new BufferedReader(new InputStreamReader(

            digit.getInputStream()));) {


      digit.setSoTimeout(20000);

      PrintStream out = new PrintStream(digit.getOutputStream());

      out.print("\015\012");

      /**
      n o w j a v a . c o m 提供 
      **/

      boolean eof = false;

      while (!eof) {

        String line = in.readLine();

        if (line != null) {

          System.out.println(line);

        } 
展开阅读全文