使用线程池的客户端/服务器连接? [英] Client/Server connection using a thread Pool?

查看:85
本文介绍了使用线程池的客户端/服务器连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用线程池的客户端/服务器应用程序,以便多个客户端可以同时与服务器通信并向其发送命令:

I am writing a client/server application that uses a threadpool, so that several clients can communicate with the server at the same time and send commands to it:

但是,即使使用非常简单的客户端,它也不起作用:

But it does not work, even with an extremely simple client:

这是我的客户端:

    if(args.length==0) {
        host="127.0.0.1";
        port=11111;
    } else {
        host=args[0];
        String portString = args[1];
        try {
            port = Integer.parseInt(portString);
        } catch(Exception e) {
            e.printStackTrace();
            port = 11111;
        }
    }

    try {
        System.out.println("Client connects to host=" + host + " port=" + port);
        Socket skt = new Socket(host, port);//line 30 in the exception
        BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        PrintWriter myOutput = new PrintWriter(skt.getOutputStream(), true);

        myOutput.println("Hello I am the Client!");
        String buf=myInput.readLine();
        System.out.println(buf + "=buf");


        if(buf!=null) {
            System.out.println("Client receives " + buf + " from the Server!");
            buf=myInput.readLine();
            System.out.println(buf);

        }
        myOutput.close();
        myInput.close();
        skt.close();

但我得到:


客户端连接到主机= 127.0.0.1端口= 11111
java.net.ConnectException:连接被拒绝:在
java.net.DualStackPlainSocketImpl.connect0(本地方法)在$ b $连接b
的java.net.DualStackPlainSocketImpl.socketConnect(未知源)b
的java.net.AbstractPlainSocketImpl.doConnect(未知源)java.net。
的java.net.AbstractPlainSocketImpl.connectToAddress(未知源) .net.AbstractPlainSocketImpl.connect(未知来源)售价
java.net.PlainSocketImpl.connect(未知来源)售价
java.net.SocksSocketImpl.connect(未知来源)售价
java.net
处的.Socket.connect(未知源)java.net.Socket.connect处的(未知源)java.net.Socket处的
java.net.Socket处的(未知源)。 (未知来源)位于
client.Client.main(Client.java:30)//在代码处查看注释

Client connects to host=127.0.0.1 port=11111 java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at client.Client.main(Client.java:30)//look at the comment at the code

所以我我的服务器可能是错误的,因为我无法在客户端中看到它:

so I thought my server is probably the mistake, because I cannot see it in the client:

ServerMain.java

ServerMain.java

public static void main(String[] args) {

    try {
        clientlistener = new ClientListener(server);
        //serversocket = new ServerSocket();
    } catch (IOException e) {
        System.out.println("Could not listen on tcpPort: " + tcpPort);
        e.printStackTrace();
        System.exit(-1);
    } 
}

在这里,我用新的

here i open the clientlistener with a new

private static Server server = new Server();

并想将其添加到serverSocket而不是threadPool:

and want to add it to the serverSocket and than to the threadPool:

public ClientListener(Server server) throws ServerException{
    this.server=server;

     try {
            serverSocket = new ServerSocket(server.getTcpPort());
        } catch (IOException e) {
            throw new ServerException(e.getMessage());
        }

    System.out.println(String.format("Accepting new clients " +
            "on %s", serverSocket.getLocalSocketAddress()));
}

/**
 * Spawns new threads on incoming connection requests from clients.
 * */
@Override
public void run() {
    while(true){
        try {
            ClientCommunicator cc = new ClientCommunicator(serverSocket.accept(), pool);
            threadPool.execute(cc);
            clientComms.add(cc);
        } catch (SocketException e) {
            System.out.println(e.getMessage());
            return;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }
    }
}

ClientCommunicator只是一个类可以从命令行中读取。

The ClientCommunicator is just a class to read from the command line.

我的问题是:

我的套接字连接怎么了?

Whats wron with my socket connection?

我要打开一个并从客户那里接受吗?

I am opening one and accepting it from the client?

我真的很感谢您的回答!!!

I really much appreaciate your answer!!!

推荐答案

我认为您的服务器已关闭,因此您收到的连接被拒绝。我看不到您的server.accept()调用,它使服务器套接字等待新客户端来并返回不是SocketServer的Socket对象。

I think your server is down and that's why you receive a connection refused. I don't see your server.accept() call that makes server socket wait for new clients to come and returns a Socket object not SocketServer.

我已经完成了这段代码为了你。希望对您有所帮助。

I have done this code for you. I hope it helps.

package com.sockets.exam1;

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

public class ServerCode {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(11111);
            while (true) {                                              
                new ThreadSocket(server.accept());
            }
        } catch (Exception e) {
        }
    }
}
class ThreadSocket extends Thread{
    private Socket insocket;    
    ThreadSocket(Socket insocket){
        this.insocket = insocket;
        this.start();
    }       
    @Override
    public void run() {     
        try {
            System.out.println("procesing request");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    insocket.getInputStream()));
            PrintWriter out = new PrintWriter(insocket.getOutputStream(),
                    true);
            String instring = in.readLine();
            out.println("The server got this: " + instring.toUpperCase());
            insocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

这篇关于使用线程池的客户端/服务器连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆