创建一个允许通过线程和Java进行多个连接的套接字服务器 [英] Creating a socket server which allows multiple connections via threads and Java

查看:59
本文介绍了创建一个允许通过线程和Java进行多个连接的套接字服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整我的简单套接字服务器,以便通过多线程可以有多个TCP连接,但我似乎无法让它工作。到目前为止我的代码如下,我不确定从哪里开始:

I am trying to adapt my simple socket server so that it can have multiple TCP connections, via multithreading, but I can't seem to get it to work. My code so far is as follows, I'm not really sure where to go from here :

import java.net.*;
import java.io.*;

public class DoSomethingWithInput implements Runnable {
   private final Socket clientSocket; //initialize in const'r
   public void run() {


     BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String nextline;
        while ((nextline = in.readLine())!=null) {
           System.out.println(nextline);
        } //... close socket, etc.
    }
}


public class Socket{

  public Socket() {
}
@Override
public void run() {
  try {
    ServerSocket serverSocket = null;
    serverSocket = new ServerSocket(5432);
    for (;;) {
      ServerSocket serverSocket = null;
      serverSocket = new ServerSocket(5432);
      for (;;) {
        Socket clientSocket = null;
        clientSocket = serverSocket.accept();
        //delegate to new thread
        new Thread(new DoSomethingWithInput(clientSocket)).start();
      }
    }
  }catch (IOException e) {
   System.err.println("Could not listen on port: 5432.");
   System.exit(1);
}
}
}

任何人都可以给予关于如何做到这一点的一些指示,以及为什么我当前的实现不起作用?我正在浏览Java教程中的提示 http:// download这里有.oracle.com / javase / tutorial / networking / sockets / examples / KKMultiServerThread.java ,但是他们在这里给出的例子似乎使用了许多外部源代码和类,如KnockKnockProtocol等。

Would anyone be able to give me some pointers on how I could do this, and why my current implementation will not work ? I was running through the tips in the Java tutorial http://download.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServerThread.java here, but the example they give here seems to use a lot of outside sources and classes like KnockKnockProtocol etc etc.

有人能帮我解决这个问题吗?

Would anyone be able to help me out with this?

非常感谢你!

推荐答案

问题是目前你正在接受连接,但是在它关闭之前立即对它进行阻塞读取:

The problem is that currently you're accepting the connection, but then immediately performing blocking reads on it until it's closed:

// After a few changes...
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
     clientSocket.getInputStream()));
String nextLine;
while ((nextLine = in.readLine()) != null) {
    System.out.println(nextline);
}

这意味着接受连接的同一个线程试图处理连接。这不会让你同时使用多个连接。

That means the same thread which accepts the connection is trying to handle the connection. That won't let you use multiple connections at the same time.

相反,创建一个类(例如 ConnectionHandler )实现 Runnable ,并有一个构造函数采用 Socket 。它的运行方法应该处理连接。然后将代码更改为:

Instead, create a class (e.g. ConnectionHandler) which implements Runnable, and has a constructor taking a Socket. Its run method should handle the connection. Then change your code to:

Socket clientSocket = serverSocket.accept();
Runnable connectionHandler = new ConnectionHandler(clientSocket);
new Thread(connectionHandler).start();

这将让你的主线程可以等待下一次连接。

That will leave your "main" thread free to wait for the next connection.

(顺便说一下, KnockKnockProtocol 类并不是真正的外部 - 这是示例的一部分。他们只是没有非常清楚来源在这里 .. 。)

(By the way, the KnockKnockProtocol class isn't really "external" - it's part of the example. They just didn't make it very clear that the source is here...)

这篇关于创建一个允许通过线程和Java进行多个连接的套接字服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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