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 服务器中同时监听两个端口
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01