swdeveloper
swdeveloper

Reputation: 918

In Java: UDP based Client/Server not giving the expected output

I have written a simple Client/Server code in Java, in which a client sends a message to server (which is displayed on Server's Standard Output) and then server also sends a message (which is displayed on Client's Standard Output). Code for Client and Server is given below:

Client.java

import java.io.*;
import java.net.*;

public class Client {
  public static void main(String[] args)throws Exception {
    DatagramSocket socket = new DatagramSocket ();
    InetAddress address = InetAddress.getByName("127.0.0.1");

    DatagramPacket packet = null;

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

    byte[] buf = new byte[256];

    String msg = stdIn.readLine();

    packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);

    // get response
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    // display response
    String received = new String(packet.getData(), 0, packet.getLength());
    System.out.println("Server says: " + received);

    socket.close();

  }
}

And below is Server.java

import java.io.*;
import java.net.*;

public class Server {
  public static void main(String[] args)throws Exception {
    DatagramSocket socket = new DatagramSocket(4445);

    byte[] buf = new byte[256];

    // receive client's message
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    // display client's message
    String received = new String(packet.getData(), 0, packet.getLength());
    System.out.println("Client says: " + received);

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

    String msg = stdIn.readLine();

    buf = msg.getBytes();

    // send the response to the client at "address" and "port"
    InetAddress address = packet.getAddress();        
    int port = packet.getPort();
    packet = new DatagramPacket(buf, buf.length, address, port);
    socket.send(packet);
  }
}

The code compiles and runs successfully, but the output does not come as expected. The message sent by Client is NOT displayed at Server, but Server's message is successfully displayed at Client.

So can anyone kindly tell what can be the problem?

Upvotes: 1

Views: 599

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500525

You never populate the packet with any useful data:

byte[] buf = new byte[256];
String msg = stdIn.readLine();
packet = new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet);

You've just posted a byte array with 256 bytes, all of which are 0. You've ignored msg entirely. Perhaps you wanted:

String msg = stdIn.readLine();
byte[] buf = msg.getBytes(StandardCharsets.UTF_8);
packet = new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet);

That's similar to the code you're using in the server, after all... except that I'm using UTF-8 explicitly, which I would recommend you do everywhere, when converting between byte[] and String.

Upvotes: 2

Related Questions