1

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

17 Comments

@So I have emulate this now final DatagramSocket serverSocketConn = new DatagramSocket (9000); while (true) { try { new Thread(new ConnectionHandler(serverSocketConn)).start(); } { try { // Socket socketConn1 = serverSocketConn.accept(); new Thread(new ConnectionHandler(serverSocketConn)).start(); }
This is an example usage of udp.systembash.com/content/a-simple-java-udp-server-and-udp-client Yes you have to emulate.
I must set the buffer size in advance ? IS there any no way like how I am doing in tcp where I have this BufferedReader bReader = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); then I do this while ((nextChar=bReader.read()) != -1)
@The received packet will will then be a full packet which is sent so I guess I need to modify by my own the extraction of it. So maybe I can play with sub string to extract rather then doing the !=-1 ?
Use String output = new String(receivedByteArray);
|
1

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.

12 Comments

can you suggest how to do it cause I end up with memory exception.
@user2711681 I just did 'suggest how to do it'. One thread, with a read loop inside it. What part of that don't you understand?
based on your suggestion I have updated my question where first I call this final DatagramSocket serverSocketConn = new DatagramSocket (9000); new Thread(new ReceiverThread(serverSocketConn)).start(); then I all the process is in done the public void run() with while loop in it. Still I get this error "MyError:Socket Conn has been caught in main loop.java.net.BindException: Address already in use java.net.BindException: Address already in use"
You do realize that's a new problem, don't you? Caused by the old code still running? or creating the DatagramSocket in a loop?
Is is cause by the while loop kind of lost here not too sure with the process ? Any hint ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.