Hallo zusammen!
Nach Jahren ohne C Programmieren versuche ich mich grade an einer kleinen UDP sende/empfang-geschichte und stoße leider auf heftige Gegenwehr
mein Problem ist, dass der sender beim sendto() aufruf einen Invalid argument Fehler wirft. (Typ-technishc sollte aber alles stimmen, sonst würde doch der compiler meckern?!)
Hier mal meine beiden kleinen Quelltexte:
udpcli.c (zum senden einer nachricht)
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netdb.h>
6 #include <arpa/inet.h>
7 #include <errno.h>
8
9 int main(int argc, char *argv[]){
10 struct addrinfo aiHints, *aiResults;
11 int fdSocket;
12 int returnValue;
13 char szBuff[1024];
14
15 snprintf(szBuff, 20, "TESTMSG\0");
16
17 memset(&aiHints, 0, sizeof aiHints);
18
19 aiHints.ai_family = AF_UNSPEC;
20 aiHints.ai_socktype = SOCK_DGRAM;
21 aiHints.ai_flags = AI_PASSIVE;
22
23 // getaddrinfo("fe80::21b:77ff:fe80:bb97", "16782", &aiHints, &aiResults); // ipv6 hardcoded
24 getaddrinfo(NULL, "16782", &aiHints, &aiResults);
25
26 fdSocket = socket(aiResults->ai_family, aiResults->ai_socktype, aiResults->ai_protocol);
27 if(fdSocket == -1) perror("ERROR: socket(): ");
28 printf("socket() returned: %d\n", fdSocket);
29 returnValue = sendto(fdSocket, (const char*)szBuff, strlen(szBuff),0, (struct sockaddr*)aiResults, sizeof (struct sockaddr));
30 printf("sendto() returned: %d\n", returnValue);
31 perror("sendto() ERROR: ");
32
33 return 0;
34 }
~
|
udpsrv.c (zum empfangen von nachrichten)
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <sys/types.h>
5 #include <netdb.h>
6 #include <arpa/inet.h>
7 #include <errno.h>
8
9 int main(int argc, char** argv){
10 short sMyPort = 16782;
11 int fdSocket;
12 int returnValue;
13
14 char szMessageBuffer[4096];
15
16 struct addrinfo aiMyAddress, *aiResults;
17
18 memset(&aiMyAddress, 0, sizeof aiMyAddress);
19
20 aiMyAddress.ai_family = AF_UNSPEC;
21 aiMyAddress.ai_socktype = SOCK_DGRAM;
22 aiMyAddress.ai_flags = AI_PASSIVE;
23
24 getaddrinfo(NULL, "16782", &aiMyAddress, &aiResults);
25
26 fdSocket = socket(aiResults->ai_family, aiResults->ai_socktype, aiResults->ai_protocol);
27 if(fdSocket == -1) perror("ERROR: socket(): ");
28 printf("socket() returned: %d\n", fdSocket);
29
30
31 returnValue = bind(fdSocket, aiResults->ai_addr, aiResults->ai_addrlen);
32 if(returnValue == -1) perror("ERROR: bind(): ");
33 printf("bind() returned: %d\n", returnValue);
34
35 do{
36 recvfrom(fdSocket, szMessageBuffer, 4095, 0, NULL, NULL);
37 printf("RECEIVED: %s\n", szMessageBuffer);
38 } while(strcmp(szMessageBuffer, "QUITYOUFUCK") != 0);
39
40 return 0;
41 }
|
wie man sieht, ganz einfache versuche zu senden / empfangen ohne viel schnickschnack. würde mich freuen wenn mir einer helfen könnte den fehler zu finden.
mfg
bauser