关闭套接字问题
Closed socket issue
尝试编写一个简单的回显文本的服务器,需要为其提供多个线程。每当客户端连接时,我都会收到 Socket 关闭。 我不太确定哪里出了问题,而且我才刚刚开始学习这个,所以我不是很了解。如果有人能告诉我出了什么问题,我将不胜感激。 注意:这段代码显然没有完全完成,我主要只需要解决上述问题。
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public final class EchoServer
{
public static void main(String[] args) throws Exception
{
try (ServerSocket serverSocket = new ServerSocket(22222))
{
while (true)
{
try (Socket socket = serverSocket.accept())
{
Thread newThread = new Thread(new server(socket));
newThread.start();
}
}
}
}
}
class server implements Runnable
{
Socket mySocket;
server(Socket s)
{
mySocket = s;
}
public void run()
{
//while(true)
//{
try
{
System.out.println("Client connected ");
InputStream is = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
OutputStream os = mySocket.getOutputStream();
PrintStream out = new PrintStream(os);
String input = ("...");
while(!input.equals("exit"))
{
input = br.readLine();
out.println("Server> " + input);
}
}
catch(IOException e)
{
System.out.println(e);
}
//}
}
}
Try-with-resources 块退出时立即关闭资源,所以
try (Socket socket = serverSocket.accept())
{
Thread newThread = new Thread(new server(socket));
newThread.start();
}
// socket is closed by here, but newThread may still be running.
所以,只需删除 try
:
Socket socket = serverSocket.accept();
Thread newThread = new Thread(new server(socket));
newThread.start();
// socket is still open here.
但请记住关闭 server
class 中的套接字(最终)。
尝试编写一个简单的回显文本的服务器,需要为其提供多个线程。每当客户端连接时,我都会收到 Socket 关闭。 我不太确定哪里出了问题,而且我才刚刚开始学习这个,所以我不是很了解。如果有人能告诉我出了什么问题,我将不胜感激。 注意:这段代码显然没有完全完成,我主要只需要解决上述问题。
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public final class EchoServer
{
public static void main(String[] args) throws Exception
{
try (ServerSocket serverSocket = new ServerSocket(22222))
{
while (true)
{
try (Socket socket = serverSocket.accept())
{
Thread newThread = new Thread(new server(socket));
newThread.start();
}
}
}
}
}
class server implements Runnable
{
Socket mySocket;
server(Socket s)
{
mySocket = s;
}
public void run()
{
//while(true)
//{
try
{
System.out.println("Client connected ");
InputStream is = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
OutputStream os = mySocket.getOutputStream();
PrintStream out = new PrintStream(os);
String input = ("...");
while(!input.equals("exit"))
{
input = br.readLine();
out.println("Server> " + input);
}
}
catch(IOException e)
{
System.out.println(e);
}
//}
}
}
Try-with-resources 块退出时立即关闭资源,所以
try (Socket socket = serverSocket.accept())
{
Thread newThread = new Thread(new server(socket));
newThread.start();
}
// socket is closed by here, but newThread may still be running.
所以,只需删除 try
:
Socket socket = serverSocket.accept();
Thread newThread = new Thread(new server(socket));
newThread.start();
// socket is still open here.
但请记住关闭 server
class 中的套接字(最终)。