简体   繁体   中英

Why is my server socket not receiving packet sent by the client

I want to create a datagram server/client program where the server picks a random string from an array and send it to the client. But when i run the server and then the client which sends data about the buf byte array. The client sends the data to the serve as I understand but the server doesn't receive it.

Here's the client code:

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

public class SDClient {

    protected static String labelText;

    public static void main(String[] args) throws IOException {
        //socket setup
        MulticastSocket socket = new MulticastSocket(1);
        System.out.println("socket opened");
        InetAddress group = InetAddress.getByName("230.0.0.0");
        socket.joinGroup(group);
        System.out.println("socket joined group");

        //packet setup
        DatagramPacket packet;
        byte[] buf = new byte[256];

        //send packet
        packet = new DatagramPacket(buf, buf.length);
        packet.setAddress(group);
        packet.setPort(2);
        socket.send(packet);
        System.out.println("packet sent to: ");
        System.out.println("Port: " + packet.getPort() + " Address: " + packet.getAddress());

        //receive packet
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        System.out.println("packet received");

        //display packet string
        labelText = new String(packet.getData(), 0, packet.getLength());
        System.out.println("label text: " + labelText);

        socket.leaveGroup(group);
        socket.close();
    }

}

here's the server code:

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

public class SDServerThread extends Thread {

    protected DatagramSocket socket = null;
    protected String[] labelText = {"Packet sent and received", "Hello World!", "It works."}; 

    public SDServerThread() throws IOException {
        this("SDServerThread");
    }

    public SDServerThread(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(2);
        System.out.println("socket opened");

    }

    public void run() {
        try {
            byte[] buf = new byte[256];
            DatagramPacket packet = null;

            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            System.out.println("packet received");

            String getText = getText();
            buf = getText.getBytes();

            InetAddress group = InetAddress.getByName("230.0.0.0");
            packet = new DatagramPacket(buf, buf.length, group, 1);
            socket.send(packet);
            System.out.println("packet sent");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String getText() {
        int textNr = (int)(Math.random() * 3);
        String returnT = labelText[textNr];

        return returnT;
    }



}

Thanks in advence

I fixed it by making the servers DatagramSocket into a MulticastSocket and then made it join the group the client is in.

public class SDServerThread extends Thread {

    protected MulticastSocket socket = null;
    ...
    socket = new MulticastSocket(2);
    ...
    socket.joinGroup(group);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM