combo of tutorials:
to compile c code:
$ gcc -o server server.c
where 'server' represents executable
Learnings:
- Used curl to test server without having implemented client-side
- Main bug was that program terminated immediately upon connecting to server
- Needed to add while loop to make sure server constantly accepts message
- If other clients connect to server I'll have created memory leaks since I didn't take into account forking and closing file descriptors
- When sending a message to my client, the message was at first not sent until I quit running server
- Realized the message was getting stuck in some sort of buffer
- Required needing to add a terminating character to hello world in form of
\n
magic
- Through testing server with curl found post request:
- could also open up webpage to point to port with localhost:2222 and display different header
$ brew install node
$ node tcp_client.js
$ node tcp_server.js
Learnings:
- Net library abstracts away the need to set up sockets
- Super easy to set up server since client is immediately returned in callback to begin sending/receiving messages
- Doesn't seem to care whether messages are properly truncated with
\n
or not - Addresses are automatically converted into local/remote ports (htons is not needed to translate between host and network byte order)
- I'm sure there's a way to change communication type, but need to look up library
- Underlying net library is still c interface to talk with sockets
- Net library handles specifying message size, request, response