Reputation: 655
I have a simple working program that can transfer files with UDP . But for each client and server, I have two sockets which send data at a port and receive data at a different port.
For example, my client socket_out sends data packet at port 9000 and receives data with socket_in which listens at port 9001. My server socket_in listens at port 9000 and sends ACK data packet at port 9001.
Now I want to simplify the design and use only one port number for receiving and sending messages at each client and server. For example, both client and server program sends and receives data at port 9000.
Is it possible to do that? How should I make the change? I tried to create two socket for send and receiving at the same port number, but I always get this error:
java.net.BindException: Address already in use
I googled found out two sockets cant share the same port number.
Adding on the code: Sender:
public FileSender(String fileName, int unrelPort, String rcvFileName) {
DatagramSocket socket_out_client, socket_in_client;
System.out.println("Start Sending " + fileName + " through port " +unrelPort + " as " + rcvFileName + ".");
try {
// create sockets
socket_out_client = new DatagramSocket();
socket_in_client = new DatagramSocket(unrelPort);
// create input file
File inputFile = new File(fileName);
if (!inputFile.exists()) {
System.err.println("Input file does not exist");
System.exit(-1);
}
// create threads to process data
InThread th_in = new InThread(socket_out_client,socket_in_client);
OutThread th_out = new OutThread(socket_in_client, unrelPort, inputFile, rcvFileName);
th_in.start();
th_out.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
Same for receiver
Upvotes: 0
Views: 2395
Reputation:
First of all why did you create two sockets at client side for sending and receiving when you can use same socket_client
to send and receive. You can do this by creating two threads one for sending and one for receiving with the same socket_client
.
code: something like this
DatagramSocket sock = new DatagramSocket();
new Thread(new Runnable() {
@Override
public void run() {
try{
//create packet
//your logic
sock.send(packet);
}
}catch(Exception e){}
}
}).start();
System.out.println("Debug :: "+"thread 1 started");
new Thread(new Runnable() {
@Override
public void run() {
try{
//your logic
sock.receive(packet)
}
}catch(Exception e){}
}
}).start();
System.out.println("Debug :: "+"thread 2 started");
Upvotes: 0
Reputation: 70909
A port number is a number embedded in the network packets. Once a computer's operating system processes an inbound network packet, it needs to "know" which program to pass the pack to as input. The port number is used to lookup the program in the receiving operating system's port to program table.
That's why you cannot have two programs reading from the same port, as that roughly makes it impossible for the operating system to determine which of the two programs the packet should be sent to as input.
Note that this is not the only way you can get a port conflict. You could just have two copies of the program running on the same machine too.
Upvotes: 1