Programming with TCP/IP

standguideNetworking and Communications

Oct 26, 2013 (3 years and 1 month ago)

59 views

read()

recv()

connection establishment


Server

(connection
-
oriented protocol)

blocks until connection


from client

Client

socket()

bind()

listen()

accept()

read()

recv()

write()

send()

socket()

connect()

write()

send()

process request

data (request)

data (reply)

Socket system calls for

connection
-
oriented

protocol


Server

(connectionless protocol)

socket()


blocks until data

received from client

bind()

recvfrom()

sendto()

socket()

bind()

sendto()

recvfrom()

process request

data (request)

data (reply)

Client

Socket system calls for

connectionless protocol

Not necessary
in UDP!!

Socket system call


int sockfd = socket (int family, int type, int protocol


Family: AF_UNIX, AF_INET; Type: SOCK_STREAM,
SOCK_DGRAM, SOCK_RAW

struct sockaddr {


unsigned short sa_family; //specifies the address type


char




sa_data[14]; //specifies the address value

};

struct sockaddr_in {






short


sin_family;





unsigned short

sin_port; 16 bit





struct in_addr

sin_addr; 32 bit

{ unsigned long s_addr; }




char




sin_zero[8];

};


Network Byte Order Functions

Example:


struct sockaddr_in sin;

sin.sin_family = AF_INET;

sin.sin_port = htons(9999);

sin.sin_addr.s_addr = inet_addr(“130.233.224.13”);

inet_aton(“130.233.224.13”, &sin.sin_addr);

unsigned short htons(unsigned short);

unsigned short ntohs(unsigned short);

unsigned long htonl(unsigned long);

unsigned long ntohl(unsigned long);

Server system calls


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


int listen(int socket, int qlength)


!newsockfd = accept(int sockfd, void* peer, int
*addrlen)


int getpeername(int sockfd, struct sockaddr
*addr, int *addrlen);


struct hostent *gethostbyaddr(const char
peer_addr, int len, int type);


! (char*) socaddr_in.sin_addr, name in
hostent
-
>h_name



Client system calls


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


struct hostent *gethostbyname(const char
*name);


! addr.sin_addr = *((struct in_addr *) hostent
-
>h_addr);


write or read(fd, buff_ptr, num_bytes)


int send(int s, const char *msg, int len, int flags)


int recv(int s, char *buf, int len, int flags)


flags: MSG_PEEK, MSG_OOB etc.


close(int socket), shutdown(int socket, int how)