I'm creating a Server-Client networking library for java. I'm just wondering if it's recomended to use the same DatagramSocket
object from multiple classes, or if it may cause problems.
The reason to why I'm wondering is because I think that it might be problems if you for example send two things at the same time from the same socket. Is that true?
Here is an example of what I mean:
ExampleClass1:
public class ExampleClass1 {
// The socket
DatagramSocket socket;
public ExampleClass1() {
// Create the socket
socket = new DatagramSocket(1111);
// Create an instance of the ExampleClass2
ExampleClass2 class2 = new ExampleClass2(socket);
}
public static void main(String[] args) {
// Send something through the socket
socket.send(new DatagramPacket(new byte[] {}, 0, 0.0.0.0, 0001));
}
}
ExampleClass2:
public class ExampleClass2 {
// Another socket object
DatagramSocket socket;
public ExampleClass2(DatagramSocket exampleClass1Socket) {
// Set this class's socket object to the socket in the ExampleClass1
this.socket = exampleClass1Socket;
}
public static void main(String[] args) {
// Send something from the socket
socket.send(new DatagramSocket(new byte[] {}, 0, 0.0.0.0, 0000));
}
}