|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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:
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 |
|
#2
|
||||
|
||||
|
Define "quickly". Don't leave anything out. I breathlessly await your reply.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread). |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
Quote:
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. |
|
#5
|
|||
|
|||
|
Any good tutorial for C++ MFC socket programming where from beginning to advanced ?
Thanks for your help. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Query multiple servers quickly |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|