简体   繁体   中英

Java tcp socket conversion to udp

Currently I have my tcp version of the socket in this form.

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.

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

You cannot create connections on UDP since it is connectionless.

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. 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. in theory but depends on ur network MTU (probably 1500bytes). 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. the same way, connection oriented TCP implemented on top of connectionless IP protocol.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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