- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在我正在玩一些客户端-服务器应用程序,例如通过java应用程序与运行服务器应用程序的树莓派进行通信。
好吧,我发现了一个问题,但没有找到可能的解决方案。使用以下代码我可以连接两次到服务器,但在两次连接后它将不再接受任何连接。例如,我第一次启动客户端,一切运行良好。然后我关闭客户端并再次启动它,它又可以工作了。但如果我关闭它并第三次启动它,它将什么也不做。服务器将不接受连接。我在我的专用网络中的不同电脑上进行了尝试,但从未运行第三个连接。
这是我在服务器上运行的代码:
public class Receiver {
private final Logger logger = Logger.getLogger(this.getClass().getName());
private ServerSocket serverSocket;
private boolean isRunning;
public Receiver(int port) {
isRunning = true;
try {
serverSocket = new ServerSocket(port);
logger.log(Level.FINER, "start listening at port " + port);
logger.log(Level.FINER, "established successful.");
} catch (IOException e) {
logger.log(Level.SEVERE, "Error while opening socket:\n" + LogUtil.getStackTrace(e));
System.exit(1);
}
}
/**
* server starts to listen at the specific port
*/
public void listenServer() {
logger.log(Level.FINER, "Server is listening");
while (isRunning) {
try {
final Socket clientsocket = serverSocket.accept();
logger.log(Level.FINER, "Server accepted Connection from " + clientsocket.getInetAddress());
new Thread(new Runnable() {
@Override
public void run() {
handleConnection(clientsocket);
}
}).start();
} catch (IOException e) {
logger.log(Level.SEVERE, "Connection with Client failed.");
}
}
}
/**
* handles the given connection
*
* @param clientSocket
* the given client socket for this connection
*/
private void handleConnection(Socket clientSocket) {
ObjectInputStream instream = null;
ObjectOutputStream outstream = null;
try {
outstream = new ObjectOutputStream(clientSocket.getOutputStream());
instream = new ObjectInputStream(clientSocket.getInputStream());
final RequestProcessor processor = new RequestProcessor();
final InetAddress inetAdress = clientSocket.getInetAddress();
logger.log(Level.FINER, "handle connection from " + inetAdress);
Object inob;
while ((inob = instream.readObject()) != null) {
logger.log(Level.FINER, "received Object from " + inetAdress);
final ObjectOutputStream finalOutputStream = outstream;
final Object finalInob = inob;
new Thread() {
public void run() {
setPriority(MAX_PRIORITY);
Object outob;
try {
outob = processor.processObject(finalInob);
logger.log(Level.FINER, "send Respond to: " + inetAdress + " Error: " + (outob instanceof ErrorMessage));
finalOutputStream.writeObject(outob);
finalOutputStream.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, "Connection closed to " + inetAdress);
}
}
}.start();
}
closeConnection(clientSocket, instream, outstream);
} catch (IOException e) {
logger.log(Level.SEVERE, "Connection closed to " + clientSocket.getInetAddress());
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, "Connection closed to " + clientSocket.getInetAddress());
} finally {
closeConnection(clientSocket, instream, outstream);
}
}
/**
* closes InputStream, OutputStream and socket
*
* @param socket
* @param instream
* @param outstream
*/
private void closeConnection(Socket socket, InputStream instream, OutputStream outstream) {
this.isRunning = false;
if (instream != null) {
try {
instream.close();
} catch (IOException e) {
}
}
if (outstream != null) {
try {
outstream.close();
} catch (IOException e) {
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
logger.log(Level.FINER, "Connection was closed to client " + socket.getInetAddress());
}
/**
* closes all connections and ends the server
*/
public void endAllConnections() {
this.isRunning = false;
if (this.serverSocket != null)
try {
this.serverSocket.close();
} catch (IOException e) {
// do nothing
}
}
}
这是我用来连接到该服务器的客户端代码:
public class SocketConnector implements IConnector {
private final Logger logger = Logger.getLogger(this.getClass().getName());
private Socket s;
private ObjectOutputStream oos;
private ObjectInputStream ois;
/**
* creates a new connection
*
* @param host
* given host
* @param port
* given port
*
* @throws UnknownHostException
* @throws IOException
*/
public SocketConnector(String host, int port) throws UnknownHostException, IOException {
logger.log(Level.FINER, "Establish connection to " + host + ":" + port);
s = new Socket(host, port);
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
}
// some methos which use oos and ois.
有人知道为什么当 2 个客户端连接并断开连接时服务器不再接受任何连接吗?我在谷歌上搜索了很多,但没有找到足够的答案:/服务器日志显示它甚至不接受新连接。
提前致谢:)
最佳答案
情况是:
第一次调用final Socketclientsocket = serverSocket.accept();
时,它正在等待第一个客户端。当第一个客户端连接时,您将此客户端传递给一个线程,然后继续第二次调用 Final Socket clientsocket = serverSocket.accept(); 的循环。由于启动线程比进入下一个循环需要更多时间,因此 isRunning
仍然为 true
。在handleConnection(Socket clientSocket)
中,您调用closeConnection(clientSocket, instream, outstream);
,将isRunning
设置为false
。这就是我想说的。当第二个客户端连接时,您也将此客户端传递给另一个线程,然后继续循环,其中 isRunning
为 false
,因此循环终止。因此,您无法联系第三个客户端。
关于Java ServerSocket 只接受 2 个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146502/
在服务器端,我使用这段代码: ServerSocket server = new ServerSocket(1234); Socket server_socket = server.accept();
假设A是服务器,B是客户端。 B 创建一个到服务器 A 的套接字 b 并 ServerSocket.accept() 创建一个相应的套接字 a 对于客户端 B 现在,如果服务器 A 关闭了 a 但客户
我想在单独的线程中运行Python serversocket 类。为此,我修改了此代码以支持 python 线程: 原始文件 import SocketServer class MyTCPHandle
我正在使用一些 java 创建一个小客户端来监控网络。对于网络上的每个节点,我运行一个客户端,但也运行一个 ServerSocket 来监听传入的连接。我写的代码似乎卡在了 ServerSocket.
虽然 ServerSocket 类被设计为一般监听传入连接,但我可以只使用 Socket 对象来代替并在 java 中检索传入数据吗?如果是,那么我是否需要采取任何额外的预防措施? 编辑:感谢您的回答
我在Java项目的类之一中有一个ServerSocket。该套接字接收一些导致新操作的命令。例如: deleteAllFromDB ->删除数据库上的所有条目。 sendMsgToX ->创建发送到X
我用java写了一个服务器,这里是代码: public mainClass() { try { ss = new ServerSocket(8080);
我目前正在参与学校的一个项目,我们正在构建一个在 Android 手机上使用的通信系统。为此,我们将使用一个向所有客户端打开套接字的服务器,使它们进行通信。 我以前做过几个聊天应用程序,没有任何套接字
我有这两个类 Client.java 和 Server.java,每个类都包含一个 main 方法。想法非常简单,其中的代码也非常相似。我试图实现的目标是打开一个使用 ServerSocket 监听特
这可能是一个愚蠢的问题,但就是这样。我正在编写这个聊天程序,其中有一个服务器和可以连接到它的客户端。我想在程序中实现私有(private)消息传递,但我不知道如何让客户端直接相互连接。对于服务器,我使
ServerSocket 与多个客户端一起使用,我们可能为每个客户端附加单独的线程来工作,但问题是连接工作正常并接受所有客户端,但仅服务最后一个连接。所以这不是问题,或者这是正常现象。 服务器代码:
我是套接字编程的新手,我只是尝试一些东西。我想做的是让客户端读取文本文件,将该文件中的行保存在 ArrayList 中,然后将这些行发送到服务器。这是我的代码。连接成功建立,但是当服务器尝试从他的Bu
我有以下代码片段: if (servsock == null) { System.out.println("HELLO1!"); servsock = new ServerSocket
我正在查看serversocket API,它说允许的最大连接数是50。我尝试测试它.. public class ClientSocketTest { public static void
我试图制作一个小聊天程序,但它真的不想工作。我的服务器和客户端基本相同:它们创建一个新的套接字和一个新的聊天(GUI)。有人可以帮我找出我犯的错误吗? 服务器代码: import java.io.In
我尝试使用蓝牙将图像从一个设备发送到其他设备。为此,我采用 Android 蓝牙聊天应用程序源代码,当我发送字符串时它工作正常。但是如果我将图像作为字节数组发送,则 while 循环不会中断或从输入流
我目前正在为我的大学开发一款 TicTacToe 多线程网络游戏,但遇到了一个恼人的问题。 我希望我的服务器每次接受 2 个连接,然后等待其他 2 个连接等。 问题是,在我的代码中,如果客户端连接到服
我正在构建一个简单的服务器应用程序,我可以在本地网络中连接到它。但我无法通过互联网连接。 这是我的服务器代码: ServerSocket server; try { server = new
我有以下构造: try { serverSocket = new ServerSocket(port); } catch (Exception e) { throw new Runti
我一直在尝试在我创建的应用程序中开发一个嵌入式网络服务器。本质上,我们有创建一个简单网络服务器(使用 ServerSocket)的主进程,然后主进程(理论上)将处理它的业务。 Main(){
我是一名优秀的程序员,十分优秀!