typically set to 0, IPPROTO_TCP, IPPROTO_UDP
Returns socket descriptor
Creating a socket is in some ways similar to opening a file. This function creates a file descriptor
and returns it from the function call. You later use this file descriptor for reading, writing and using with
other socket functions
Binding Socket with an Address
1
intbind(intsd,structsockaddr*addr,intlen)
sd: socket descriptor returned by socket()
addr: pointer to sockaddr structure
containing address to be bound to socket
len: length of address structure
Returns 0 if success, -1 otherwise
You also do not need to find information about the IP addresses associated with the host you are working
on. You can specify: INNADDR_ANY to the address structure and the bind function will use on of the
available (there may be more than one) IP addresses. This ensures that connections to a specified port will
be directed to this socket, regardless of which Internet address they are sent to. This is useful if host has
multiple IP addresses, then it enables the user to specify which IP address will be b_nded to which port
number
3. Specifying Socket Address
1
2
3
4
5
6
7
8
9
10
structsockaddr_in{shortsin_family;/* set to AF_INET */u_shortsin_port;/* 16 bit port number */structin_addrsin_addr;/* 32 bit host address */charsin_zero[8];/* not used */};structin_addr{u_longs_addr;/* 32 bit host address */};
listen for incoming connections
1
intlisten(intsocket_file_descriptor,intbacklog);
It is important in determining how many
connections the server will connect with. Typical values for backlog are 5 – 10.
The parameter socket_file_descriptor is the socket file descriptor returned by a call to socket() function.
The return value of listen() is 0 for success and –1 for failure.
Connecting to Server
1
intconnect(intsd,structsockaddr*addr,intlen)
sd: socket descriptor returned by socket()
addr: pointer to sockaddr structure containing server’s address (IP address and port)
len: length of address structure
Returns 0 if success, -1 otherwise
Byte Ordering
Network Byte Order: Big Endian
Byte Order Conversion
m = ntohl(m) : network-to-host byte order, 32bit
m = htonl(m) : host-to-network byte order
ntohs, htons : short(16bit)
Connection Acceptance by Server
1
intaccept(intsd,structsockaddr*from,int*len)
sd: socket descriptor returned by socket()
from: pointer to sockaddr structure which gets filled with client’s address information
len: length of address structure
Blocks until connection requested or error
returns a new socket descriptor on success
accept() returns a new socket file descriptor for the purpose of reading and writing to the client.
It dequeues the next connection request on the queue for this socket of the server. If queue is empty, this
function blocks until a connection request arrives
More on Socket Descriptor
A 5-tuple associated with a socket
{protocol, local IP address, local port, remote IP address, remote port}
socket() fills the protocol component
local IP address/port filled by bind()
remote IP address/port by accept() in case of server
in case of client, both local and remote by connect()
Complete socket is like a file descriptor
Both send and recv through same socket
Accept returns a new complete socket
Original one can be used to accept more connections
address convert
convert a string dotted decimal IP4 address to a NETWORK BYTE ORDERED 32 bit value: inet_addr(), inet_aton()
convert a 32 bit NETWORK BYTE ORDERED to a IP4 dotted decimal string: inet_nota()
intsd;structsockaddr_insa;sd=socket(AF_INET,SOCK_STREAM,0);sa.sin_family=AF_INET;sa.sin_port=htons(5100);//htons() converts host-byte-order into network-byte-order
sa.sin_addr.s_addr=inet_addr(“128.101.34.78”);if(connect(sd,(structsockaddr*)&sa,sizeof(sa))!=-1)...
Connection-oriented Server
1
2
3
4
5
6
7
8
9
10
11
intsd,cd,calen;structsockaddr_inma,ca;sd=socket(AF_INET,SOCK_STREAM,0);ma.sin_family=AF_INET;ma.sin_port=htons(5100);ma.sin_addr.s_addr=htonl(INADDR_ANY);bind(sd,(structsockaddr*)&ma,sizeof(ma));listen(sd,5);calen=sizeof(ca);cd=accept(sd,(structsockaddr*)&ca,&calen);//…read and write to client treating cd as file descriptor…
Client example
Get current time from 192.43.244.18 and print it to stdout.
/*
* daytime.c
*
* Programmed by G. Adam Stanislav
*/#include<stdio.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>intmain(){registerints;registerintbytes;structsockaddr_insa;charbuffer[BUFSIZ+1];if((s=socket(PF_INET,SOCK_STREAM,0))<0){perror("socket");return1;}bzero(&sa,sizeofsa);sa.sin_family=AF_INET;sa.sin_port=htons(13);sa.sin_addr.s_addr=htonl((((((192<<8)|43)<<8)|244)<<8)|18);if(connect(s,(structsockaddr*)&sa,sizeofsa)<0){perror("connect");close(s);return2;}while((bytes=read(s,buffer,BUFSIZ))>0)write(1,buffer,bytes);close(s);return0;}