So I wrote out about half of this log four hours ago, only for my internet to go down and blank this page, and then stay down for the next two hours.
One trivial mistake I kept making was after passing from the listening socket to the worker socket, I would still call the listening socket. This was the last bug I had before my TCP server started working. I kept trying to delete the buffer like so:
delete [] & buffer
Honestly, no idea why I ever tried that. Eventually I had to common sense to comment it out. And it also got working again. I tried to copy directly from &rNodeList.size() to a buffer with memcpy, couldn't get that working either.
The UDP server was surprisingly harder than the TCP server. I was receiving Winsock Error 10014 because I was using sizeof() wrong. Then I got Error 10013, which is a permissions issue so I had to manually add the debug executables to Windows Firewall. Turns out the Firewall had nothing to do with it. Fixed a couple things and it looped me back to Error 10049, which I had received before.
Alright, my internet's back after losing my internet over 9000 times tonight. Here are the mistakes I made converting from TCP to UDP:
Server
1. local.sin_addr.s_addr set to inet_addr(HOST) instead of INET_ANY.
2. Forgot to remove the listen() method.
3. Kept worker socket and waited with the accept method.
Client
1. Used 0 instead of IPPROTO_UDP
2. Tried using *((unsigned long*) host->h_addr) from the tutorial for the hostname instead of remote.sin_addr.S_un.S_addr, which worked.
3. Forgot to remove the connect block and that's where all those errors came from (I should have printed the block I was receiving the errors in).
2013/04/27
2013/04/19
04/10-04/18 Assignment Log
So I started with Bird, straight into alphabetical order and I had no idea it was supposed to be second-hardest, not that it mattered because I instinctively make everything harder for myself, so I started with bitwise operators! I wanted to try manipulating bits instead of memcpy to see if it would work, serialize:
buffer[0] = (char) (getX() >> 24);
buffer[1] = (char) (getX() >> 16);
buffer[2] = (char) (getX() >> 8);
buffer[3] = (char) getX();
And deserialize:
this->x = 0;
this->x |= buffer[0] << 24;
this->x |= buffer[1] << 16;
this->x |= buffer[2] << 8;
this->x |= buffer[3];
Never got it to work in C++. The same algorithm worked perfect in Java, so maybe some of my surrounding code is broken. I stored the size of the string in a single byte because the size can never change, and then I used a for loop to iterate through both the buffer and the string at the same time. This was broken for a while because I was missing the + 5 on the loops to reach the end of the string, without which it was grabbing random bytes of data.
I tried to get memcpy to save to the class member:
memcpy(this->x, buffer, sizeof(x));
Didn't work because it passes the value, not the reference.
I did Cat and Dog next, very simple, copying from the sample class for the most part.
Bird actually took me an hour or two, mainly because of dumb mistakes I kept making. I knew I couldn't save from the buffer straight into the apple and orange pointers to a null location, did it anyway. This was my absolute worst mistake that held me up for an hour:
memcpy(buffer, &pApple, sizeof(pApple));
pointer += sizeof(pApple);
Obviously, the size of a pointer is always 4 bytes. Even after I printed the sizeof I had to think about it for a few minutes. Also, the I changed the source parameter for memcpy to this->pApple; don't know if there's any significant difference between the two methods. I'm used to structs being capitalized (like classes in Java), so I kept trying to say:
apple = new apple();
Then I finally remembered how to declare it (generally, I name my objects as lowercase version of the class in Java and mostly all object-oriented languages). Oh, and then I forgot that structs need to be pointers, so I had tried to do this:
apple app = new apple();
It's been far too long since I've worked with structs, even though they are virtually the same thing as classes.
I also kept questioning whether I was offsetting the buffer correctly while this problem with structs was going on in the background. Simply adding to the buffer pointer seemed to easy, so I tried to do an array look-up and then a reference of buffer until I remembered that it was a pointer and that I shouldn't have second-guessed myself because I was doing it right to begin with.
Oh hey, remember that memcpy to a class member from up above?
memcpy(&this->x, buffer, sizeof(x));
Figured it out while I was writing this blog post, so I went over my code again and removed the temp variables I was using to memcpy to.
buffer[0] = (char) (getX() >> 24);
buffer[1] = (char) (getX() >> 16);
buffer[2] = (char) (getX() >> 8);
buffer[3] = (char) getX();
And deserialize:
this->x = 0;
this->x |= buffer[0] << 24;
this->x |= buffer[1] << 16;
this->x |= buffer[2] << 8;
this->x |= buffer[3];
Never got it to work in C++. The same algorithm worked perfect in Java, so maybe some of my surrounding code is broken. I stored the size of the string in a single byte because the size can never change, and then I used a for loop to iterate through both the buffer and the string at the same time. This was broken for a while because I was missing the + 5 on the loops to reach the end of the string, without which it was grabbing random bytes of data.
I tried to get memcpy to save to the class member:
memcpy(this->x, buffer, sizeof(x));
Didn't work because it passes the value, not the reference.
I did Cat and Dog next, very simple, copying from the sample class for the most part.
Bird actually took me an hour or two, mainly because of dumb mistakes I kept making. I knew I couldn't save from the buffer straight into the apple and orange pointers to a null location, did it anyway. This was my absolute worst mistake that held me up for an hour:
memcpy(buffer, &pApple, sizeof(pApple));
pointer += sizeof(pApple);
Obviously, the size of a pointer is always 4 bytes. Even after I printed the sizeof I had to think about it for a few minutes. Also, the I changed the source parameter for memcpy to this->pApple; don't know if there's any significant difference between the two methods. I'm used to structs being capitalized (like classes in Java), so I kept trying to say:
apple = new apple();
Then I finally remembered how to declare it (generally, I name my objects as lowercase version of the class in Java and mostly all object-oriented languages). Oh, and then I forgot that structs need to be pointers, so I had tried to do this:
apple app = new apple();
It's been far too long since I've worked with structs, even though they are virtually the same thing as classes.
I also kept questioning whether I was offsetting the buffer correctly while this problem with structs was going on in the background. Simply adding to the buffer pointer seemed to easy, so I tried to do an array look-up and then a reference of buffer until I remembered that it was a pointer and that I shouldn't have second-guessed myself because I was doing it right to begin with.
Oh hey, remember that memcpy to a class member from up above?
memcpy(&this->x, buffer, sizeof(x));
Figured it out while I was writing this blog post, so I went over my code again and removed the temp variables I was using to memcpy to.
2013/04/09
04/02-04/09 Assignment Log
So the first thing I did was create the C++ lower-to-upper program, do the fun part first. Hard code the input string? Nope. I want to test any string I want, then Imma loopin' this. Haven't used C++ since sophomore year of college when I taught myself.
>Remembered that I needed to pause program before exiting, sync the buffer every loop, and limit the size of the input.
>Forgot about cin.get();
>Forgot about cin.sync();
>Forgot about setw() (and that setw() was in iomanip).
I didn't know about cin.clear(), but that was helpful nonetheless. I had also never used cin to fill a Boolean, did it anyway. Also, almost forgot to put the actual toUpper() code in its own function. Then I had trouble getting const chars and chars to play nice, so I overrode the method. Also, forgot how annoying Microsoft's _CRT_SECURE_NO_WARNINGS is. Had to add it to Preprocessor Definitions in C/C++ -> Preprocessor. I also define a char array with new but I never delete it, which would technically be a memory leak but the new is only called for hardcoded strings, so it won't memory leak in the loop and therefore cause no damage.
I've done Git and SVN, but no Perforce. Let's see how fast I can get used to this. Alright, long story short I installed Perforce on a non-administrator account. Big mistake, crashed on start-up. Had to uninstall and reinstall it, quite annoying. I told it to put the workspace on my desktop as Perforce, but instead it's "Perforce\Perforce". Created a workspace in the wrong directory, can't find out how to delete it. Finally, it's working. I know how to commit files... but how to unstage files from commit? I'll figure it out later.
Class seems like it will be fun. Perforce and blogging is just something to get used to.
>Remembered that I needed to pause program before exiting, sync the buffer every loop, and limit the size of the input.
>Forgot about cin.get();
>Forgot about cin.sync();
>Forgot about setw() (and that setw() was in iomanip).
I didn't know about cin.clear(), but that was helpful nonetheless. I had also never used cin to fill a Boolean, did it anyway. Also, almost forgot to put the actual toUpper() code in its own function. Then I had trouble getting const chars and chars to play nice, so I overrode the method. Also, forgot how annoying Microsoft's _CRT_SECURE_NO_WARNINGS is. Had to add it to Preprocessor Definitions in C/C++ -> Preprocessor. I also define a char array with new but I never delete it, which would technically be a memory leak but the new is only called for hardcoded strings, so it won't memory leak in the loop and therefore cause no damage.
I've done Git and SVN, but no Perforce. Let's see how fast I can get used to this. Alright, long story short I installed Perforce on a non-administrator account. Big mistake, crashed on start-up. Had to uninstall and reinstall it, quite annoying. I told it to put the workspace on my desktop as Perforce, but instead it's "Perforce\Perforce". Created a workspace in the wrong directory, can't find out how to delete it. Finally, it's working. I know how to commit files... but how to unstage files from commit? I'll figure it out later.
Class seems like it will be fun. Perforce and blogging is just something to get used to.
Subscribe to:
Posts (Atom)