[英]Receive data with UDP socket in VS2013
I wanted to receive an *.xml
data by port 3702
. 我想通过端口
3702
接收*.xml
数据。
So I made a example Server. 所以我做了一个示例服务器。 And sended data by three port
1500,2500,3702
.(Edit the PORT in line 43) 并通过三个端口
1500,2500,3702
发送数据(在第43行中编辑PORT)
It worked and printed data correctly from port 1500,2500. 它可以正常工作并从1500,2500端口正确打印数据。 But when I set the PORT to 3702.
但是当我将端口设置为3702时。
it returned me a error: **Bind failed with error code :10048**
它返回了一个错误:
**Bind failed with error code :10048**
I found that maybe it existed other Client IP were sending data by PORT 3702 in my LAN. 我发现可能存在其他客户端IP通过LAN中的端口3702发送数据。 How can I fix it?
我该如何解决?
#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;
}
This is due to Address already in use . 这是由于Address已被使用 。
Typically, only one usage of each socket address (protocol/IP address/port) is permitted. 通常,每个套接字地址(协议/ IP地址/端口)仅允许使用一种。 This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing.
如果应用程序尝试将套接字绑定到已用于现有套接字的IP地址/端口,或者未正确关闭的套接字或仍在关闭过程中的套接字,则会发生此错误。 For server applications that need to
bind
multiple sockets to the same port number, consider using setsockopt ( SO_REUSEADDR
). 对于需要
bind
多个套接字bind
到同一端口号的服务器应用程序,请考虑使用setsockopt ( SO_REUSEADDR
)。
Client applications usually need not call bind at all - connect chooses an unused port automatically. 客户端应用程序通常不需要调用bind在所有- 连接自动选择一个未使用的端口。 When bind is called with a wildcard address (involving
ADDR_ANY
), a WSAEADDRINUSE
error could be delayed until the specific address is committed. 当使用通配符地址(涉及
ADDR_ANY
)调用bind时,可能会延迟WSAEADDRINUSE
错误,直到提交特定地址为止。 This could happen with a call to another function later, including connect
, listen , WSAConnect , or WSAJoinLeaf . 稍后调用另一个函数(包括
connect
, listen , WSAConnect或WSAJoinLeaf)可能会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.