25. Internet and BSD Sockets
To access networks and the internet, such as the web, then you need to know about how to send and receive information over a network, in particular, using TCP/IP protocols. The Amiga uses the bsdsocket.library which either comes with the AmigaOS 3.5 or later or with a TCP package (Miami, Genesis, AmiTCP etc).
Network programming requires a lot of knowledge of internet protocols including TCP/IP, DNS, DHCP, HTTP/S, FTP, and IP addressing.
a) Opening the bsdsocket library.
You can manually open the bsdsocket library using the standard Exec function OpenLibrary
#include <sys/socket.h>
SocketBase = OpenLibrary("bsdsocket.library", 4);
b) Making a connection.
There are between 2 to 5 functions on making a connection, the first is creating a socket, which is an endpoint for communication, stating the type and protocol to use. Format is:
long socket = socket (long domain, long type, long protocol);
domain = AF_INET (ARPA Internet protocols).
type = SOCKET_STREAM (sequenced, reliable two-way connection byte streams), SOCK_DGRAM (datagrams for unreliable, connectionless
messages of fixed length),
SOCK_RAW (
access to internal network protcols and interfaces), SOCK_SEQPACKET (sequenced, reliable two-way connection of datagrams of fixed max length), and
SOCK_RDM (not impletemented).
protocol =
Protocol to use e.g. 80 or 443 for HTTP, 21 for FTP etc.
The second function is bind, which can be used for server TCP streams or UDP client/server connections. It binds a name to a socket. Format is:
long bind = bind (long s, struct sockaddr *name, long namelen);
bind = result of bind.
s = socket descriptor (see socket()).
sockaddr = IP address and port to use.
namelen = Length of sockaddr structure.
If opening a TCP connection from a server, then you would use the listen function which listens for connections on a socket from clients.. Format is:
long success = listen (long s, long backlog);
success = result.
s = socket descriptor.
backlog =
maximum length of queue of pending connections.
If opening a TCP connection from a client, then you would use the connect function, which makes a connection to a socket.
long success = connect (long s, struct sockaddr *name, long namelen);
success result of connect.
s = socket descriptor (see socket()).
sockaddr = IP address and port to use.
namelen = Length of sockaddr structure.
On the other hand, a server would try to accept a TTCP connection from a client with the accept function.
long socket = accept (long s, struct sockaddr *addr, long *addrlen);
socket = socket of the client socket connection.
s = descriptor of socket set up to listen for a connection.
addr = structure filled with address of connecting client.
addrlen = resulting length of the addr structure.
c) Receiving data from a client or server.
To receive data, the client can use the recv (recvfrom) funtion, which receives a message from a socket. Format is:
long nbytes = recv (long s, void *buffer, long length, long flags);
nbytes = Length of message received.
s = socket descriptor.
buffer = buffer containing the data.
length = maximum length of buffer.
flags = MSG_OOB ( process out-of-band data), MSG_PEEK (peek at incoming traffic), MSG_WAITALL (wait for full request or error).
d) Sending data from a client or server.
To send data from a client or server, the send (sendto, sendmsg) function is used, which sends a message from a socket.
long nbytes = send (long s, void *msg, long length, long flags);
nbytes = Length of message sent.
s = socket descriptor.
msg = buffer containing the data.
length = maximum length of buffer.
flags = MSG_OOB ( process out-of-band data), MSG_DONTROUTE (bypass routing, use direct interface).
e) Closing the connection.
When the connection is no longer required, the close function is used on the socket.
long success = CloseSocket(s);
success = Successful close (0) or unsuccessful (-1).
s = Socket descriptor.
AmigaWiki - Developing networking applications
Introduction to Sockets Programming using C