简体   繁体   English

Java tcp套接字转换为udp

[英]Java tcp socket conversion to udp

Currently I have my tcp version of the socket in this form. 目前我有这种形式的套接字的tcp版本。

final ServerSocket serverSocketConn = new ServerSocket(9000);               
               while (true) 
                    {
                        try
                        {
                                Socket socketConn1 = serverSocketConn.accept();
                                new Thread(new ConnectionHandler(socketConn1)).start();                     
                        }
                        catch(Exception e)
                        {
                            System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
                            e.printStackTrace(System.out);
                        }
                    }
class ConnectionHandler implements Runnable {

    private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }

     try{
          BufferedWriter bWriter =  new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
         BufferedReader bReader = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));

     }

}

It works fine so I am in the midst of converting it into UDP. 它工作正常,所以我正在将其转换为UDP。

public class commUDP9000 {
  class ReceiverThread implements Runnable {
    private DatagramSocket  receivedSocketConn1;
    ReceiverThread(DatagramSocket  receivedSocketConn1) {
      System.out.println("Thread Received");
      this.receivedSocketConn1=receivedSocketConn1;
    }
    public void run(){
        while (true){
            try{
                    //                      
             final byte[] buffer = new byte[1024];
             final DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
             receivedSocketConn1.receive(receivePacket);
             String sentence = new String( receivePacket.getData());                   
             System.out.println("RECEIVED: " + sentence);   
            }
            catch(Exception e){
                System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
                e.printStackTrace(System.out);
            }
        }
     }
   }
   public static void main(String[] args) {
       new commUDP9000();
   }
   commUDP9000() {     
      try{
               final DatagramSocket  serverSocketConn = new DatagramSocket (9000);
               new Thread(new ReceiverThread(serverSocketConn)).start();                            

      } 
      catch (Exception e) 
      {
         System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
         e.printStackTrace(System.out);
         //System.exit(0); 
      }
   }
}

I run this and end up with 我运行这个并最终结束

MyError:Socket Accepting has been caught in main loop.java.net.BindException: Address already in use MyError:Socket Accepting已在主循环中捕获.java.net.BindException:地址已在使用中

You cannot create connections on UDP since it is connectionless. 您无法在UDP上创建连接,因为它是无连接的。

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

DatagramPacket packet = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);

Only send and receive are allowed here. 这里只允许发送和接收。

with UDP, what you can do is, send one single packet to the other end. 使用UDP,您可以做的是,将一个数据包发送到另一端。 if you have sequence of packets, they are treated as individual packets. 如果您有数据包序列,则将它们视为单个数据包。 the delivery is not guaranteed. 交货不保证。 the limit of payload per message is around 64kbites. 每条消息的有效负载限制大约为64kbites。 in theory but depends on ur network MTU (probably 1500bytes). 理论上,但取决于你的网络MTU(可能是1500字节)。 the only benefit is its fast and overhead is minimum. 唯一的好处是它的快速和开销是最小的。

So you should create a enough buffer early. 所以你应该尽早创建一个足够的缓冲区。 If you want to emulate a stream, you have to implement your own protocol on top of Datagram. 如果要模拟流,则必须在Datagram之上实现自己的协议。 the same way, connection oriented TCP implemented on top of connectionless IP protocol. 同样,面向连接的TCP在无连接IP协议之上实现。

You're just creating endless threads that all just receive from the same socket. 你只是创建了无限的线程,所有线程都只是从同一个套接字接收。 You only need one of those, and the loop should be inside its run() method. 您只需要其中一个,循环应该在其run()方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM