⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 这是按照一些说明说
💻 JAVA
字号:
/*
 * Client.java
 *
 * Created on 2007年6月8日, 下午2:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.luhan.read;

import java.net.*;
import java.awt.event.*;
import java.io.*;
/**
 *
 * @author Administrator
 */
public class Client implements Runnable {
    BufferedReader in;
    PrintWriter out;
    Socket socket;
    
    final static String ADDRESS_IP_NUMBER = "192.168.1.102";
    final static int PORT_NUMBER = 10001;
    /**
     * Creates a new instance of Client
     */
    public Client() {
    }
   
    public void run(){
        init();
           this.sayToServer(new InputStreamReader(System.in).toString());
           System.out.println(getFromServer()); 

    }
    private void init(){
        try{
        socket = new Socket(ADDRESS_IP_NUMBER,PORT_NUMBER);
        }catch(IOException e){
           // e.printStackTrace();
            System.out.println("建立Socket时有误!");
        }
        try{
        in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
        out = new PrintWriter(socket.getOutputStream(),true); 
        }catch(IOException er){
            System.out.println("建立缓存时错误");
            er.printStackTrace();
        }catch(NullPointerException er){
            System.out.println("无法输入输出,因为无法建立连接!");
        }
    }
    
    private void sayToServer(String str){
        try{
        out.print(str);
        }catch(NullPointerException er){
            System.out.println("无法输出,因为无法建立");
        }
    }
    
    private String getFromServer(){
       String str = "";
       int data = 0;
       try{  
           str = in.readLine();
       }catch(Exception eer){
           System.out.println("读取时错误!");
           eer.printStackTrace();
       }
        return str;
    }

    private boolean Ready(){
        boolean B = false;
        try{
           B = in.ready();
        }catch(IOException er){
            System.out.println("准备读取时出现错误!");
            er.printStackTrace();
        }catch(NullPointerException er){
            System.out.println("无法读取,因为无法建立连接!");
        }
        return B;
    }
    
    public void Close(){
        try{
        in.close();
        out.close();
        }catch(IOException ee){
            System.out.println("关闭输入输出流时出现错误!");
            ee.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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