tryclient.java

来自「用java实现的一个bbs的portal」· Java 代码 · 共 66 行

JAVA
66
字号
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jul 19, 2003
 * Time: 6:13:49 PM
 * To change this template use Options | File Templates.
 */
package ConnectAdapter;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TryClient extends Thread{

    Socket echoSocket;
    PrintWriter out;
    BufferedReader in;
    int selfCount;
    static int count = 0;

    public TryClient(int selfCount){
        this.selfCount = selfCount;
    }

    private void newClient(){
        try{
//创建Socket
            echoSocket = new Socket("162.105.30.254", 2617);
//创建读写对象
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
//利用读写对象,与服务器通信
            out.println("LOGIN%letter%letter");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void run(){
        newClient();
        while(true){
            try{
                String inputLine = in.readLine();
                System.out.println(this.selfCount+" -- Input Message : "+inputLine);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public static void main(String args[]){
        for (int i = 0;i<500;i++){
            count ++;
            TryClient tryClient = new TryClient(count);
            tryClient.start();
            try{
                Thread.sleep(2000);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

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