Listen from a different port SOCKET

3

I have the following program:

Server

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


public class Professor {


  public static void main(String[] args) throws IOException
    {
        try{
        DatagramSocket ds = new DatagramSocket(1234);
        byte[] receive = new byte[65535];
         System.out.println("Esperant rebré missatges");

        DatagramPacket DpReceive = null;
        {
            DpReceive = new DatagramPacket(receive, receive.length);
            ds.receive(DpReceive);

            System.out.println("Alumne:" + data(receive));
            System.out.println("");

            System.out.println("Professor: Missatge rebut.Enviat la resposta a l'alumne");     

        }
     } catch (IOException e) 
        {      
            e.printStackTrace();    
        }

    }
    public static StringBuilder data(byte[] a)
    {
        if (a == null)
            return null;
        StringBuilder ret = new StringBuilder();
        int i = 0;
        while (a[i] != 0)
        {
            ret.append((char) a[i]);
            i++;
        }
        return ret;
    }
}    

The client class.

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


public class Alumne {

   public static void main(String args[]) throws IOException
    {
        try{   // Creant socket datagrama
        DatagramSocket ds = new DatagramSocket();
        InetAddress ip = InetAddress.getLocalHost();
        byte buf[] = null;

        {
            System.out.println("Comunicant al profesor que ja he acabat la practica..");
                  // Enviant missatge
            String inp="Ja tinc la practica acabada";
            System.out.println("");
            buf = inp.getBytes();
            DatagramPacket DpSend =
                  new DatagramPacket(buf, buf.length, ip, 1234);
            ds.send(DpSend);


            System.out.println("Esperant resposta del profesor....");
                // Rebent resposta



        }
    }     catch (IOException e) { 

      e.printStackTrace();  
}
    }
}

I have two doubts.

But the most important one is:

How can I make professor (server) use port 5555 And the "Student" client use port 5554 and they can be heard? I do not get it in any way.

thousand thanks!

    
asked by Montse Mkd 03.04.2018 в 19:19
source

1 answer

3

In Professor you have:

DatagramSocket ds = new DatagramSocket(1234);

The that constructor's javadoc specifies :

  

Constructs a datagram socket and binds it to the specified port on the   local host machine

This means that the server listens on that port.

In Alumne you have:

DatagramSocket ds = new DatagramSocket();

The javadoc of that other constructor specifies :

  

Constructs a datagram socket and binds it to any available port on the   local host machine.

Because of what you hear in any available port.

You are only sending data in Alumne and doing so you do it with:

InetAddress ip = InetAddress.getLocalHost();
DatagramPacket DpSend =
                  new DatagramPacket(buf, buf.length, ip, 1234);

But, the javadoc of that DatagramPacket constructor says:

  

Constructs a datagram packet for sending packets of length length to   the specified port number on the specified host. The length argument   must be less than or equal to buf.length .

     

Parameters:

     
  • buf - the packet data.
  •   
  • length - the packet length.
  •   
  • address - the destination address.
  •   
  • port - the destination port number.
  •   

Then, the application of your question:

  

How can I make professor (server) use port 5555 and the   "Student" client use port 5554 and that can be heard?

It is direct, just change the listening ports of the sockets and specify the correct ports in the packets.

    
answered by 03.04.2018 / 21:14
source