473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sending UDP packets.

158 100+
Hello all,

I am trying to send a few UDP packets and cant figure it fully out.

I am running under UNIX OpenBSD 4.2

Heres what I got so far.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netinet/in_systm.h>
  9. #include <netinet/ip.h>
  10. #include <netinet/tcp.h>
  11. #include <arpa/inet.h>
  12.  
  13.  int main()
  14. {
  15.  
  16. int port = 1936;
  17. char addr[15] = "2.0.0.2";
  18.  
  19. // I know you intaililize the socket with socket
  20. sock = socket (AF_INET, SOCK_STREAM, 0);
  21.  
  22. // This is where i get confused
  23. struct sockaddr_in addr;
  24. address.sin_family = AF_INET; //we're using inet
  25. address.sin_port = htons (port); //set the port
  26.  
  27. connect (sock, (struct sockaddr *) &address, sizeof(address))
  28.  
  29. //Create the packet
  30. sprintf(packet, "La la la la");
  31.  
  32. //And send it
  33. write (sock, packet, strlen(packet));
  34. close (sock); // Close the connection
  35.  
  36. }
  37.  
  38.  
  39.  
Jul 9 '08 #1
3 25548
Banfa
9,065 Expert Mod 8TB
You will not be able to use this code to send UDP packets because this

sock = socket (AF_INET, SOCK_STREAM, 0);

you will need to use SOCK_DGRAM to create a UDP socket.
Jul 9 '08 #2
kardon33
158 100+
Thanks for the help, I got that changed but am still in need of help.

Please look at lines 19, 31, 35. these are the parts im not sure exactly how to do. Mostly binding to a remote host??

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. /* for EXIT_FAILURE and EXIT_SUCCESS */
  5. #include <stdlib.h>
  6.  
  7. /* network functions */
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11.  
  12.  
  13. int main()
  14. {
  15.   int socket_desc;
  16.   struct sockaddr_in address;
  17.   int addrlen;
  18.   int new_socket;
  19.  
  20.   char *addr = "10.0.0.146";
  21.  
  22. /* create the master socket and check it worked */
  23.   if ((socket_desc=socket(AF_INET,SOCK_DGRAM,0))==0)
  24.   {
  25. /* if socket failed then display error and exit */
  26.     perror("Create socket");
  27.     exit(EXIT_FAILURE);
  28.   }
  29.  
  30. /* type of socket created */
  31.   address.sin_family = AF_INET;
  32.   address.sin_addr.s_addr = inet_addr(addr); // **** This is the part im not sure about ****
  33.  
  34.   address.sin_port = htons(1936);
  35.  
  36.   if (bind(socket_desc,(struct sockaddr *)&address,sizeof(address))<0) // Could someone explain this function and how im using it, I took it from a website and dont fully understand.
  37.   {
  38. /* if bind failed then display error message and exit */
  39.     perror("bind");
  40.     exit(EXIT_FAILURE);
  41.   }
  42.  
  43. }
  44.  
  45.  
Jul 10 '08 #3
kardon33
158 100+
i got it sending a string that I take from the input :),

for others,

Expand|Select|Wrap|Line Numbers
  1.  
  2. int main(int argc, char **argv)
  3. {
  4.   int s;
  5.   int ret;
  6.   char *buf;
  7.   struct sockaddr_in addr;
  8.  
  9.   if (argc != 4) {
  10.     puts("usage: send ipaddr port data");
  11.     exit(1);
  12.   }
  13.  
  14.   addr.sin_family = AF_INET;
  15.   ret = inet_aton(argv[1], &addr.sin_addr);
  16.   if (ret == 0) { perror("inet_aton"); exit(1); }
  17.   addr.sin_port = htons(atoi(argv[2]));
  18.   buf = argv[3];
  19.  
  20.   s = socket(PF_INET, SOCK_DGRAM, 0);
  21.   if (s == -1) { perror("socket"); exit(1); }
  22.  
  23.   ret = sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
  24.   if (ret == -1) { perror("sendto"); exit(1); }
  25.  
  26.   return 0;
  27. }
  28.  
  29.  
  30.  
Jul 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: pigeon | last post by:
I have 2 users that their client software must be going crazy.. they are sending packets every .02 seconds to the db server... I know this because I stuck a sniffer on teh traffic.. but now i just...
5
by: Glenn Wilson | last post by:
I am writing a network system for project I am working on. How would I send a class or structure to the clients and recieve one back. Or how would I go about building a packet with a header and...
6
by: Ryan | last post by:
Hi, I am confused with how NetworkStream works. My application needs to handle heavy requests sent through TCP socket connection. I use NetworkStream.Read method to get the stream...
7
by: D. Patrick | last post by:
I need to duplicate the functionality of a java applet, and how it connects to a remote server. But, I don't have the protocol information or the java source code which was written years ago. ...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
6
by: White Spirit | last post by:
I can send data over a raw socket in C#. The part that is currently a problem is constructing the packet to be sent. To construct the packet, is it necessary to construct the packet 'by hand' and...
5
by: jaco.versfeld | last post by:
Hi There, I have a basic TCP client and TCP server in C++. The TCP client connects to the server, and after a setup phase starts to transmit a file to the TCP server using multiple packets...
5
by: scripteaze | last post by:
ok, im new to this sort of coding so excuse me if im not exactly sure as to what i need to pull this off. I need to be able to send a rip1 request to my rip1 enabled device., so i need python to...
2
by: =?Utf-8?B?R3JlZ0lJ?= | last post by:
Hi All, I have some problems with sending UDP packets using Winsock. I tried to send some to a closed port, and according to the documentation on Microsoft MSDN site...
1
by: Polyhymnia | last post by:
Hello, I get AccesViolationException with a code when I try to send a packet back to a server. I tried another way with my packet sniffer dll, there I get a SEHexception. Here is my code I used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.