绑定到特定的IP地址和端口接收UDP数据 [英] Binding to a specific IP address and port to receive UDP data

查看:266
本文介绍了绑定到特定的IP地址和端口接收UDP数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收到广播由PlayCap到网络地址192.168.103.255端口UDP 3000数据( HTTP:// WWW .signal11.us / OSS / playcap / )。我有问题结合到该地址和端口。这里是我的Java code:

I am trying to receive UDP data that is broadcast to network address 192.168.103.255 port 3000 by PlayCap (http://www.signal11.us/oss/playcap/). I'm having problems binding to this address and port. Here's my Java code:

public static void main(String[] args) {
    try {
        DatagramSocket s = new DatagramSocket();
        InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
        s.bind(address);

        byte buffer[] = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        System.out.println("Waiting...");
        s.receive(packet);
        System.out.println("Received!");

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

这将返回错误:

java.net.SocketException: already bound
    at java.net.DatagramSocket.bind(Unknown Source)
    at runner.main(runner.java:12)

我已经运行命令netstat的-a -n,既不地址192.168.103.255和端口3000的输出的任何地方上市,所以我不认为此端口已在使用。事实上,我得到这个错误的地址/端口组合,我尝试(包括我的静态IP地址)。

I have run the command "netstat -a -n", and neither address 192.168.103.255 nor port 3000 are listed anywhere in the output, so I don't think this port is already in use. In fact, I get this error for any address/port combination I try (including my static IP address).

我也写了一些C code键使套接字,并绑定到该地址和端口,但它也无法在绑定呼叫。这code,但是,将绑定到端口上我的静态IP地址(192.168.1.149)。下面是code:

I also wrote some C code to make a socket and bind to this address and port, but it also fails on the bind call. This code, however, will bind to ports on my static IP address (192.168.1.149). Here's that code:

#include <stdio.h>
#include <sys/types.h>
#include <winsock.h>
#include <unistd.h>

#define a1 192
#define a2 168
#define a3 103
#define a4 255
#define PORT 3000

int main() {

    /* Open windows connection */
    WSADATA w;
    if (WSAStartup(0x0101, &w) != 0)
    {
        printf("Could not open Windows connection.\n");
        exit(0);
    }

    /* Clear out server struct */
    SOCKADDR_IN server;
    memset((void *)&server, '\0', sizeof(struct sockaddr_in));

    /* Set family and port */
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
    server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
    server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
    server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

    /* Open a datagram socket */
    int sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sd == INVALID_SOCKET)
    {
        printf("Could not create socket.\n");
        WSACleanup();
        exit(0);
    }

    /* Bind address to socket */
    if (bind(sd, (struct sockaddr *)&server, sizeof(SOCKADDR_IN)) == -1)
    {
        printf("Could not bind name to socket.\n");
        closesocket(sd);
        WSACleanup();
        exit(0);
    }

    /* Receive */
    char data[1024];
    printf("Waiting to receive...\n");
    if (recv(sd, (char *)&data, (int)sizeof(data), 0))
    {
        printf("Error receiving data.\n");
        closesocket(sd);
        WSACleanup();
        exit(0);
    }

    printf("Data: %s", data);

    return 0;
}

我使用的是Windows 7计算机。我运行Eclipse的Java code。我使用的命令编译C code。与MinGW的:

I'm using a Windows 7 machine. I'm running the Java code in Eclipse. I'm compiling the C code with MinGW using the command:

gcc a.c -lws2_32

(交流转换器是文件名)。

("a.c" is the file name).

虽然Java code是更重要的,我会很高兴知道我要去哪里错了或者我的code的例子。任何建议都非常AP preciated。

While the Java code is more important, I would be happy to know where I'm going wrong in either of my code examples. Any suggestions are very much appreciated.

推荐答案

试试这个为你的Java code来代替:

Try this for your Java code instead:

public static void main(String[] args) {
    try {
        DatagramSocket s = new DatagramSocket(null);
        InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
        s.bind(address);

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

呼唤一个数据报套接字的无参数的构造函数将导致它绑定到一个随机的,可用的端口。一旦绑定,进一步尝试(重新)绑定将抛出一个异常插座(与你看到的错误)。要'延迟'的结合,而不是你在创建未绑定状态的数据报套接字(在构造函数传递一个空的),然后调用绑定以后。

这篇关于绑定到特定的IP地址和端口接收UDP数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆