多播服务器接收它发送的相同数据

时间:2013-03-02 19:18:00

标签: java networking udp multicast

我正在尝试允许连接到我的UDP服务器的客户端向服务器发送数据包,我有一台服务器正在运行,因此可以将数据包发送到客户端,但是尝试通过客户端将数据包发送回服务器似乎创造一些奇怪的错误。

如果我启动服务器,然后打开客户端,客户端将从服务器接收第一个数据包,然后客户端尝试将数据包发送到服务器,这是由服务器接收的,但是然后客户端抛出此错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "testing"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at Pong.PongUDPClient.main(PongUDPClient.java:71)

第71行将在代码中指向 服务器控制台只是打印出应该发送给客户端的数据,

这是服务器的整个代码

package Pong;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;

//This is the actual server
public class PongUDPServerThread extends Thread {

//speed to send data
private long dataSpeed = 10;
private int port = 4447;
private int idPlayer = 0;
private PongModel model;
private PongView  view;
private PongPlayerThread players[] = new PongPlayerThread[2];
private MulticastSocket socket = null;
private InetAddress group;
private DatagramPacket packet = null;
private int ID = 1;

public PongUDPServerThread() throws IOException 
{
    super("PongUDPServerThread");
}

public String rtnInfo()
{
    String str;
    str = model.getBall().getX() + ":" +
          model.getBall().getY() + ":" +
          model.getBats()[0].getX() + ":" +
          model.getBats()[0].getY() + ":" +
          model.getBats()[1].getX() + ":" +
          model.getBats()[1].getY();
    return str;
}

public void setupPong() throws IOException 
{
    System.out.println("Pong");
    model = new PongModel();
    view  = new PongView();
            new PongController( model, view );

    model.addObserver( view );       // Add observer to the model

    //view.setVisible(true);           // Display Screen
    model.makeActiveObject();        // Start play
    //start server
    socket = new MulticastSocket(port);
    InetAddress group = InetAddress.getByName("230.0.0.1");
    socket.setReuseAddress(true);
    socket.joinGroup(group);
    //socket.setReuseAddress(true);
    //Inform server user
    System.out.println("Server is running");
}

public void run()
{
    //Server loop
    while(true)
    {

        //Receive the data
        try
        {
        byte[] receiveBuf = new byte[256];
        packet = new DatagramPacket(receiveBuf, receiveBuf.length);
        socket.receive(packet);

        String received = new String(packet.getData(), 0, packet.getLength());

        System.out.println(received);
        }
        catch (IOException e)
        {

        }


        try
        {
            byte[] buf = new byte[256];

            //Gather data
            buf = rtnInfo().getBytes();
            //System.out.println(buf.toString());

            //Send data
            packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("230.0.0.1"), port);
            socket.send(packet);

            //Sleep the server
            try
            {
                sleep((long)dataSpeed);
            }
            catch (InterruptedException e)
            {

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

    //socket.close();

}

}

这是客户端代码

package Pong;

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

public class PongUDPClient {

private static int port = 4447;

public static void main(String[] args) throws IOException
{

    //Setup pong rdy
    PongModel model = new PongModel();
    PongView  view  = new PongView();
                      new PongController( model, view );

    model.addObserver( view );       // Add observer to the model

    String pos;

    model.makeActiveObject();        // Start play
    //model.clientModel();

    MulticastSocket socket = new MulticastSocket(port);
    //socket.setReuseAddress(true);
    InetAddress address = InetAddress.getByName("230.0.0.1");
    //Join the UDP list port
    socket.setReuseAddress(true);
    socket.joinGroup(address);

    DatagramPacket packet;
    view.setVisible(true);           // Display Screen

    System.out.println("Connected");
    //Id is sent here.

    while(true)
    {

        //Send data to server
        try
        {
            byte[] receiveBuf = new byte[256];

            //Gather data
            receiveBuf = "testing".getBytes();
            //System.out.println(buf.toString());

            //Send data
            packet = new DatagramPacket(receiveBuf, receiveBuf.length, InetAddress.getByName("230.0.0.1"), port);
            socket.send(packet);

            //Sleep the server
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        byte[] buf = new byte[256];
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);

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

        String[] posValues = received.split(":");

        model.getBall().setX(Double.parseDouble(posValues[0])); // <-- Line 71
        model.getBall().setY(Double.parseDouble(posValues[1]));
        model.getBats()[0].setX(Double.parseDouble(posValues[2]));
        model.getBats()[0].setY(Double.parseDouble(posValues[3]));
        model.getBats()[1].setX(Double.parseDouble(posValues[4]));
        model.getBats()[1].setY(Double.parseDouble(posValues[5]));

        //Check for keyboard input

        if(PongController.moveUp == true && PongController.moveDown == false)
        {
            System.out.println("Up");
            PongController.moveUp = false;
        }
        else if(PongController.moveUp == false && PongController.moveDown == true)
        {
            System.out.println("Down");
            PongController.moveDown = false;
        }
        else
        {
            //serverOut.println("nothing");
        }


    }
}

}

已经坚持了一段时间,似乎找不到任何有关如何使用Multicast从客户端向服务器发送数据包的教程。

1 个答案:

答案 0 :(得分:5)

您正在体验多播环回。使用MulticastSocket#setLoopbackMode(true)将其关闭,或者,如果该对等方只是发送到多播组而未接收,则根本不需要加入该组。