Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Winsock send/receive method

This topic is 5808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, being the cunning little bastard that I am, I have devised a way to send data all at once, verusus sending several seperate sections, a few for the size of the main data, then the actual data sent. Please tell me if you have any suggestions on this: -- WRITE / SEND --

	void g_send(SOCKET connsck, char* data)
		char prefix1, *prefix2;
		char *sendbuffer;
		int size = 0;
		prefix2 = new char [5];
		// 1. Get the size of the data string

		size = strlen(data);
		// 2. Put that length in an ASCII string [prefix2]

		itoa(size, prefix2, NUMBER_BASE);
		// 3. Get the length of prefix2

		size = strlen(prefix2);
		// 4. Put that length (of prefix2) in prefix1 (as an ASCII character)

		itoa(size, &prefix1, NUMBER_BASE);
		// Assemble the string to be sent

		sendbuffer = new char [1 + strlen(prefix2) + strlen(data)];
		sendbuffer[0] = prefix1;
		strcat(sendbuffer, prefix2);
		strcat(sendbuffer, data);		
		send(connsck, sendbuffer, sizeof(sendbuffer),0);
		delete [] prefix2;
		delete [] sendbuffer;
char* g_recv(SOCKET connsck)
		char *buffer1, *buffer2, a;
		int len;
		// Determine how much to read to get the length of the data

		recv(connsck, &a, 1, 0);
		len = atoi(&a);
		// Get the length of the data

		buffer1 = new char[len];
		recv(connsck, buffer1, len, 0);
		// Put that length in [len]

		len = atoi(buffer1);
		// Set the size of the buffer

		buffer2 = new char [len];
		// Read the data

		recv(connsck, buffer2, len, 0);
		delete[] buffer1;
		return buffer2;
====EXAMPLE / LAMEN'S TERMS==== --> The original string: "Hello, My Name Is Zach. I have a great life" --> Would turn out to be: "22DHello, My Name Is Zach. I have a great life" --> 2 - 2D (45 in decimal) is 2 chars long --> 2D - The string is 45 (44?) chars long --> Hello,.... - the string itself ================================ ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice -- [edited by - zackriggle on December 23, 2002 2:19:51 PM] [edited by - zackriggle on December 23, 2002 4:17:18 PM]

Share this post

Link to post
Share on other sites
You''re on the right track here, but I think you''d find that your code would be much simpler if you were to send the length of the string as a binary value rather than an ASCII one. Consider the following:

void g_send(SOCKET connsck, char* data)
// find the length of the string
int len = strlen(data);

// send the length as a 32-bit integer
send(connsck, &len, sizeof(int), 0);

// and then send the actual string
send(connsck, data, len, 0);

I''ve made a few assumptions and omissions for the sake of brevity. First, I assume that the computer on the receiving end uses the same byte order as the sender. Second, I assume that the data parameter points to a null terminated string. And finally, I omit any error handling. You''d want to take each of these issues into consideration.

One problem you may run into is that streaming sockets don''t maintain record boundaries. This means that the system is free to break up or join packets as it sees fit. You might call send() twice, but then have to call recv() three times to get all of the information. This means that most applications end up doing some sort of buffering whenever they receive data. You might want to make sure you''ve got it all before you go ahead and do anything with it.

I hope this helps. If you have any questions, feel free to ask.

- Matt

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!