gpt4 book ai didi

Java:多线程和 UDP 套接字编程

转载 作者:搜寻专家 更新时间:2023-11-01 01:19:39 25 4
gpt4 key购买 nike

我是 Java 多线程和套接字编程的新手。我想知道什么是实现 2 个线程的最佳方法 - 一个用于接收套接字,一个用于发送套接字。如果我正在尝试做的事情听起来很荒谬,请告诉我为什么!代码很大程度上是从 Sun 的在线教程中获得灵感的。我想使用多播套接字,以便我可以使用多播组。

class Server extends Thread
{

static protected MulticastSocket socket = null;
protected BufferedReader in = null;
public InetAddress group;

private static class Receive implements Runnable
{

public void run()
{
try
{
byte[] buf = new byte[256];
DatagramPacket pkt = new DatagramPacket(buf,buf.length);
socket.receive(pkt);
String received = new String(pkt.getData(),0,pkt.getLength());
System.out.println("From server@" + received);
Thread.sleep(1000);
}
catch (IOException e)
{
System.out.println("Error:"+e);
}
catch (InterruptedException e)
{
System.out.println("Error:"+e);
}

}

}


public Server() throws IOException
{
super("server");
socket = new MulticastSocket(4446);
group = InetAddress.getByName("239.231.12.3");
socket.joinGroup(group);
}

public void run()
{

while(1>0)
{
try
{
byte[] buf = new byte[256];
DatagramPacket pkt = new DatagramPacket(buf,buf.length);
//String msg = reader.readLine();
String pid = ManagementFactory.getRuntimeMXBean().getName();
buf = pid.getBytes();
pkt = new DatagramPacket(buf,buf.length,group,4446);
socket.send(pkt);
Thread t = new Thread(new Receive());
t.start();

while(t.isAlive())
{
t.join(1000);
}
sleep(1);
}
catch (IOException e)
{
System.out.println("Error:"+e);
}
catch (InterruptedException e)
{
System.out.println("Error:"+e);
}

}
//socket.close();
}

public static void main(String[] args) throws IOException
{
new Server().start();
//System.out.println("Hello");
}

}

最佳答案

首先是第一件事:根据 Java Naming Conventions,您的类(class)应以大写字母开头:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

第二:尝试将代码分解成连贯的部分,并围绕您正在处理的一些常见功能来组织它们……也许是围绕您正在编程的功能或模型。

服务器的(基本)模型是它所做的唯一事情是接收套接字连接……服务器依赖处理程序来处理这些连接,仅此而已。如果您尝试构建该模型,它将看起来像这样:

class Server{
private final ServerSocket serverSocket;
private final ExecutorService pool;

public Server(int port, int poolSize) throws IOException {
serverSocket = new ServerSocket(port);
pool = Executors.newFixedThreadPool(poolSize);
}

public void serve() {
try {
while(true) {
pool.execute(new Handler(serverSocket.accept()));
}
} catch (IOException ex) {
pool.shutdown();
}
}
}

class Handler implements Runnable {
private final Socket socket;
Handler(Socket socket) { this.socket = socket; }
public void run() {
// receive the datagram packets
}
}

第三:我建议您查看一些现有示例。

根据评论更新:
好的 Ravi,您的代码存在一些问题和一些问题:

  1. 我假设 Receive 类是您的客户端...您应该将它作为一个单独的程序(有自己的主类)拉出来,然后运行您的服务器和多个客户端同时。为您发送的每个新 UDP 包从您的服务器生成一个新的“客户端线程”是一个令人不安的想法(问题)。

  2. 当您制作客户端应用程序时,您应该让它在自己的 while 循环中运行接收代码(次要 问题),例如:

    public class Client extends Thread
    {
    public Client(/*..*/)
    {
    // initialize your client
    }

    public void run()
    {
    while(true)
    {
    // receive UDP packets
    // process the UDP packets
    }
    }

    public static void main(String[] args) throws IOException
    {
    // start your client
    new Client().start();
    }
    }
  3. 每个客户端只需要一个线程,每个服务器只需要一个线程(从技术上讲,您甚至不需要一个单独的线程,因为 main 有自己的线程),所以您可能找不到 ExecutorService 那个有用。

否则您的方法是正确的...但我仍然建议您查看一些示例。

关于Java:多线程和 UDP 套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com