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
Re: UDP connection [message #36026 is a reply to message #36023] Thu, 19 April 2012 20:32 Go to previous messageGo to previous message
Zbych is currently offline  Zbych
Messages: 326
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);
}
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Linux external libs
Next Topic: Zooming layout in Windows
Goto Forum:
  


Current Time: Wed May 08 11:26:12 CEST 2024

Total time taken to generate the page: 0.02905 seconds