This is kind of a followup to the question I had yesterday. I had a homework assignment to send and receive data with a client/server TCP socket connection. I would like to make a version of it using UDP. The idea is that I can redirect standard I/O and send the streams using UDP. For example, if I type in:
server: java UDPServer 5555 < file1.txt
client: java UDPClient localhost 5555 > file2.txt
It should send the data in file1.txt from the server to client's file2.txt. When I run the client/server pair in separate terminals, file2.txt is created but the data is never actually sent. Instead it seems like I am stuck in an infinite loop, where I cannot enter anything into the terminal unless I kill the application.
The server code is:
public static final int BUF_SIZE = 256;
public static void main(String[] args) throws IOException{
port = Integer.parseInt(args[0]);
DatagramSocket serverSocket = new DatagramSocket(port);
BufferedInputStream input = new BufferedInputStream(System.in);
BufferedOutputStream output = new BufferedOutputStream(System.out);
byte[] receiveData = new byte[BUF_SIZE];
byte[] sendData = new byte[BUF_SIZE];
byte[] buf = new byte[BUF_SIZE];
String sentence;
if(System.in.available() > 0) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress address = receivePacket.getAddress();
int bytesRead = 0;
while((bytesRead = input.read(buf, 0, BUF_SIZE)) != -1) {
sentence = new String(buf, 0, bytesRead);
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
serverSocket.send(sendPacket);
}
} else {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
sentence = new String(receivePacket.getData());
output.write(sentence.getBytes());
}
serverSocket.close();
input.close();
output.close();
}
And the client code is:
public static final int BUF_SIZE = 256;
public static void main(String[] args) throws IOException{
String hostName = args[0];
port = Integer.parseInt(args[1]);
DatagramSocket clientSocket = new DatagramSocket();
InetAddress address = InetAddress.getByName(hostName);
BufferedInputStream input = new BufferedInputStream(System.in);
BufferedOutputStream output = new BufferedOutputStream(System.out);
byte[] sendData = new byte[BUF_SIZE];
byte[] receiveData = new byte[BUF_SIZE];
byte[] buf = new byte[BUF_SIZE];
String sentence;
if(System.in.available() > 0) {
int bytesRead = 0;
while((bytesRead = input.read(buf, 0, BUF_SIZE)) != -1) {
sentence = new String(buf, 0, bytesRead);
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
clientSocket.send(sendPacket);
}
} else {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
sentence = new String(receivePacket.getData());
output.write(sentence.getBytes());
}
clientSocket.close();
input.close();
output.close();
}
I am still new to socket programming so I am basing this off of example code in my textbook. Is there some glaring mistake that I am making that is preventing the data from being transferred? Thanks very much for your patience and help!