🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Yet Another Winsock 2 Question (parsing input)

Started by
4 comments, last by TipTup 23 years, 10 months ago
When I receive something is there a way to parse the incoming results? Also is there a way to determine exactly how much data per line so I dont get those alt characters at the end? ie. Incoming: player:0 x:0 y:0 How do I grab the 3 0s? I tried using a char * and writing a substring function but that failed... is there an easy way that doesnt use a string class? Also when you receive 256 and there is only 128 bytes of data, you end up with 128 alt characters appended onto it... any way to fix that? (ie. get the amount of data before accepting it? I know this can be done very easily if my first question is answered, ie: Incoming:29043 29043 is the amount of data to be readed in ;P -TipTup TipTup.Com
Advertisement
Greetings. This is my first post here

Firstly, i am wondering why you are sending text, rather than binary data. If you are generating the data, you can send information on the length etc.
For example, you can send a packet detailing what the following data is, and infer from that the length (you suggested this with the ''incoming:xxxx'' but it should be in the previous packet to your data).

If you don''t have control over the sent data, then the alternative that i use is to reallocate memory.

an example:
    /* example assumes you have already opened socket etc    also, this is a windows program - hence the windows memory    functions    sock is the socket.   the DataAvail function is one i wrote that uses select to check for data - i use blocking sockets & threads for preference. leave this check out if you are using asynchronous sockets and KNOW there is data.*/	char *pnt;	HANDLE Hdata;		int total=0;	do{if((Hdata=LocalReAlloc(Hdata,total+1024,LMEM_ZEROINIT))==NULL)		 return FALSE;		pnt=(char *)LocalLock(Hdata)+total;		if(DataAvail(sock))			len=recv(sock,pnt,1024,0);		else		{			closesocket(sock);			return FALSE;		}		LocalUnlock(Hdata);		total+=len;	}while(len>0);	printf("\nDone: %u characters received.\n",total);    


hope this helps/makes sense.

of course, you can disregard all of the previous, if, once you read in your data (128 bytes in a 256 byte memory block) you take into account that the return value from recv is the number of bytes recieved.

-- Wyzfen
never send text data, unless it is a typed conversation or something. Do something like this
    struct MESSAGE{  int player;  int x;  int y;}MESSAGE myMessage;myMessage.player = 0;myMessage.x = 0;myMessage.y = 0;// on the sending sidesend(address, (char*)myMessage, sizeof(MESSAGE), otherParameter);// those arent all the parameters and they may not even be in the right order but you get the idea// on the receiving sideMESSAGE myMessage;receive(address, (char*)myMessage, sizeof(Message), otherStuff);// same comment as aboe on the send function    

now on the receive side
myMessage.player = 0;
myMessage.x = 0;
myMessage.y = 0;


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Thanks for the help it makes a lot of sense. =)

-TipTup
TipTup.Com
hello!
i would like to use the code above (myMessage and so on) but if i tried to compile it, the compiler hangs at the send command
because he needs a char FAR pointer to the data and the conversion from int* to char* doesnt work... (char*)myMessage...

can anybody help me?
im using msvc++5

thanks for all
elmo
my fault it should be


send(address, (char*)&myMessage, sizeof(MESSAGE), otherParameter);

notice the address of operator in front of myMessage now
the same thing needs to be done in the recv function


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

This topic is closed to new replies.

Advertisement