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.
Related
According to the Linux man pages for Unix sockets, "Valid socket types in the UNIX domain are . . . SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.4) SOCK_SEQPACKET, for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent." (http://man7.org/linux/man-pages/man7/unix.7.html).
I thought "always reliable and don't reorder datagrams" is the same as "delivers messages in the order that they were sent."
What's the practical difference between SOCK_DGRAM and SOCK_SEQPACKET?
In the context of UNIX domain sockets, the main difference between two is "datagram-oriented" vs "connection-oriented".
In case of SOCK_DGRAM you don't create a connection (to a server, for example), you just send packets to the server socket. And if server needs to reply, you need to create your own socket, make server aware of this socket and then server can send a reply to it. Very inconvenient, if you really need a connection, but can be useful when you just need one-way communication, i.e. to send some notifies.
SOCK_SEQPACKET is the way to go, when you need connection-oriented approach.
The difference is better understood by the help of UDP and TCP.
A protocol like UDP(connection-less) uses SOCK_DGRAM, implementation
A protocol like TCP(connection-oriented) uses SOCK_STREAM. However, even SOCK_SEQPACKET can be used. The difference between the two is very minimal, TCP can be implemented using the latter as well. In fact, SOCK_SEQPACKET is somewhat a hybrid of both.
STCP is a use case for SOCK_SEQPACKET. Explained in this article: http://urchin.earth.li/~twic/Sequenced_Packets_Over_Ordinary_TCP.html
Here's a post that has discussed this in detail.
Can anyone help me in tracing bind() system call in socket programming. I would like to know what happens when bind() is called, in kernel space. Like which are the structures it updates and what functions are invoked in lower level
The bind(2) system call just configures the local side's address parameters that a socket will use once you have connected (or sendto(2)). If you don't use it, the kernel selects defaults for it, depending on the underlying protocol.
The exact procedure bind(2) follows depends on the protocol family you are working on, as bind will behave differently depending if you are using PF_UNIX, PF_INET, PF_PACKET, PF_XNS, etc.
For example, in Unix sockets, you'll get your socket associated to an inode in the filesystem (an inode that supports unix sockets, of course), so clients have a path to connect to (in Unix sockets, addresses are paths in the filesystem). In TCP/IP sockets, you can fix the local IP address or the local IP port your socket can listen on (to accept connections) or you can force a IP address and/or port to connect from, to a server.
For a deeper understanding of networking sockets internals, I recommend you reading the excellent book from W.R. Stevens "TCP/IP Illustrated Vol 2. The implementation," describing the implementation of BSD sockets in NET2. It's old, but still the best explanation ever made. For a good introduction of the BSD socket system calls use, there's also an excellent book (for a long time it was indeed also the best system call reference for BSD unix system calls) by W.R.Stevens: "UNIX network programming, Vol 1 (2ND Ed): The sockets API." Both are two jewels everyone should have available at work.
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>
I want to send the two different packets in different port numbers parallely using UDP.
Can I achive this using single socket() or should I create another socket??
Can anbody give me some idea on this.
Thanks in advance
You can use a single socket, you will be using the system calls sendto(2) and recvfrom(2) to send and receive data over the datagram sockets.
Take a look at https://beej.us/guide/bgnet/html/multi/syscalls.html for more information (the entire guide is definitely worth the read).
Beej's guide on socket programming
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.