[英]How to create UDP socket connection to receive data from gstreamer udpsink?
[英]Receive data with UDP socket in VS2013
我想通过端口3702
接收*.xml
数据。
所以我做了一个示例服务器。 并通过三个端口1500,2500,3702
发送数据(在第43行中编辑PORT)
它可以正常工作并从1500,2500端口正确打印数据。 但是当我将端口设置为3702时。
它返回了一个错误: **Bind failed with error code :10048**
我发现可能存在其他客户端IP通过LAN中的端口3702发送数据。 我该如何解决?
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 8192 //Max length of buffer
#define PORT 3702 //The port on which to listen for incoming data
int main()
{
SOCKET s;
struct sockaddr_in server, si_other;
int slen, recv_len;
char buf[BUFLEN];
WSADATA wsa;
slen = sizeof(si_other);
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create a socket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
//Bind
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//keep listening for data
while (1)
{
printf("Waiting for data...");
fflush(stdout);
//clear the buffer by filling null, it might have previously received data
memset(buf, '\0', BUFLEN);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
//exit(EXIT_FAILURE);
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n", buf);
//now reply the client with the same data
}
closesocket(s);
WSACleanup();
return 0;
}
这是由于Address已被使用 。
通常,每个套接字地址(协议/ IP地址/端口)仅允许使用一种。 如果应用程序尝试将套接字绑定到已用于现有套接字的IP地址/端口,或者未正确关闭的套接字或仍在关闭过程中的套接字,则会发生此错误。 对于需要bind
多个套接字bind
到同一端口号的服务器应用程序,请考虑使用setsockopt ( SO_REUSEADDR
)。
客户端应用程序通常不需要调用bind在所有- 连接自动选择一个未使用的端口。 当使用通配符地址(涉及ADDR_ANY
)调用bind时,可能会延迟WSAEADDRINUSE
错误,直到提交特定地址为止。 稍后调用另一个函数(包括connect
, listen , WSAConnect或WSAJoinLeaf)可能会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.