Listening to two ports simultaneously in Java server using multithreading(使用多线程在 Java 服务器中同时监听两个端口)
问题描述
我正在构建一个简单的 Java 服务器,它使用两个 ServerSocket 实例同时监听两个端口上的客户端请求.这是服务器的代码:
I am building a simple Java server which simultaneously listens for client request on two ports simultaneously using two ServerSocket instances. Here is the code for Server:
import java.io.PrintWriter;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Socket1 implements Runnable
{
public void run()
{
try
{
ServerSocket serverSock=new ServerSocket(5000);
while(true)
{
Socket sock=serverSock.accept();
PrintWriter writer=new PrintWriter(sock.getOutputStream());
String text="Welcome to Port 5000";
writer.println(text);
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
class Socket2 implements Runnable
{
public void run()
{
try
{
ServerSocket serverSock=new ServerSocket(3000);
while(true)
{
Socket sock=serverSock.accept();
PrintWriter writer=new PrintWriter(sock.getOutputStream());
String text="Welcome to Port 3000";
writer.println(text);
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
public class Server
{
public static void main(String[] args)
{
Socket1 s1=new Socket1();
Socket2 s2=new Socket2();
Thread t1=new Thread(s1);
Thread t2=new Thread(s2);
t1.start();
t2.start();
}
}
现在为 Client1 编码:
Now code for Client1:
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.Socket;
public class Client1
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("127.0.0.1", 5000);
InputStreamReader streamReader=new InputStreamReader(s.getInputStream());
BufferedReader reader=new BufferedReader(streamReader);
String text=reader.readLine();
System.out.println(text);
reader.close();
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
Client2 包含与 Client1 相同的代码,唯一的区别是它尝试连接到端口 3000.现在,我首先使用命令提示符执行 Server.java.当服务器阻塞时,我打开两个单独的命令提示符并在一个命令提示符上执行 Client1.java,在另一个命令提示符上执行 Client2.java.但是,两个客户端都进入阻塞状态,我没有得到任何输出.知道如何解决这个问题吗?
Client2 contains the same code as Client1, the only difference being that it tries to connect to port 3000. Now, I execute Server.java first using command prompt. While the server blocks, I open two separate command prompts and execute Client1.java on one command prompt and Client2.java on the other command prompt. But, both clients go to blocking state and I get no output. Any idea how to solve this ?
推荐答案
PrintWriter 被默认缓冲.在 writer.println()
之后添加一个 writer.flush()
(或者更好的是一个 close()
因为你不再使用流了)代码>.
PrintWriter is buffered by defaut. Add a writer.flush()
(or even better a close()
since you're not using the stream anymore) after writer.println()
.
这篇关于使用多线程在 Java 服务器中同时监听两个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用多线程在 Java 服务器中同时监听两个端口
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01