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!