Sockets Programming in C Using TCP/IP: TA: Awad A Younis Class: CS457 Fall 2014
Sockets Programming in C Using TCP/IP: TA: Awad A Younis Class: CS457 Fall 2014
using TCP/IP
Close(): Hang up
Server Client
1. Create a TCP socket using socket() 1. Create a TCP socket using socket()
2. Assign a port number to the socket with bind() 2. Establish a connection to server using
3. Tell the system to allow connections to be connect()
made to that port using listen() 3. communicate using send() and recv()
4. Repeatedly do the following: 4. Close connection using close()
Call accept() to get a new socket for each
client connection
communicate with the client using send()
and recv()
Close the client connection using close()
Why socket programming?
To build network applications.
Firefox, google chrome, etc.
Apache Http server
What is a socket?
It is an abstraction through which an application may send and receive
data
File is an analogy: read (receive) and write (send)
Types of sockets
Stream sockets (TCP): reliable byte-stream service
Datagram sockets (UDP): best effort datagram service
What is a socket API?
An interface between application and network
Applications access the services provided by TCP and UDP through the
sockets API
Specifying Addresses
Applications need to be able to specify Internet address and Port
number. How?
Use Address Structure
1. Sockaddr: generic data type
2. in_addr : internet address
3. sockaddr_in: another view of Sockaddr
struct sockaddr_in{
unsigned short sin_family; /* Internet protocol (AF_INET) */
unsigned short sin_port; /* Address port (16 bits) */
struct in_addr sin_addr; /* Internet address (32 bits) */
char sin_zero[8]; /* Not used */
}
Create a socket
int socket(int protocolFamily, int type, int protocol)
socket () returns the descriptor of the new socket if no error occurs and -1
otherwise.
Example:
#include <sys/types.h>
#include <sys/socket.h>
int servSock;
if ((servSock= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
Bind to a socket
int bind(int socket, struct sockaddr *localAddress, unsigned int addressLength)
socket: Socket (returned by socket ())
localAddress: Populated sockaddr structure describing local address
address Length: Number of bytes in sockaddr structure--usually just size o f ( localAddress )
bind() returns 0 if no error occurs and - 1 otherwise.
Example:
struct sockaddr_in ServAddr;
ServAddr.sin_family = AF_INET; /* Internet address family
ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface
ServAddr.sin_port = htons(ServPort); /* Local port */
accept () returns the newly connected socket descriptor if no error occurs and -1
otherwise.
Example:
#define MAXPENDING 5
if ((clientSock=accept(servSock,(structsockaddr*)&ClntAddr,&clntLen))<0)
Constricting a Message
1. Encoding data: array vs struct
Reference
Pocket Guide to TCP/IP Socket, by Michael J. Donahoo and Kenneth
L. Calvert
Beejs Guide to Network Programming Using Internet Sockets, by
Brian "Beej" Hall. (http://www.cs.columbia.edu/~danr/courses/6761/Fall00/hw/pa1/6761-sockhelp.pdf)