How might I utilize socket connections using only the C standard libraries? - c

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

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.

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

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

Related

Which function should I look into to make a socket in C?

What function should I look into to create a socket for my mini-ftp server in C in the stdlib, except recv, send and all system calls that make a socket non-blocking?
There are plenty of guides for this sort of thing, in particular see Beej's Guide to Network Programming for working with Linux. There are similar / equivelant APIs exposed in Windows and other operating systems.
Fundamentally though, if you're using Liunx, you'll be interested in:
socket() to create the socket
connect() / bind() and listen() to connect or listen
send() and recv() to send and receive
If you're interested in non-blocking I/O, then the "Blocking" section will help.

Socket programming - What's the difference between listen() and accept()?

I've been reading this tutorial to learn about socket programming. It seems that the listen() and accept() system calls both do the same thing, which is block and wait for a client to connect to the socket that was created with the socket() system call. Why do you need two separate steps for this? Why not just use one system call?
By the way, I have googled this question and found similar questions, but none of the answers were satisfactory. For example, one of them said that accept() creates the socket, which makes no sense, since I know that the socket is created by socket().
The listen() function basically sets a flag in the internal socket structure marking the socket as a passive listening socket, one that you can call accept on. It opens the bound port so the socket can then start receiving connections from clients.
The accept() function asks a listening socket to accept the next incoming connection and return a socket descriptor for that connection. So, in a sense, accept() does create a socket, just not the one you use to listen() for incoming connections on.
It is all part of the historic setup. listen prepares socket for the next accept call. Listen also allows one to setup the backlog - the number of connections which will be accepted by the system, and than put to wait until your program can really accept them. Everything which comes after the backlog is full well be rejected by the system right away. listen never blocks, while accept will block (unless the socket is in non-blocking mode) until the next connection comes along. Obviously, this does not have to be two separate functions - it is conceivable that accept() function could do everything listen does.
The above two answers clearly state the difference between accept and listen. To answer your other question - why we need two separate functions?
One use case is, for e.g. if you only want to test if a port is still available/and accessible, you can do so by just listening to the port and then closing it without accepting any connections.
For e.g. https://github.com/coolaj86/golang-test-port uses the listen call for testing a port's availability.
listen() uses a backlog parameter which specifies the maximum number of queued connections and should be at least 0. It's value increases as the server receives a lot of connection requests simultaneously.
accept() waits for incoming connections. When a client connects, it returns a new socket object representing the connection.
Another imperative thing to note is that accept() creates a new socket object which will be used to communicate with the client. It is different from the listening socket that server uses to accept new connections.
Maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.

In socket programming how to make a server listen for limited no of clients?

I am working on socket programming using c language and linux platform and my requirement is to make a server listen only for two or three clients. How is it possible ?
you make that by specifying second argument on listen() call.
i assume that you are using TCP protocol.
from man pages:
int listen(int sockfd, int backlog);
The backlog argument defines the maximum length to which the queue
of pending
connections for sockfd may grow. If a connection request arrives when the queue
is full, the client may receive an error with an indication of ECONNREFUSED or,
if the underlying protocol supports retransmission, the request may be ignored
so that a later reattempt at connection succeeds.
so for two clients - you call listen like: listen(fd, 2);
suggested reading: https://beej.us/guide/bgnet/html/multi/syscalls.html#listen
Close the listening socket when you reach the limit, and open it again when one of the clients disconnects.
Or
Leave it open but stop accepting connections while you're at the limit.
The backlog parameter to listen() has nothing to do with any of this, contrary to other answers and comments here.

C: UDP send & receive packets

I've been trying to use the socket() api in C but no luck so far.
I would like to send a request to a specific device (Address: 192.168.2.55 Port: 12850) which will then return data to the application. How do I do this in C. I'm on a Mac so "the Unix way" if that differs from Windows...
Thanks and merry christmas!
For socket programming introduction, see http://beej.us/guide/bgnet/
The steps involved in establishing a socket on the client side are as follows:
Create a socket with the socket() system call
Connect the socket to the address of the server using the connect() system call
Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.
The steps involved in establishing a socket on the server side are as follows:
Create a socket with the socket() system call
Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
Listen for connections with the listen() system call
Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
Send and receive data
Check to see if you have followed these steps thusfar with the code you have written.

WINAPI: CreateFile to Network Adapter to Read Raw Bytes

Is it possible to read a Network Adapter similar to a Serial Port? I know that Serial Ports can be read with CreateFile WINAPI Function. Is there a similar way to read raw bytes from a Network Adapter?
I am aware of the WiFi/Network Functions but the WiFi Examples are fairly sparse.
You can pass the SOCK_RAW flag when you create the socket using WSASocket() (or socket(), as your tastes run). This is described in further detail under TCP/IP Raw Sockets on MSDN.
From that page --
Once an application creates a socket
of type SOCK_RAW, this socket may be
used to send and receive data. All
packets sent or received on a socket
of type SOCK_RAW are treated as
datagrams on an unconnected socket.
Of note, Microsoft crippled their raw sockets implementation after Windows XP SP2; the details are described on the MSDN page in the section Limitations on Raw Sockets:
TCP data cannot be sent over raw sockets.
UDP datagrams with an invalid source address cannot be sent over raw
sockets.
A call to the bind function with a raw socket is not allowed.
If these limitations are too restrictive, you can fall back to the previously recommended winpcap library.
If you want to capture raw packets you need a support driver like WinPCAP to do that.

Resources