Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem sending data over UDP sockets
    text
    copied!<p>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:</p> <pre><code> server: java UDPServer 5555 &lt; file1.txt client: java UDPClient localhost 5555 &gt; file2.txt </code></pre> <p>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. </p> <p>The server code is:</p> <pre><code>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() &gt; 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(); } </code></pre> <p>And the client code is:</p> <pre><code>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() &gt; 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(); } </code></pre> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload