LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2016, 07:31 PM   #16
pronoob
LQ Newbie
 
Registered: Aug 2016
Posts: 11

Original Poster
Rep: Reputation: Disabled

Many thanks

Here's a rough UDP non-blocking client using select / fcntl in a class


Code:
int NetUDP_Client::create(char *server_IP, unsigned int port){
	
	echoServPort = port;	//	8888
	servIP = server_IP;		//	"127.0.0.1"
	
	//	GET A SOCKET
	if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
		//DieWithError("socket() failed");
		std::cout << "socket() failed" << std::endl;
	}
	
	//	INIT ADDRESS STRUCT
	memset(&echoServAddr, 0, sizeof(echoServAddr));
	echoServAddr.sin_family = AF_INET;
	echoServAddr.sin_addr.s_addr = inet_addr(servIP);
	echoServAddr.sin_port = htons(echoServPort);
	
	return 0;
}

int NetUDP_Client::send_msg(char *echoString){
	
	int BUFLEN = 255;
	unsigned int max_buf = BUFLEN;
	char echoBuffer[BUFLEN+1];
	
	//	CALC SIZE OF MESSAGE
	unsigned int echoStringLen = strlen(echoString);
	
	//	SEND MESSAGE
	if(sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) != echoStringLen){
		//DieWithError("sendto() sent a different number of bytes than expected");
	}
	
	//	INIT RESULT BUFFER
	memset(echoBuffer,'\0', BUFLEN);
	
	//	RECEIVE RESULT
	response_result = timeout_recvfrom(sock, echoBuffer, &max_buf, &echoServAddr, 1);	//	int sock, char *buf, int *length, struct sockaddr_in *connection, int timeoutinseconds
	std::cout << "result: " << response_result << std::endl;
	if(response_result){
		std::cout << "response: " << echoBuffer << std::endl;
	}else{
		std::cout << "timed out" << std::endl;
	}
	
	return response_result;
}

int NetUDP_Client::timeout_recvfrom (int sock, char *buf, unsigned int *length, struct sockaddr_in *connection, int timeoutinseconds)
{
    fd_set socks;
    struct timeval t;
    
    //	SET FILE DESCRIPTOR TO NON-BLOCKING
    fcntl(sock, F_SETFL, O_NONBLOCK);
    
    //	INIT BUFFER
    FD_ZERO(&socks);
    FD_SET(sock, &socks);
    
    //	INIT TIME STRUCT
    t.tv_sec = timeoutinseconds;
    t.tv_usec = 0;
    
    //	CHECK FILE DESCRIPTOR
    int rv = select(sock + 1, &socks, NULL, NULL, &t);
    //if(select(sock + 1, &socks, NULL, NULL, &t) && recvfrom(sock, buf, *length, 0, (struct sockaddr *)connection, length)!=-1)
    
    if(rv != -1){
    	//	RECEIVE DATA
    	int n = recvfrom(sock, buf, *length, 0, (struct sockaddr *)connection, length);
		if(n!=-1)
		{
			return n;
		}
	}
	else{
		//	errno, 
	}
	return 0;
}
 
1 members found this post helpful.
Old 09-30-2016, 02:09 AM   #17
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
removed

Last edited by bigearsbilly; 09-30-2016 at 02:14 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with callback function in pcap_loop function kikilinux Programming 1 03-28-2016 07:14 AM
[SOLVED] Class callback function teapottwo Programming 2 01-12-2016 07:52 PM
[SOLVED] C++ class member pointer changes after function returns Snark1994 Programming 5 03-30-2011 06:46 PM
C++: How to put a pointer to a function, in a class template, without typedef'ing it? Aquarius_Girl Programming 3 03-05-2011 07:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration