Hello to everyone !!
I am using winsock in an application to communicate with some services in a request-response manner but multiple requests can happen at the same time in the same socket because it is kind of a complex system having a loadbalancer and a relay server sitting in the middle i will paste here only the code that I'm not sure that will do the job its written in c.
Im using 6 byte in order to set the length of the message then in a second recv i read until that length is reached.
This is a typical message i am using a delimiter of some kind at the end too for the message processing also the message is base64 and url encoded
000271Rg2hPgN17awwZJrrd5jm/CXWd2979rh4MV5w4yxGoBXQ4BPqLutM5VwE4gWLPA2F64Wc43Tks2eymzV9E3MOp23izNtnFoPmz8wr/OAvFC2OPE+cImMj8fxo9rL6o0K1Pa01kOHgccVE3Bgv4odOHpB1vcQCkDX/Y/ltlCdzw+XbTxZ47LkJdVB8LVAUOCqepHfeSMIuQvTHQh2jwWnuND/mG5OtYGZGgBUpNHopkkT61L4y588QPDcCUi/dxRq141aa5AqcG2o=[^]
I would like to ask you the following
- Do you think this is sufficient for the task to receive reliable all the messages.
- Can i improve the code or fix any problematic part.
- In general suggestions to take it further
I had many problems with missing data but at this state it looks stable with the little testing that i did with 2 clients together asking data also this code run in a thread and I'm opening a thread like that.
HANDLE hHandle = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)Th,NULL,NULL,NULL);
Thanks a lot for your time !!!!!!!!!!!
fstream afile;
int HEADER_SIZE=6;
int sizeFirstBuffer = HEADER_SIZE*sizeof(char);
int finalResult = 0;
char* RCV_BUFFER_SECOND;
bool isRunning = true;
while (isRunning) {
char* RCV_BUFFER=(char*) malloc(sizeFirstBuffer+1);
int result = 0;
for(int i=0;i<HEADER_SIZE;i++){
RCV_BUFFER[i]=' ';
}
RCV_BUFFER[sizeFirstBuffer]='\0';
int sizeThatIHaveToRead= 0;
while(sizeThatIHaveToRead < 30 || result < sizeFirstBuffer){
result += recv(tcp_socket,RCV_BUFFER,sizeFirstBuffer - result,0);
handleError();
if (result < 0){
free(RCV_BUFFER);
free(RCV_BUFFER_SECOND);
isRunning = false;
break;
}
sizeThatIHaveToRead = atoi(RCV_BUFFER);
}
free(RCV_BUFFER);
if( sizeThatIHaveToRead == 0) {
free(RCV_BUFFER_SECOND);
continue;
}
afile.open(outpath, fstream::in | fstream::out | fstream::app );
afile << "" << endl;
afile.close();
int sizeSecondBuffer = sizeThatIHaveToRead*sizeof(char);
RCV_BUFFER_SECOND=(char*) malloc(sizeSecondBuffer+1);
for(int i=0;i<sizeSecondBuffer;i++){
RCV_BUFFER_SECOND[i]=' ';
}
RCV_BUFFER_SECOND[sizeSecondBuffer]='\0';
finalResult = 0;
while((sizeSecondBuffer - finalResult) > 0 ){
finalResult += recv(tcp_socket,RCV_BUFFER_SECOND,sizeSecondBuffer - finalResult,0);
handleError();
}
sizeThatIHaveToRead = 0;
PostMessage(hWndMain,WM_USER+8,sizeSecondBuffer,(LPARAM)RCV_BUFFER_SECOND);
/*if(reconnects>1){
Sleep(100);
free(RCV_BUFFER_SECOND);
}*/
}