使用udp无连接协议在简单的java服务器中可以实现多线程吗?

发布于 2024-09-06 17:29:49 字数 44 浏览 3 评论 0原文

使用udp无连接协议在简单的java服务器中可以实现多线程吗?举个例子!!

Is multi-threading possible in a simple java server using udp connectionless protocol? give an example!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-09-13 17:29:49

使用 UDP 的多线程实际上更简单,因为您不必担心连接状态。这是我的服务器的监听循环,

           while(true){

                    try{
                            byte[] buf = new byte[2048];

                            DatagramPacket packet = new DatagramPacket( buf, buf.length, address );

                            socket.receive( packet );
                            threadPool.execute( new Request( this, socket, packet ));

           .......

threadPool 是一个 ThraPoolExecutor。由于UDP会话的短暂性,需要线程池来避免重复创建线程的开销。

Multi-threading is actually simpler with UDP because you don't have to worry about connection state. Here is the listen loop from my server,

           while(true){

                    try{
                            byte[] buf = new byte[2048];

                            DatagramPacket packet = new DatagramPacket( buf, buf.length, address );

                            socket.receive( packet );
                            threadPool.execute( new Request( this, socket, packet ));

           .......

The threadPool is a ThreaPoolExecutor. Due to the short-lived nature of UDP sessions, thread pool is required to avoid the overhead of repeatedly thread creation.

〆一缕阳光ご 2024-09-13 17:29:49

是的,可以通过使用 < java.nio 中的 code>DatagramChannel 类。 这是一个教程(它没有解决多线程问题,但这是无论如何,这是一个单独的问题)。

Yes, it's possible through the use of the DatagramChannel class in java.nio. Here's a tutorial (It does not address the multithreading, but that is a separate issue anyway).

鹤仙姿 2024-09-13 17:29:49

这是一个示例,尝试输入您自己的 IP 来获取硬编码消息

package a.first;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class Serv {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

        Listner listner = new Listner();
        Thread thread = new Thread(listner);
        thread.start();


        String messageStr = "Hello msg1";
        int server_port = 2425;
        DatagramSocket s = new DatagramSocket();
        InetAddress local = InetAddress.getByName("172.20.88.223");
        int msg_length = messageStr.length();
        byte[] message = messageStr.getBytes();
        DatagramPacket p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg1");
        s.send(p);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
        messageStr = "Hello msg2";
        msg_length = messageStr.length();
        message = messageStr.getBytes();
        p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg2");
        s.send(p);
}


}
   class Listner implements Runnable
    { 
            @Override
            public void run() {
        String text = null;
        while(true){
            text = null;    
        int server_port = 2425;
        byte[] message = new byte[1500];
        DatagramPacket p = new DatagramPacket(message, message.length);
        DatagramSocket s = null;
        try{
           s = new DatagramSocket(server_port);
        }catch (SocketException e) {
            e.printStackTrace();
            System.out.println("Socket excep");
        }
        try {
        s.receive(p);
       }catch (IOException e) {
            e.printStackTrace();
                System.out.println("IO EXcept");
            }
        text = new String(message, 0, p.getLength());
        System.out.println("message = "+text);
        s.close();

    }
}

}

Here is one example try putting your own ip to get the hard-coded message back

package a.first;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class Serv {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

        Listner listner = new Listner();
        Thread thread = new Thread(listner);
        thread.start();


        String messageStr = "Hello msg1";
        int server_port = 2425;
        DatagramSocket s = new DatagramSocket();
        InetAddress local = InetAddress.getByName("172.20.88.223");
        int msg_length = messageStr.length();
        byte[] message = messageStr.getBytes();
        DatagramPacket p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg1");
        s.send(p);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
        messageStr = "Hello msg2";
        msg_length = messageStr.length();
        message = messageStr.getBytes();
        p = new DatagramPacket(message, msg_length, local,
                server_port);
        System.out.println("about to send msg2");
        s.send(p);
}


}
   class Listner implements Runnable
    { 
            @Override
            public void run() {
        String text = null;
        while(true){
            text = null;    
        int server_port = 2425;
        byte[] message = new byte[1500];
        DatagramPacket p = new DatagramPacket(message, message.length);
        DatagramSocket s = null;
        try{
           s = new DatagramSocket(server_port);
        }catch (SocketException e) {
            e.printStackTrace();
            System.out.println("Socket excep");
        }
        try {
        s.receive(p);
       }catch (IOException e) {
            e.printStackTrace();
                System.out.println("IO EXcept");
            }
        text = new String(message, 0, p.getLength());
        System.out.println("message = "+text);
        s.close();

    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文