3

How might I utilize socket connections using only the C standard libraries? I do not wish to use any third-party libraries.

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

The C standard library doesn't contain any library to create a socket connection.

1 Comment

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.
2

... 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.