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. -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <netdb.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <netinet/in_systm.h>
-
#include <netinet/ip.h>
-
#include <netinet/tcp.h>
-
#include <arpa/inet.h>
-
-
int main()
-
{
-
-
int port = 1936;
-
char addr[15] = "2.0.0.2";
-
-
// I know you intaililize the socket with socket
-
sock = socket (AF_INET, SOCK_STREAM, 0);
-
-
// This is where i get confused
-
struct sockaddr_in addr;
-
address.sin_family = AF_INET; //we're using inet
-
address.sin_port = htons (port); //set the port
-
-
connect (sock, (struct sockaddr *) &address, sizeof(address))
-
-
//Create the packet
-
sprintf(packet, "La la la la");
-
-
//And send it
-
write (sock, packet, strlen(packet));
-
close (sock); // Close the connection
-
-
}
-
-
-
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.
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?? -
-
#include <stdio.h>
-
-
/* for EXIT_FAILURE and EXIT_SUCCESS */
-
#include <stdlib.h>
-
-
/* network functions */
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
-
-
int main()
-
{
-
int socket_desc;
-
struct sockaddr_in address;
-
int addrlen;
-
int new_socket;
-
-
char *addr = "10.0.0.146";
-
-
/* create the master socket and check it worked */
-
if ((socket_desc=socket(AF_INET,SOCK_DGRAM,0))==0)
-
{
-
/* if socket failed then display error and exit */
-
perror("Create socket");
-
exit(EXIT_FAILURE);
-
}
-
-
/* type of socket created */
-
address.sin_family = AF_INET;
-
address.sin_addr.s_addr = inet_addr(addr); // **** This is the part im not sure about ****
-
-
address.sin_port = htons(1936);
-
-
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.
-
{
-
/* if bind failed then display error message and exit */
-
perror("bind");
-
exit(EXIT_FAILURE);
-
}
-
-
}
-
-
i got it sending a string that I take from the input :),
for others, -
-
int main(int argc, char **argv)
-
{
-
int s;
-
int ret;
-
char *buf;
-
struct sockaddr_in addr;
-
-
if (argc != 4) {
-
puts("usage: send ipaddr port data");
-
exit(1);
-
}
-
-
addr.sin_family = AF_INET;
-
ret = inet_aton(argv[1], &addr.sin_addr);
-
if (ret == 0) { perror("inet_aton"); exit(1); }
-
addr.sin_port = htons(atoi(argv[2]));
-
buf = argv[3];
-
-
s = socket(PF_INET, SOCK_DGRAM, 0);
-
if (s == -1) { perror("socket"); exit(1); }
-
-
ret = sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
-
if (ret == -1) { perror("sendto"); exit(1); }
-
-
return 0;
-
}
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |