Sie sind auf Seite 1von 7

Berkeley Sockets

ir. Pieter Liefooghe


pieter@info.vub.ac.be

Intro
• File I/O based
– open
– create
– close
– read
– write
– lseek

1
A Socket?

A Socket?
• End-point of communication
– ip @
• struct hostent *gethostbyname(char *hostname)
– port number
• Server: www=80, FTP = 21, Telnet = 23, etc…
• Client: Dynamic (1024-5000)
– protocol
• SOCK_STREAM
• SOCK_DGRAM
• SOCK_RAW
– Additional settings (TTL, TOS,… )

2
Functions: Overview
TCP UDP
Server socket() socket() Create Endpoint
bind() [bind()] Bind Address
listen() specify queue
accept() wait for connection
read() recvfrom() data transfer
write() sendto()
recv()
send()
Client socket() socket() Create Endpoint
[bind()] bind() Bind Address
connect() establish connection

read() sendto() data transfer


write() recvfrom()
recv()
send()
Termination close() | shutdown() close()

socket()
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int socket(int family, int type, int protocol)

family: AF_UNIX, AF_INET, AF_NS, AF_IMPLINK


type: SOCK_STREAM, SOCK_DGRAM,SOCK_RAW
protocol: IPPROTO_UDP,IPPROTO_TCP,IPPROTO_ICMP,
IPPROTO_RAW

3
bind()
#include <sys/types.h>
#include <sys/socket.h>

int bind(int sockfd, struct sockaddr *myaddr, int addrlen);


myaddr: protocol-specific address
addrlen: sizeof(myaddr)

1. servers register well-known @


2. client can register a specific address for itself
3. connectionless client: make sure the src address & port is
filled in!

connect()
#include <sys/types.h>
#include <sys/socket.h>

int connect(int sockfd, struct sockaddr *servaddr, int addrlen);

Note:
* CO client doesn’t need bind before calling connect!
* CL client can use connect() so you can use send()
rather than sendto()

4
listen()
int listen(int sockfd, int backlog);

before accept() and after bind()

backlog: how many connection requests can be queued.


(max 5!)

accept()
#include <sys/types.h>
#include <sys/socket.h>

int accept(int sockfd, struct sockaddr *peer, int *addrlen);

takes first connection request on the queue and creates a NEW


socket with the same properties as sockfd.

Blocking Call!!!

peer & addrlen: used to return @ info about who “connected”.

5
send, sendto, recv and recvfrom
#include <sys/types.h>
#include <sys/socket.h>

int send(int sockfd, char *buff, int nbytes, int flags);


int sendto(int sockfd, char *buff, int nbytes, int flags,
struct sockaddr *to, int addrlen);
int recv(int sockfd, char *buff, int nbytes, int flags);
int recvfrom(int sockfd, char *buff, int nbytes, int flags,
struct sockaddr *from, int *addrlen);

flags: MSG_OOB send or receive out-of-band data.(urgent data)


MSG_PEEK: peek at incoming message
MSG_DONTROUTE: bypass routing

struct sockaddr
Generic Address structure:

Defined in <sys/socket.h>
struct sockaddr {
sa_family_t sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};

In TCP/IP we use

struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};

6
struct in_addr
struct in_addr {
union {
struct { uchar_t s_b1, s_b2, s_b3, s_b4; } _S_un_b;
struct { ushort_t s_w1, s_w2; } _S_un_w;
uint32_t _S_addr;
} _S_un;
#define s_addr _S_un._S_addr
};

A final note...
Compiling with gcc:

gcc … -lsocket –lnsl

With VC++

Add wsock32.lib to project


Don’t forget WSAStartup!

Das könnte Ihnen auch gefallen