Beruflich Dokumente
Kultur Dokumente
SOCKET PROGRAMMING
B.Vijayalakshmi
AP/CSE, RIT
What is a socket?
Chapter 1
Socket
The point where a local application process attaches
to the network
An interface between an application and the network
An application creates the socket
Creating a socket
Attaching a socket to the network
Sending and receiving messages through the socket
Closing the socket
Chapter 1
What is a socket?
Socket Family
Chapter 1
Socket
PF_INET denotes the Internet family
PF_UNIX denotes the Unix pipe facility
PF_PACKET denotes direct access to the network
interface (i.e., it bypasses the TCP/IP protocol stack)
Socket Type
Chapter 1
SOCK_STREAM
a.k.a. TCP
reliable delivery
in-order guaranteed
connection-oriented
bidirectional
App
3 2
1
SOCK_DGRAM
a.k.a. UDP
unreliable delivery
no order guarantees
no notion of connection
app indicates dest. for each
packet
can send or receive
D1
App
socket
Dest.
3 2
1
D2
socket
D3
Chapter 1
Socket
Descriptor
references
Applications
UDP sockets
TCP sockets
65535
65535
UDP ports
UDP
TCP
IP
Chapter 1
Ports
20,21: FTP
23: Telnet
80: HTTP
Port 0
Port 1
Port 65535
Chapter 1
- Procedures
Chapter 1
Sockets
Server
Unix
socket()
(e.g.
TCP)
(e.g. UDP)
Client
Server
socket()
socket()
socket()
bind()
bind()
bind()
Chapter 1
Client
Server
Communication
Stream
Datagram
Client
listen()
accept()
synchronization
point
connect()
recv()
send()
recvfrom()
sendto()
send()
recv()
sendto()
recvfrom()
close()
close()
close()
close()
CS556 - Distributed
16
10
Chapter 1
Chapter 1
Creating a Socket
int sockfd = socket(address_family, type, protocol);
12
Chapter 1
Specifying
Addresses
Socket API defines a generic data type for addresses:
struct sockaddr {
unsigned short sa_family; /* Address family (e.g.
char
sa_data[14];
/* Family-specific address information
AF_INET)
*/
}
*/
struct in_addr {
unsigned long s_addr;
}
unsigned
short
sin_family;
struct
sockaddr_in
{
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
Chapter 1
Passive open
Prepares to accept connection, does not actually establish a
connection
Server invokes
int bind (int socket, struct sockaddr *address,
int addr_len)
int listen (int socket, int backlog)
int accept (int socket, struct sockaddr *address,
int *addr_len)
14
Chapter 1
Binds the newly created socket to the specified address i.e. the
network address of the local participant (the server)
Address is a data structure which combines IP and port
Listen
15
Chapter 1
16
Chapter 1
17 17
Chapter 1
18 18
Chapter 1
Passive participant
The accepted
connection is on a new
socket
The old socket
continues to listen for
other active participants
Why?
Active participant
a-sock-1
socket
Active 1
l-sock
a-sock-2
socket
Active 2
19 19
Chapter 1
20 20
Chapter 1
Client invokes
int connect (int socket, struct sockaddr *address,
int addr_len)
Connect
21
Chapter 1
connect call
connect is blocking
22 22
Chapter 1
23
Chapter 1
Chapter 1
Chapter 1
close
Closing a socket
26 26
Chapter 1
Header Files
#include <sys/types.h>
This header file contains definitions of a number of data
types used in system calls. These types are used in the
next two include files.
#include <sys/socket.h>
The header file socket.h includes a number of definitions
of structures needed for sockets.
#include <netinet/in.h>
The header file netinet/in.h contains constants and
structures needed for internet domain addresses.
27
Chapter 1
28
Chapter 1
rd=read(acptd,buf,10);
29
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int sid,con,sd,rd;
struct sockaddr_in sin;
char buf[10];
sid=socket(PF_INET,SOCK_STREAM,0);
if(sid == -1)
{
printf("\n Error in creating the socket \n");
exit(-1);
Chapter 1
30
con=connect(sid,(struct sockaddr*)&sin,sizeof(sin));
if(con < 0)
{
printf("\n The connection established falied \n");
exit(-1);
}
printf("\n Connection established successfully \n");
do
{
printf(" Enter the string: ");
scanf("%s",buf);
sd=send(sid,buf,10,0);
if(sd < 0)
{
printf("\n Error in sending the data to the server \n");
exit(-1);
}
rd=read(sid,buf,10);
if(rd < 0)
{
Chapter 1
31