Archived

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

Ivko

Argh.. what's wrong with this function

Recommended Posts

First of all..
void ProcessInput(Client_t *d) {
	int ret = 0;
	char *rd = "";

	ret = recv(d->Socket, rd, 1, 0);

	/* Check how the read went */
	if(ret == 0) {
		/* They closed the connection on their end */
		NET_Drop(d, "client dropped link");
		return;
	} else if(ret < 0) {  // An error happened
		if(!(WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINTR)) {
			printf("Unable to read from socket! (WinSock Error #%d)\n", WSAGetLastError());
			G.Shutdown = TRUE;
			return;
		}
	}

	/* We read a character, add it to clients input buffer unless it's a newline. If it is, process the command */
	NET_WriteToSocket(d->Socket, rd);  // Echo text back to client
	if(*rd == '\n') {
		ProcessCommand(d);
	} else {  // Add the character to input buffer
		/* Make sure the buffer isn't full, if it is then return without adding the character */
		if(strlen(d->Input) > 1022) {
			d->Overflow = TRUE;
			return;
		}
		printf("(%s)", rd);

		strcat(d->Input, rd);
	}
}
  
As you can see, I'm reading a character from a socket. If it's a newline, it gets sent to a function to process whatever's in the buffer (and the newline is NOT supposed to get added to the buffer). If it's not a newline, it just gets added to the client's input buffer. My problem is, if it's a newline it's going to the command processor like it's supposed to, but it's ALSO getting added to the input buffer. If it's not a newline, it works right (it gets added to the buffer and the command processor is not called). Why is it doing this? Why would it call the ProcessCommand() function the continue going to the else? Edited by - Ivko on October 16, 2001 11:59:46 PM

Share this post


Link to post
Share on other sites
EDIT: Nevermind this problem, I fixed it by initializing one of my ints to 0 :] Still can't figure out the above problem though.

Ok, now this is even weirder. If I compile the program under Win32 - Debug configuration in MSVC++6 it works fine except the problem i mentioned above. If I compile it under Win32 - Release though, in the NET_WriteToSocket function which echoes what they typed back I get an error #20022 (Invalid argument) from winsock, and whoever's connected doesn't get what they typed back, but instead one of the first things I sent them except screwed up looking followed by some garbage. And get this: if I add just 'printf("%s", rd);' above the NET_WriteToSocket statement, it works like it does in the debug build.

I haven't changed anything in the release settings that I have in the debug. Can someone please explain what's happening here?

Edited by - Ivko on October 16, 2001 11:58:11 PM

Share this post


Link to post
Share on other sites
Processing end of lines is a bit iffy because every system does it differently. Win32 uses CR and LF (\r\n) and the c-runtime tries to hide it from you so that you can just use \n and it will work most of the time. It will fail to work just enough to make life really difficult sometimes. If you check the hex values of the buffer that gets passed to ProcessCommand I''ll bet you have a \r character (0x0A).

Unix uses just LF (\n). I believe Macs use just CR (\r) just to be really annoying.

For your other problem the only thing that jumps out is this:

char *rd = "";

That, IMHO, is a really strange way to do it and could get you in trouble if the compiler decides to put "" in read-only memory. I would change this to be a simple char that you pass the address of instead of this funky string/char thing.

-Mike

Share this post


Link to post
Share on other sites