我正在尝试接收由 PlayCap(http://www.signal11.us/oss/playcap/)广播到网络地址 192.168.103.255 端口 3000 的 UDP 数据。我在绑定到此地址和端口时遇到问题。这是我的Java代码:
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 地址),我都会收到此错误。
我还编写了一些 C 代码来创建一个套接字并绑定到这个地址和端口,但它在绑定调用时也失败了。但是,此代码将绑定到我的静态 IP 地址 (192.168.1.149) 上的端口。这是代码:
#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 代码。我正在使用以下命令用 MinGW 编译 C 代码:
gcc a.c -lws2_32
(“ac”是文件名)。
虽然 Java 代码更重要,但我很高兴知道我的代码示例中哪里出错了。任何建议都非常感谢。