Networking&Text files; A string or two, and LOTS of int's
Hey all, I want to thank anyone who helps out ahead of time. Thanks!
I have a two part problem, but first. I am writing a program that computes primes and then computes factors. I am looking to be able to port this over many computers similiar to a cluster, but without the need for a cluster. I am going to have one program running as the server and another program running on each client. The server is going to divide the work of computing the primes and record them to a list. Then compute the factors of the non-prime numbers. My problem is that I need to find a way to send a client a long list of numbers in a single packet(so as to not cause massive latancy[sorry for spelling]). Then the server is going to record the primes into one file, and the factors into another file. I need to be able to read and write to the files so they must be in a logical format. Here is what I'm hoping to files to look somewhat like: Primes.dat: 1 2 3 5 7 11 13 17 19 23 etc....... Factors.dat: 4=2x2 6=2x3 8=2x4 9=3x3 10=2x5 12=2x6,3x4 14=2x7 15=3x5 16=2x8,4x4 etc...... I need to be able to read the prime numbers and add them into a vector so that I can send them to a client. This way I can divide up the work. For instance: Server says to Client1"check the range [100-200] with primes [5,7,11,13]. Server says to Client2"check the range [200-300] with primes [5,7,11,13]. etc.... I have divised a way to send a one byte packet to a client which would tell it what kind of data to expect and what it's job will be. Like one control packet would tell it that it is about to be sent it's range. Then it is sent '100-200' The next packet would be a control packet telling it that it's list of primes will be updated. then it is sent '5,7,11,13'. Any Ideas? Anyone? |
Doable, but why not to send everything as one message? You can use a simple format like (example):
Code:
2\n100-200\n5,7,11,13\n Is there a reason you want to divide the messages? |
WOW!! Hehe, I can be easily excited sometimes. If it is possible to send it in that format it would be amazing. My problem is that I have limited knowledge of the code that would parse(or other reasonable word) the information from that packet from the char buffer into something usable.
like: char buffer[big_global_decent_buffer_size_here]; buffer now equals: 2\n100-200\n5,7,11,13\n how to I get that in to 3 int's and a vector? int control_number, low_range, high_range; vector<int> primes; how do I get it to now be: control_number=2; low_range=100; high_range=200; primes=5,7,11,13; Here is what I have for the basic testing server so far: Code:
int startupServerForListening(unsigned short port); |
The code will look like this:
Code:
int nBytes=recv(clientSocket,buffer,MAX_MESSAGE_SIZE,0); isDigit - true if the char given as parameter is digit charToInt - converts char (digit) to int error - does something when there's an error (the message is incorrect) BTW If winsock works like Unix sockets, your recv syntax looks wrong. Second parameter is buffer, not its size. |
The winsock thing, check it again. I've designed it to send a number first that will tell how long the data file will be, then send the data file. It works, I've tested it thoroughly. Unix is different? hmm... Then I guess it should be interesting when I port this to unix and linux.
Thanks a lot for that section of code, that should be awesome. Hmm.... Ok, I think I can write the code for the charToInt() pretty easily it would just be a 10-11 case switch statement, and the isDigit() would be the same. Hmm, But I'm wondering about your code. Is this what you meant? Code:
while(buffer[i]!='-'){ Now, what about the code that would save to a data file? I'm thinking that a very similiar section of code would work very well. What I was thinking is that the client would use a single control number to send the primes, ie:3. and another number to send the factors, ie:4. I wanted to use these control numbers so that if and when my program needs to send extremely large amounts of data it could send it in parts and use to the control numbers to signify what that data means. |
Quote:
Not sure what do you mean by the code fragment. It looks rather OK :) Quote:
Quote:
|
What about a technique for finding a server? I think I've heard it called subnet broadcasting, but I could be wrong. I need some way to find the server without manually telling the client the ip of the server. Any ideas?
|
Right now it works easily because I am testing it on the same computer. Both client and server are running on one computer. But i need a way to move them to two different computesr.
|
You have two options: giving the server's IP as parameter (or in a configuration file) or broadcast. The second option has limitations - increased traffic and works only in the same subnet (if server and client are in different ones it won't work).
To use broadcast in your code (again, in Unix socket implemantation) you need to setup a socket with broadcast address (like 192.168.1.255 when network is 192.168.1.0), set the socket to broadcast (setsockopt with SO_BROADCAST). Then you can just use sendto to set the data as usual. Of course, only server should wait for the data. |
Thanks Mara, you've been a wonderful help. Thanks soo much for the sample code you gave me it really helped. I completed the project I was working on and now feel satisfied with it's completion. But, as with the case with many great learners, I have found another project. It too deals with networking.
As for the final question about how to send a packet to all computers on a LAN or subnet you would send the packet to the broadcast address. This can be found on Windows by typing 'ipconfig' at a DOS prompt or in Linux by typing 'ifconfig' in the shell. I will post a new thread with a discription of my new project. Thanks all!!! |
All times are GMT -5. The time now is 01:41 AM. |