UDP C Server 未收到数据包
我知道已经回答了相关问题,但我没能解决我的问题。
我有一个简单的 UDP 客户端-服务器应用程序。客户和服务器看起来没问题,但是服务器没有从客户端接收数据包,它只是无限地接收网络 ID 192(或者客户端可能没有正确发送数据包)。
我似乎无法弄清楚问题、地址和地址。端口都可以,我没有任何防火墙,我什至为端口添加了一个例外,只是为了确定。如果我启动服务器,我可以看到它在正确的端口上正常监听(netstat -a -s -p udp)。你能给我一个提示,告诉我出了什么问题吗?
这是我的服务器代码:
/*.. includes */
#define PORT 8888
#define NPACK 10
#define MAXLEN 100
void signalError(char* s){
perror(s);
exit(1);
}
int main()
{
struct sockaddr_in struct_srv, struct_client;
int s,i,cod, numbytes;
size_t clientSize;
int32_t nr;
int32_t sir1[MAXLEN], sir2[MAXLEN], sirComune[MAXLEN], nrEl1, nrEl2, nrComune;
//Creating the socket:
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s==-1) signalError("Error while creating socket!");
memset(&struct_srv, 0, sizeof(struct_srv));
struct_srv.sin_family = AF_INET;
struct_srv.sin_addr.s_addr = htonl(INADDR_ANY);
struct_srv.sin_port = htons(PORT);
s = bind(s, (struct sockaddr*) &struct_srv, sizeof(struct_srv));
if(s==-1) signalError("Bind error. Port is already in use!");
//receive packets:
nrEl1 = -1;
char buf[MAXLEN];
printf("Accepting packets:\n");
//for(i=0;i<NPACK;i++) {
for(;;) {
//Receive packets from client:
clientSize = sizeof(struct_client);
numbytes = recvfrom(s, buf, MAXLEN - 1, 0,
(struct sockaddr*) &struct_client, &clientSize);
buf[numbytes] = '\0';
printf("Packet is %d long.\n", numbytes);
printf("Packet contains %s:\n", buf);
sleep(3);
}
close(s);
return 0;
}
和我的客户端代码:
/* includes */
#define SRV_IP "127.0.0.1"
#define NPACK 100
#define MAXLEN 100
#define PORT 8888
void signalError(char* s){
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in struct_client;
int s, i, result, size_client = sizeof(struct_client);
int32_t nr, sir1[MAXLEN], sir2[MAXLEN];
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s==-1) signalError("Erorr creating socket!");
memset((char*) &struct_client, 0, sizeof(struct_client));
struct_client.sin_family = AF_INET;
struct_client.sin_addr.s_addr = htonl(INADDR_ANY);
struct_client.sin_port = htons(PORT);
if(inet_aton(SRV_IP, &struct_client.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
char buf[MAXLEN];
int len;
for(i=0;i<NPACK;i++) {
printf("Give packet %d:\n", i+1);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
printf("I've read %s\n", buf);
printf("Sending packet %d\n", i+1);
result = sendto(s, buf, sizeof(buf)+1, 0,
(struct sockaddr*) &struct_client, size_client);
if(result==-1) signalError("Error sending packets!");
}
close(s);
return 0;
}
I know that there are related questions already answered, but I didn't manage to solve my problem.
I have a simple UDP client-server app. Both the client & server look ok, however the server doesn't receive packets from the client, it just infinitely receives the net id, 192 (or maybe the client doesn't send the packages correctly).
I can't seem to figure out the problem, the address & port are both ok, I don't have any firewall, and I even added an exception for the port, just to be sure. If I start the server, I can see that it's listening ok, on the right port (netstat -a -s -p udp). Can you please give me a hint on what's wrong?
Here is my server code:
/*.. includes */
#define PORT 8888
#define NPACK 10
#define MAXLEN 100
void signalError(char* s){
perror(s);
exit(1);
}
int main()
{
struct sockaddr_in struct_srv, struct_client;
int s,i,cod, numbytes;
size_t clientSize;
int32_t nr;
int32_t sir1[MAXLEN], sir2[MAXLEN], sirComune[MAXLEN], nrEl1, nrEl2, nrComune;
//Creating the socket:
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s==-1) signalError("Error while creating socket!");
memset(&struct_srv, 0, sizeof(struct_srv));
struct_srv.sin_family = AF_INET;
struct_srv.sin_addr.s_addr = htonl(INADDR_ANY);
struct_srv.sin_port = htons(PORT);
s = bind(s, (struct sockaddr*) &struct_srv, sizeof(struct_srv));
if(s==-1) signalError("Bind error. Port is already in use!");
//receive packets:
nrEl1 = -1;
char buf[MAXLEN];
printf("Accepting packets:\n");
//for(i=0;i<NPACK;i++) {
for(;;) {
//Receive packets from client:
clientSize = sizeof(struct_client);
numbytes = recvfrom(s, buf, MAXLEN - 1, 0,
(struct sockaddr*) &struct_client, &clientSize);
buf[numbytes] = '\0';
printf("Packet is %d long.\n", numbytes);
printf("Packet contains %s:\n", buf);
sleep(3);
}
close(s);
return 0;
}
And my client code:
/* includes */
#define SRV_IP "127.0.0.1"
#define NPACK 100
#define MAXLEN 100
#define PORT 8888
void signalError(char* s){
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in struct_client;
int s, i, result, size_client = sizeof(struct_client);
int32_t nr, sir1[MAXLEN], sir2[MAXLEN];
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s==-1) signalError("Erorr creating socket!");
memset((char*) &struct_client, 0, sizeof(struct_client));
struct_client.sin_family = AF_INET;
struct_client.sin_addr.s_addr = htonl(INADDR_ANY);
struct_client.sin_port = htons(PORT);
if(inet_aton(SRV_IP, &struct_client.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
char buf[MAXLEN];
int len;
for(i=0;i<NPACK;i++) {
printf("Give packet %d:\n", i+1);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
printf("I've read %s\n", buf);
printf("Sending packet %d\n", i+1);
result = sendto(s, buf, sizeof(buf)+1, 0,
(struct sockaddr*) &struct_client, size_client);
if(result==-1) signalError("Error sending packets!");
}
close(s);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是一些要点:
recvfrom
之前设置size_client = sizeof(struct_client)
。它既是输入参数又是输出参数,因此您需要确保一次调用的输出不会干扰下一次调用的输入。buf
的最后一个字节设置为'\0'
以防止printf
读取超出边界的内存。numbytes
。它不起作用的主要原因是:
您从未初始化过 nr,因此无法得知 sendto 正在发送什么。它肯定不会发送
buf
,我认为这就是您想要的......您可能想要这个:Here are a few minor points:
size_client = sizeof(struct_client)
before every call torecvfrom
. It is both an input and output parameter, so you want to make sure the output from one call does not interfere with the input to the next.buf
to'\0'
to prevent yourprintf
from reading memory beyond the boundary.cod
? Where did it come from? I think you meant to testnumbytes
.buf[strlen(buf) - 1] = '\0'
chops off the last character of the buffer; is that what you were trying to do?And the major reason it doesn't work:
You never initialized
nr
, so there is no telling what sendto is sending. It certainly isn't sendingbuf
, which I think was what you wanted... You probably want this:除非我错过了一些重大的事情,否则你只是在这里发送垃圾。
nr
永远不会在发送者上初始化,除了在评论中。buf
正在初始化,但未在任何地方使用。您可以尝试将
sendto
部分更改为以下内容:Unless I'm missing something drastic, you're just sending garbage here.
nr
is never initialized on the sender, except in a comment.buf
is being initialized, but not used anywhere.Can you try changing the
sendto
part to something like:在您的客户端代码中,通过网络发送的
nr
变量未初始化,可以指向它想要的任何内容。您可能想发送您的buf
数组。In your client code, the
nr
variable that is sent over the net is not initialized and can point to whatever it wants. You probably want to send yourbuf
array instead.