How might I utilize socket connections using only the C standard libraries? I do not wish to use any third-party libraries.
3 Answers
There is nothing in the C standard library that has anything to do with sockets. C is agnostic of networking. You could take a look at Posix standard API which is not technically a standard C library, but it is a standard API.
Comments
The C standard library doesn't contain any library to create a socket connection.
1 Comment
ysdx
No but on POSIX.1-1990, you can create the socket, wrap it in a FILE* with fdopen. Then you can use the FILE* with C standard libraries.
... but disregarding the exact name of the library, the important functions to get your head around are:
- socket() - creates a socket
- close() - closes a socket
- bind() - associates a socket with a port number; typically on "server" end only
- listen() and accept() - handle incoming connections ("server side", TCP)
- connect() - initiate outgoing connection ("client side", TCP)
- recv() - receive data from connection (TCP)
- send() - send data over connection (TCP)
- recvfrom() - receive data from connectionless socket (UDP)
- sendto() - send data over connectionless socket (UDP)
Depending on the environment you're coding in, you'll need to include something like
#include <sys/socket.h>
#include <netinet/in.h>