Wuts.the.Martyr
Wuts.the.Martyr

Reputation: 71

networking and messages

okay so i am trying to be more efficient in my programming, by attempting to pass multiple strings as one char array[100]. I can pass all this info from my client perfectly fine, but am now trying to use substr or strch to remove the values i passed using the char array on the server side.

server will look like this on the other end, with the data passed from the client being stored into memory under vel_rec:

recv(sock, vel_rec, 100, 0);

The program is set up to send some basic numerical data to a threaded server to run computations on and return results. I will most likely be using atoi function to get the integer values back out of the strings and then return a result back to the client. This returned result again will be a character array.

client:

char vel_snd[100], vel_rec[100], char buffer[100]; 
memset(buffer, 0, 99); 
memset(vel_snd, 0, 99); 
memset(vel_rec, 0, 99); 
recv(sock, buffer, 100, 0);
//are we connected? 
cout << "connection status = " << buffer << endl; 
string v0, a, time, space = "\n", stop;  stop = 'g';
while (stop != "x")
{
    cout<<":::Press 'x' then 'enter' at any time to quit:::"<< endl;
    cout<<"enter data? "<< endl;
    cin>>v0;
    stop = v0;
    cout<<"some more data? "<<endl;
    cin>>a;
    stop = a;
    cout<<"even more data? "<<endl;
    cin>>time;
    stop = time;
    v0 += space + a + space + time;
    strcpy(vel_snd, v0.c_str());        
    send(sock,vel_snd,strlen(vel_snd), 0);
    cout<<"Calculating...\n";
    recv(sock, vel_rec, 100, 0);
    cout<<"\nThe data in "<<time<<" seconds will be "<<vel_rec<<endl;
}

okay so this is all fine and dandy, I am sure there exist infinitely better solutions to concatenation of the null characters which I am not even sure will work, just left them there for you to pick over if you can use them on the server side of things. I was attempting to use them to break out my original strings, but without success. I just want the server to receive this char array and copy sections of it into strings. I am assuming this is possible, but if not I could always send multiple char arrays through, it just seems to me that they would get jumbled up on the server if not properly flagged and organized as the data comes in. one in - one out, seems very clean to me and preferable honestly. The use of strings may not even be needed here as well, I just thought it would be better than using int values and converting, when i can just pass them as strings to begin with. Either way, I still need to implement some method of breaking out the values sent to the server from the client and the substr doesn't want to work with the char array.

I am also incredibly unfamiliar with anything networking and this is my first attempt at it from some tutorials i have seen. I had a 30 min lecture on the subject, with no further explanation other than "it works". That being said please be constructive in your responses. Hopefully I can learn a bit more than just this, since I plan on implementing something similar in a networked game I am working on. oh and before i forget this is using wsock32.lib.

recap:

[client] 
char array1[100] = string 1 + string 2 + string 3 
server<--char array1 
char array2<--- server 
print 
[server] 
char array1<---client string 1 + string 2 + string 3 
int A = string 1 
int B = string 2 
int C = string 3 
<some math...> 
<char array 2 = itoa  A B C> 
client <--- char array2

Thanks a bunch

Shawn

Upvotes: 0

Views: 154

Answers (3)

Wuts.the.Martyr
Wuts.the.Martyr

Reputation: 71

well i finally just broke down and sent separate char arrays each containing the necessary values to the multithreaded server, did atoi, did the calculations, stored the int calculated result back using itoa, and sent it back to the clients. Just needed to be smacked in the face with KISS. Tried making a gui to go with it in vs2010 but that was a headache, so switched to Qt, so i will be back with questions on that I am sure. Thanks for all the replies. I tried to vote, but I am too low on reputation.

Upvotes: 0

Chris Condy
Chris Condy

Reputation: 636

Not sure I understood the question but a simple technique would be to for example, everytime the client enters something it will be newline terminated. Make the and when you have finished inputting data then send that message. When the message is then sent you automatically send the size of the array to the server. So for example on the server side:

char* temp;
int i=0;
nr=read(sd,buf,sizeof(buf));
temp = strtok(buf,"\n");
//Got the first token within the string.
//I'm pretending its an int to switch case the server
switch(i){
case 1:
temp = strtok(NULL,"\n");
//Get the next token
printf("CASE 1 with: %s",temp);
break;
}

etc...

I have not tested this code and just wrote it from the back of my mind so if there is faults in syntax I oppologize.

Good luck

Upvotes: 1

esskar
esskar

Reputation: 10940

suppose you store the received data in std::string received, then you can split it using this function:

std::vector<std::string> string_split(const std::string & str, char delim) 
{
    std::vector<std::string> elems;

    std::stringstream strstream(str);
    std::string item;
    while(std::getline(strstream, item, delim))
        elems.push_back(item);    

    return elems;
}

then call

std::vector<std::string> messages = string_split(received, '\n');

EDIT

Btw, you can replace this code

char vel_snd[100], vel_rec[100], char buffer[100]; 
memset(buffer, 0, 99); 
memset(vel_snd, 0, 99); 
memset(vel_rec, 0, 99); 

with this code

char vel_snd[100] = {0}, vel_rec[100] = {0}, char buffer[100] = {0}; 

not exactly the same, but good enough when working with char arrays.

Upvotes: 1

Related Questions