Sinisterly
Async sockets recv - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Async sockets recv (/Thread-Async-sockets-recv)

Pages: 1 2


Async sockets recv - addicted2you - 01-17-2014

I'm using winsock 2 async sockets. Because I've read on MSDN that using send() will not always send all the bytes so I've wrote a function to ensure all the bytes are send

Code:
int bufsize = strlen(sendstring); int offset = 0; int r; //Counting the sended bytes while (offset < bufsize) { r = send(sock, sendstring+offset, bufsize-offset, 0); if(r <= 0) break; //SOCKET_ERROR or other errors if(WSAGetLastError() == WSAENETRESET || WSAECONNABORTED || WSAENETUNREACH || WSAECONNRESET || WSAENETDOWN) return 0; offset += r; }

My packets are serialized and end with a '\r\n'. But I'm thinking about this scenario:

1 ) The server is trying to send me a packet, but it fails to send all the data at once and send it via 2 send() commands;
2 ) The client (async socket) is receiving the first packet but will it contain all the data, or the packets will arive as 2 (because 2 send()'s are used)

And if the packets will arrive as 2 packets, can someone give me an example how to append the two packets (having in mind that I'm using async sockets). Now my code for receiving packets is:

Code:
case FD_READ: { CreateThread(NULL, 0, HandleRecv,(LPVOID)wParam, 0, &thread); }

Any suggestions ?


RE: Async sockets recv - ArkPhaze - 01-18-2014

The answer to this question is more obvious to me than it would seem to you based on the question being asked here. Yes, it'll be sent as 2 packets because if it was only sent as one, how would the send() command determine magically, that it's anticipating a second call to append to the first piece of data sent, to be considered a single packet? This reversely answers your second question where you'll need to peek at the data received in a given packet and validate whether it contains all of the data or not. For a trivial example of how this may be done, to show you what I mean, imagine sending a PNG via raw bytes over a socket connection in chunks or multiple packets. How would the receiver determine whether all of the bytes have been received? Check the data for the "IEND" string at the end of the bytes of the packet in question. the "IEND" chunk determines the end of the PNG structure, thus when this is seen in the data being sent, you know that this is the last packet being sent.

I hope you're sending data in chunks rather than all at once too.


RE: Async sockets recv - addicted2you - 01-19-2014

Yes, but this popup another questions in my mind. For example how to understand which socket is sending the data and how to strcat the data to the old one. How to manage to do all that, because I'm having lots of connections at once. My first thought was to keep the old data in global variables, when receive data on the async and call FD_READ to CreateThread to read the data, but have to check for the socket number with a for loop, and this will slow down a lot my rat. Any other suggestions or example code ?!


RE: Async sockets recv - ArkPhaze - 01-21-2014

Encapsulate that within a class and assign a socket a specific ID if you need to identify them then. You want to concatenate data to another socket? What do you mean? I'm not helping you with your RAT though, I don't develop for malware.


RE: Async sockets recv - addicted2you - 01-21-2014

yes but how to assing a specific ID ? To include the ID of the packet in the data or ??


RE: Async sockets recv - ArkPhaze - 01-22-2014

Yes, you could put the ID into the packet being sent in.


RE: Async sockets recv - addicted2you - 01-22-2014

And then ? Is it better to read the packets directly when FD_READ is called or to create a thread and read it there ?

And what about sending files through the socket ? Can u give me an example with async file transfer sockets ?


RE: Async sockets recv - addicted2you - 01-22-2014

Also I'm having a problem with the receiving part ... When I'm (for example) listing a directory, some of the files dont show or show wrong (without file sizes and etc.) but I see that the packet is received. On the other hand, when I add Sleep(100); on the client, the server receives and handles the packets successfully, without such errors (like null file size and etc.). Can it be from the async sockets and the calculations (like decoding the packet first, then switch() and case .. the result and then adding it to the table) ?!

Would it be better to use synchronous sockets instead of async ones ? You know what I'm conding and hope that u know what it will contain (as functions)


RE: Async sockets recv - ArkPhaze - 01-24-2014

(01-22-2014, 12:17 PM)addicted2you Wrote: And then ? Is it better to read the packets directly when FD_READ is called or to create a thread and read it there ?

And what about sending files through the socket ? Can u give me an example with async file transfer sockets ?

That depends.. Are you familiar with the benefits of multi-threading? The answer to your question is not more trivial than that. I'm not writing your project for you, and as I said, this is for malware, so I'm already helping more than usual.

Quote:On the other hand, when I add Sleep(100); on the client, the server receives and handles the packets successfully, without such errors (like null file size and etc.)

Familiarize yourself with proper callbacks then. I'm assuming that you don't guarantee that the data is captured before trying to send a packet, and assume that the data was already retrieved; synchronization conflict between threads.

The problem as I see it is that you don't quite understand a basic asynchronous model yet. If that's the case, you won't have much luck in structuring your own to successfully do this asynchronously... And putting a static value in there with a Sleep() call won't suffice as a (proper) fix either in my opinion. Just waiting a certain amount of time by blocking a thread to allow other actions to happen, doesn't mean that other threads will always be ready at the end of that blocking.


RE: Async sockets recv - addicted2you - 01-24-2014

Yes, you're right. The only thing I know about the asynchronous sockets is the thing that the socket API will continue flooding the app message dispatcher with FD_READ messages (if there's information on the socket waiting to be read) despite the fact that there might be other threads working and managing other packets (they're non-blocking sockets). So can u at least give me something to read on the net about async model and how to handle the packets or an example about server/client async sockets model and how to handle packets. Because I cant find anything interesting. Only read the Beej's Guide on Network Programming but not so much of a help about async model there .. Sad