Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » UDP connection
UDP connection [message #36005] Wed, 18 April 2012 10:00 Go to next message
Wolfgang is currently offline  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 #36006 is a reply to message #36005] Wed, 18 April 2012 10:10 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Check Urr package in bazaar.
Re: UDP connection [message #36021 is a reply to message #36006] Thu, 19 April 2012 14:59 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
thank you, i can connect and send but i dont know how to listen - there is nothing in the client example about that (the example just shows how to call and get the response)
Re: UDP connection [message #36022 is a reply to message #36021] Thu, 19 April 2012 15:25 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
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 #36023 is a reply to message #36022] Thu, 19 April 2012 16:03 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
I dont see...

on udp:3600 a program sends output and i want to listen to it..

you say i have to open another port and bind it to udp:3600?

Can you give me an example.... I had opened a server and i connected a client but dont know how to bind.
Re: UDP connection [message #36026 is a reply to message #36023] Thu, 19 April 2012 20:32 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
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 #36028 is a reply to message #36026] Thu, 19 April 2012 21:24 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
Thank you but with this example I just get a sock bind error if i try to connect to my UDP Socket 3600 (there the program which sends informations is)...
Re: UDP connection [message #36031 is a reply to message #36028] Fri, 20 April 2012 08:14 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Maybe you should decide whether you need client or server.
If you get bind error that means that port 3600 is already taken by another program.

You should do some preliminary tests with netcat.
To run UDP server just type:
nc -u -l -p 3600

UDP client:
nc -u 127.0.0.1 3600
Re: UDP connection [message #36033 is a reply to message #36031] Fri, 20 April 2012 09:34 Go to previous messageGo to next message
Wolfgang is currently offline  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 #36034 is a reply to message #36033] Fri, 20 April 2012 10:16 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
It appears that hdeam is a server. So you need a client.

Keep in mind that UDP is connectionless protocol and sever doesn't know that client has opened a socket. Client has to send something to get response or wait for broadcast. Try to use wireshark or tcpdump to see communication protocol between hdeam and ldeam.
Re: UDP connection [message #36035 is a reply to message #36034] Fri, 20 April 2012 10:23 Go to previous message
Wolfgang is currently offline  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

Previous Topic: Linux external libs
Next Topic: Zooming layout in Windows
Goto Forum:
  


Current Time: Tue Apr 23 09:31:36 CEST 2024

Total time taken to generate the page: 0.03840 seconds