I’ve worked with TCP sockets before in Python. It looks pretty similar in C but I can’t get anything to work. socket(AF_INET, SOCK_STREAM, 0); returns -1, which of course indicates an error. How could I go so wrong so fast?
If you could help me with this problem, that would be nice, but it would be incredibly helpful if you could provide me with some simple, bare bones source code. It doesn’t need to even do anything really, and it doesn’t need to handle errors. I just need to see how to properly create a server socket, bind it, listen on it, and accept clients and how to create and connect a client socket. I can figure out all the bells and whistles on my own.
Thanks!
You need to initialise WinSock with the
WSAStartupfunction before you can use sockets. Python’s implementation of sockets on Windows likely calls this automatically so you don’t need to worry about it, however when using WinSock directly it is important to callWSAStartupbefore any other WinSock calls, and when your program is done with sockets, you need to callWSACleanup.