|
|
Home » Community » Newbie corner » UDP connection
UDP connection [message #36005] |
Wed, 18 April 2012 10:00  |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
can some1 tell me how to connect to an UDP Socket...
I've included web.h but the only thing i get is an TCP connection...
|
|
|
|
|
Re: UDP connection [message #36022 is a reply to message #36021] |
Thu, 19 April 2012 15:25   |
Zbych
Messages: 327 Registered: July 2009
|
Senior Member |
|
|
Take a look at Server.cpp in urr.
You need to open socket and bind:
sas_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sas_sock < 0) {
RLOG("SAS SOCK ERROR");
goto End;
}
srvadr.sin_family = AF_INET;
srvadr.sin_port = htons(SAS_PORT);
srvadr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sas_sock, (sockaddr *) &srvadr, sizeof(srvadr)) != 0) {
RLOG("SAS SOCK BIND ERROR");
goto End;
}
len = 1;
setsockopt(sas_sock, SOL_SOCKET, SO_BROADCAST, (const char *)&len, sizeof(len));
and then wait for input:
struct sockaddr address;
socklen_t addr_size = sizeof(address);
ssize_t len = recvfrom(sas_sock, (char *)buffer, buff_len, 0, &address, &addr_size);
|
|
|
|
Re: UDP connection [message #36026 is a reply to message #36023] |
Thu, 19 April 2012 20:32   |
Zbych
Messages: 327 Registered: July 2009
|
Senior Member |
|
|
There are two packages in bazaar: UrrPingClient and UrrPingServer.
Here you have simple Linux udp server. On windows you need to add some winsock initialization.
#include <Core/Core.h>
#include <arpa/inet.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock < 0) {
RLOG("SOCK ERROR");
return;
}
sockaddr_in srvadr;
srvadr.sin_family = AF_INET;
srvadr.sin_port = htons(9999);
srvadr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (sockaddr *) &srvadr, sizeof(srvadr)) != 0) {
RLOG("SOCK BIND ERROR");
close(sock);
return;
}
struct sockaddr addr;
socklen_t addr_size = sizeof(addr);
char buff[256];
while(1){
ssize_t len = recvfrom(sock, buff, sizeof(buff), 0, &addr, &addr_size);
if (len > 0){
Cout() << Format("%d.%d.%d.%d: '%s'",
addr.sa_data[2], addr.sa_data[3], addr.sa_data[4], addr.sa_data[5], String(buff, len));
}
}
close(sock);
}
|
|
|
|
|
Re: UDP connection [message #36033 is a reply to message #36031] |
Fri, 20 April 2012 09:34   |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
Thats my big question, as i wrote this is the situation:
- On port 3600 UDP is a program that sends information
- I want to listen to this information
Quote: |
root@chieftec:/home/wolfgang# lsof -i | grep -i UDP
hdeam 807 root 4u IPv4 4277 0t0 UDP *:3600
ldeam 2125 wolfgang 3u IPv4 35397 0t0 UDP *:39563
|
"hdeam" sends information....
ldeam is a program that gets information from hdeam - and I want to get information, too!
In this case I think i need just a client?!
But with urrClient I don't know how to listen because I don't want to send to the socket, just read from it.
So I really don't know if I need a server and a client or just a client for this. Whats the schematic of such a connection?
- Do I have to start a own server on another port than 3600, connect through another socket to UDP 3600 and tell the server to establish a connection to my own server?
OR
- Do I just have to connect to UDP 3600 Port with a client and listen? (If yes - I tried but in UrrClient for example is no method for listening..)
[Updated on: Fri, 20 April 2012 09:53] Report message to a moderator
|
|
|
|
Re: UDP connection [message #36035 is a reply to message #36034] |
Fri, 20 April 2012 10:23  |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
okay, I'll try. Thank you very much for your help!
I already connected with nc as client twice to the udp 3600 port, if i send from nc#1 the information appears at nc#2 and at ldeam, if I send from nc#2 it appears at nc#1 and ldeam but if i send from ldeam#2 i just appears at ldeam.... thats strange, isn't it?
and... all the input / output is scrambled...
I send A at nc#1 and at nc#2 comes A��d�
[Updated on: Fri, 20 April 2012 10:29] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Apr 28 20:35:23 CEST 2025
Total time taken to generate the page: 0.04296 seconds
|
|
|