Introduction to Socket Programming. CONTENTS SOCKETS SOCKET ADDRESS SOCKET SYSTEM CALLS...

Post on 14-Dec-2015

239 views 2 download

transcript

Introduction to Socket

Programming

CONTENTSCONTENTS• SOCKETS• SOCKET ADDRESS• SOCKET SYSTEM CALLS• CONNECTIONLESS ITERATIVE SERVER• UDP CLIENT-SERVER PROGRAMS• CONNECTION-ORIENTED CONCURRENT SERVER• TCP CLIENT-SERVER PROGRAMS

CONTENTS (continued)CONTENTS (continued)• BYTE ORDERING• ADDRESS TRANSFORMATION• BYTE MANIPULATION FUNCTIONS• INFORMATION ABOUT REMOTE HOST

SOCKETS

What is socket

• Unix programs do any sort of I/O by reading or writing to a file descriptor.

• This logical file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else.

• When a program wants to communicate with another program over the Internet, also do it through a file descriptor-- socket descriptor.

Declaration for socket function

Socket types

SOCKETS ADDRESS

socket address structure

// Internet address: AF_INET

struct sockaddr_in

{ short sin_family; // 2 bytes

u_short sin_port; // 2 bytes

struct in_addr sin_addr; // 4 bytes

char sin_zero; // 8 bytes, unused, set to 0

}

// General socket address structure

struct sockaddr

{ unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address

}; OR

// Internet addressstruct in_addr { unsigned long s_addr; // that's a 32-bit long, or 4 bytes

};

OR

struct in_addr

{ union

{ struct { u_char s_b1, s_b2, s_b3,s_b4; };

struct { u_short s_w1, s_w2; };

u_long S_addr;

} S_un;

}

#define s_addr S_un.S_addr

Internet address structure

Internet address structure

struct sockaddr_in my_addr; my_addr.sin_addr.s_addr =

INADDR_ANY; // use my IP address

• By setting my_addr.sin_addr.s_addr to INADDR_ANY, you are telling it to automatically fill in the IP address of the machine the process is running on.

SOCKET SYSTEMCALLS

Declaration for bind function

sockfd is the socket file descriptor returned by socket().

localaddr is a pointer to a struct sockaddr that contains information about your address, namely, port and IP address.

localaddrlen can be set to sizeof(struct sockaddr).

By setting localaddr.sin_port to zero, you are telling bind() to choose the port for you.

IANA Port Numbers Ranges

The Internet Assigned Numbers Authority(IANA)

http://www.iana.org/assignments/port-numbers

Declaration for listen function

sockfd is the usual socket file descriptor from the socket() system call.

backlog is the number of connections allowed on the incoming queue.

Declaration for connect function

sockfd is the socket file descriptor, as returned by the socket() call. But can be used to communicate with server after successfully called connect().

serveraddr is a struct sockaddr containing the destination port and IP address.

serveraddrlen can be set to sizeof(struct sockaddr).

Declaration for accept function

sockfd is the listen()ing socket descriptor. Easy enough.

clientaddr will usually be a pointer to a local struct sockaddr_in. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port).

clientaddrlen is a local integer variable that should be set to sizeof(struct sockaddr_in) before its address is passed to accept().

Declaration for close function

Declaration for write function

Used in connection-oriented (TCP) program.

sockfd is the socket descriptor you want to send data to (whether it's the one returned by socket() or the one you got with accept().) buf is a pointer to the data you want to send, and buflen is the length of that data in bytes.

If the value returned by write() doesn't match the value in buflen, it's up to you to send the rest of the string.

Ssize_t send(int sockfd, const void *buf, int buflen, int flags);

Just set flags to 0. Set flag=MSG_OOB to send out-of-band data.

Declaration for read function

Used in connection-oriented (TCP) program.

sockfd is the socket descriptor to read from, buf is the buffer to read the information into, buflen is the maximum length of the buffer, flags can again be set to 0. Set flag=MSG_OOB to read out-of-band data.

A similar system call

ssize_t recv(int sockfd, void *buf, int len, unsigned int flags);

Declaration for sendto function

Used in connection-less (UDP) program.

toaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the destination IP address and port).

toaddrlen can simply be set to sizeof(struct sockaddr).

Declaration for recvfrom function

Used in connection-less (UDP) program.

fromaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the IP address and port of the sender).

fromaddrlen can simply be set to sizeof(struct sockaddr).

CONNECTIONLESSITERATIVESERVER &

UDP CLIENT-SERVERPROGRAMS

Socket interfacefor connectionless

iterative server

CONNECTION-ORIENTEDCONCURRENT SERVER

Socket interfacefor connection-oriented

concurrent server

Part I

Part II

Part I

Clientand

Server

Part II

BYTE ORDERING

Big-endian byte order

Little-endian byte order

The byte order for the TCP/IPThe byte order for the TCP/IPprotocol suite is big endian.protocol suite is big endian.

Bite-order transformation

Declarations for byte-order transformation

Examples:

my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address

ADDRESSTRANSFORMATION

Address transformation

Internal data in socket_address structure

Predefined in program or user input

Declarations for address transformation

functions

Example:

struct sockaddr_in dest_socket_add;

dest_socket_add.sin_addr.s_addr = inet_addr("10.12.110.57");

INFORMATIONABOUT REMOTE

HOST

Declaration for gethostbyname

When you know the remote site’s domain name, but you don’t know its IP address.

Hostent structure