C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 3rd, 2008, 03:44 PM
Stack Overflow Stack Overflow is offline
C Programmer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 15 Stack Overflow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 15 m
Reputation Power: 0
Send a message via ICQ to Stack Overflow Send a message via AIM to Stack Overflow Send a message via MSN to Stack Overflow Send a message via Yahoo to Stack Overflow
Query multiple servers quickly

Hello,

I am trying to determine the best possible solution to accomplish a simple, yet complex task. (Regarding Socket Programming)

I've been working on a new project for quite some time and have written a custom framework from the ground up. It's more or less a (Client --> Server --> <-- Server) system.

So far I have the basics done. Here's the process:
  1. I expect to have one client connect to my listening server.
  2. That client will periodically send me data -- a string of IP Addresses (10 or more at a time).
  3. When I receive the data, I split it up accordingly and begin querying the list of IPs just received.

All of that I can handle and have already completed the code for. The string of IP Addresses I expect to receive (from the client) are of other servers in which I will attempt to query and gather data from.

However, the problem is I may need to query up to 100 or 1000 servers and I don't want to have to wait for one to finish, in case it's unresponsive and gets delayed the full timeout length before moving onto the next request.

With that said, I want to query as many servers as fast as possible. And I don't care which order they come back in. As soon as I receive the data (from the query) I just need to save it to a file.

I am writing this in C and in the Linux environment -- so is there a way to even do that? Would I need to use the fork() command or write my own custom Socket Pool to handle multiple outgoing connections and responses without delay?


Stack Overflow

Reply With Quote
  #2  
Old May 3rd, 2008, 08:43 PM
sizablegrin's Avatar
sizablegrin sizablegrin is online now
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,063 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 13 h 19 m 45 sec
Reputation Power: 1442
Define "quickly". Don't leave anything out. I breathlessly await your reply.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread).

Reply With Quote
  #3  
Old May 4th, 2008, 03:22 AM
LittleGrin LittleGrin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 127 LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level)LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level)LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level)LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level)LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level)LittleGrin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 2 h 32 m 51 sec
Reputation Power: 32
I'll also point out that the architecture you describe is quite unusual in client-server applications; in fact, it sounds like spyware installed on a client machine that communicates with a central server and that central server covertly intercepts client communications (eg username and password exchanges) with sites the client really intends to communicate with.

Reply With Quote
  #4  
Old May 5th, 2008, 03:59 AM
etienne141's Avatar
etienne141 etienne141 is offline
Paris est magique!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: France!
Posts: 346 etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 2 h 12 m 41 sec
Reputation Power: 41
Send a message via ICQ to etienne141 Send a message via MSN to etienne141
Quote:
However, the problem is I may need to query up to 100 or 1000 servers and I don't want to have to wait for one to finish, in case it's unresponsive and gets delayed the full timeout length before moving onto the next request.

With that said, I want to query as many servers as fast as possible. And I don't care which order they come back in. As soon as I receive the data (from the query) I just need to save it to a file.


That is more or less the default behaviour of standard sockets.
You could just send() your data to each server you want to address, then select() or epoll() on the sockets you connect()ed to them. Process each answer at a time.

If you want to start handling answers while still sending requests, you would launch a thread (or a pool of threads) dedicated to select()/recv().
__________________
UNIX shells are so cool!
etienne:~ > %blow
fg: %blow: no such job


There are 10 kind of people:
- those who know binary
- those who don't.

Reply With Quote
  #5  
Old May 5th, 2008, 05:20 AM
Peter_APIIT Peter_APIIT is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 22 Peter_APIIT Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 40 m 10 sec
Reputation Power: 0
Any good tutorial for C++ MFC socket programming where from beginning to advanced ?

Thanks for your help.
Comments on this post
sizablegrin disagrees: Please quit hijacking other threads with this question. Start your own.

Reply With Quote
  #6  
Old May 5th, 2008, 09:50 PM
Stack Overflow Stack Overflow is offline
C Programmer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 15 Stack Overflow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 15 m
Reputation Power: 0
Send a message via ICQ to Stack Overflow Send a message via AIM to Stack Overflow Send a message via MSN to Stack Overflow Send a message via Yahoo to Stack Overflow
Hello again,

This is geared toward obtaining data from game servers.

I'd like to show you an example of what I'm trying to do in code format:

Code:
void queryServers(int n, char data[][64]) {
	/*** n = amount of servers to query ***/
	/*** data[x] is the string holding IP and Port info ***/
	/*** Example: 123.45.67.89:10110 */

	/* local variables ... */
	int i, port;

	/* loop through all servers needed to be queried */
	for (i = 0; i < n; i++) {
		/* TODO: parse port from data[] and convert to integer */
		port = atoi( [i] );

		/* function to query server */
		queryData(data[i], port);
	}

	/* end */
	return;
}



Keep in mind that I have already written the query code. I know exactly what to query and what data I should receive. Very much like querying a game server -- you query a specific IP & Port and it dumps a string back to you (like map name, etc). I just need to save that dump information to a file.

What I'm struggling with is developing an efficient way to complete a lot of server queries without losing time if one them times out or hangs.

My code above is very incomplete, as you can probably tell, but it's the basic idea. The for loop is very bad because it only processes one query at a time and I want to find a good way to tackle a lot of them even if I have to open multiple sockets and threads to get it done.

So I'd find a way to open 10 different connections to 10 different servers and begin querying. As soon as one is done, I'd close that connection and begin querying the 11th. So on and so forth... If one of them takes fifteen seconds to complete the query and the other 9 finish sooner than that, it then frees up more slots to begin querying more servers while that slow one doesn't affect the rest of the group.

Later: I received some information that non-blocking sockets would be ideal for such a situation. So here's what I've developed since then...

As stated, I have decided to implement non-blocking sockets into my program. I have one question though:

How do you send out data and then catch the response using non-blocking sockets?

So far I have a non-blocking server that waits to see if there are any incoming connections or data, but doesn't block if there aren't. However I can't seem to find out how to query another server and have my non-blocking server pick up the response. I think it may be related to the sendto() or recvfrom() command, but I can't get my head around it.

A diagram:
Code:
     Client
	\
	 \
	  \
	Server		Non-Blocking Server
	    \		  /
	     \		 /
	      \		/
	      Game Server


So to clarify, the blue and teal colored objects are my listening servers. I use the fork() command to give my Non-Blocking Server full control when it's time to start querying the list of servers sent by the original client. I initialize the NB server only when it's time to start querying and close it down when I'm completely done.

Layout: One Client connects to my basic Server. That Client sends a string of IP Addresses to me. I parse the received data into a readable list and start querying the Server's.

Lastly, which is where I'm stuck, I need the received data from the Server's to send its data to my Non-Blocking Server -- so it doesn't block per server query in case I have 1000 servers to poll.

I have the non-blocking server configured 100%. I just need to know if it's possible to receive that data to a specific socket or port. Also, as a piece of information, my two servers operate on two different ports. For example my basic server listens on port 10110 and my NB server listens on 10111. I need the poll results to send to port 10111 so my NB server can pick it up properly.

I hope that makes sense. Thanks again for any assistance you may be able to provide.

Reply With Quote
  #7  
Old May 5th, 2008, 10:19 PM
sizablegrin's Avatar
sizablegrin sizablegrin is online now
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,063 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 13 h 19 m 45 sec
Reputation Power: 1442
Sendto and recvfrom are most often used with connectionless sockets.

Did you even look at "select", as mentioned above? Select tells you which sockets are ready to be read or written. When you ask a non-ready, non-blocking socket to read (or write), it will either read (or write) or it will tell you that it "would block" (if it weren't non-blocking). That action allows you to not waste time on non-responsive sockets.

Generally, multiple threads would not be necessary unless you had unrelated operations that needed to be served, also. In that case you could use threads or event handlers of one type or another.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Query multiple servers quickly


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway