Click here to Skip to main content
15,299,443 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to send a UDP packet to a ip or domain and i don't want to receive eny packets. Just that simple, Just answer how to:

1: Make the socket.
2: Make it so that the packet will be sent to ip.
3: Send the packet.

That's all. :)

What I have tried:

i have just tried googleing it and i just found tutorials that gave me errors, probably out dated tutorials.
Posted
Updated 19-May-16 9:45am
Comments
Matt T Heffron 19-May-16 14:19pm
   
What kind of errors did you get from what you tried?
Run-time? Compile-time?

Since we don't know WHICH tutorials you tried, we can't even evaluate if they're good or bad examples.

I just found this: http://www.binarytides.com/programming-udp-sockets-c-linux/
Their Client example is pretty straightforward.
To send without listening, just skip the recvfrom part.
(I'm not a LINUX developer.)
Sladetbask 19-May-16 14:40pm
   
This is the error it gave me:

demo-udp-05.c: In function ‘int main()’:
demo-udp-05.c:31:21: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
die("socket");
^
demo-udp-05.c:47:9: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(message);
^
demo-udp-05.c:47:21: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(message);
^
demo-udp-05.c:52:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
die("sendto()");
^
demo-udp-05.c:59:77: error: invalid conversion from ‘int*’ to ‘socklen_t* {aka unsigned int*}’ [-fpermissive]
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from demo-udp-05.c:8:
/usr/include/x86_64-linux-gnu/sys/socket.h:174:16: error: initializing argument 6 of ‘ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)’ [-fpermissive]
extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
^
demo-udp-05.c:61:29: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
die("recvfrom()");
^
demo-udp-05.c:70:14: error: ‘sleep’ was not declared in this scope
sleep(1)
^
demo-udp-05.c:72:7: error: expected ‘;’ before ‘if’
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
^
Matt T Heffron 19-May-16 15:24pm
   
You've lost me.
I don't use LINUX and I'm not familiar with these specific compiler messages.
However, I think:
the 'sleep" error means you need #include <unistd.h>

You stated that you didn't need to get any data back, so you should be able to delete any use of recvfrom. Or change the declaration of slen from int to be socklen_t.

The last error you show looks like there is a missing ; typo in the code. (On the sleep(1) line?)

The others are warnings of doing things that are unsafe.
Switch the gets(message) to fgets(...).
gets is inherently unsafe as there's nothing to prevent more than input than will fit in the buffer passed to it.
Albert Holguin 23-May-16 17:47pm
   
There are a ton of resources online that give examples on this topic. Please try another one if one doesn't compile off the back.

1 solution

I compiled the same sample code (I suppose[^]), on my Lubuntu 15.10 Box.

Got the following output:

(build process)
gcc -g    -c -o udp-send.o udp-send.c
udp-send.c: In function ‘main’:
udp-send.c:54:6: warning: implicit declaration of function ‘inet_aton’ [-Wimplicit-function-declaration]
  if (inet_aton(server, &remaddr.sin_addr)==0) {
      ^
udp-send.c:75:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
  close(fd);
  ^
gcc -o udp-send udp-send.o 
gcc -g    -c -o udp-recv.o udp-recv.c
gcc -o udp-recv udp-recv.o


(server execution)
~/devel/junk/demo-udp-04$ ./udp-recv 
waiting on port 21234
received message: "This is packet 0" (16 bytes)
sending response "ack 0"
waiting on port 21234
received message: "This is packet 1" (16 bytes)
sending response "ack 1"
waiting on port 21234
received message: "This is packet 2" (16 bytes)
sending response "ack 2"
waiting on port 21234
received message: "This is packet 3" (16 bytes)
sending response "ack 3"
waiting on port 21234
received message: "This is packet 4" (16 bytes)
sending response "ack 4"
waiting on port 21234


(client execution)
Sending packet 0 to 127.0.0.1 port 21234
received message: "ack 0"
Sending packet 1 to 127.0.0.1 port 21234
received message: "ack 1"
Sending packet 2 to 127.0.0.1 port 21234
received message: "ack 2"
Sending packet 3 to 127.0.0.1 port 21234
received message: "ack 3"
Sending packet 4 to 127.0.0.1 port 21234
received message: "ack 4"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900