Sunteți pe pagina 1din 47

Socket

cyt@pmlab.iecs.fcu.edu.tw

TCP/IP Socket Socket API Socket Socket API

TCP / IP
 

FTP , SMTP , Telnet , DNS , SNMP

What is TCP / IP Transmission Control Protocol / Internet Protocol


TCP , UDP Application IP , ARP Transport
Ethernet , FDDI , Token Ring

Network

Network Access

Port(


65536 ports
port 0 ~ port 65535
Port 0

ports
20, 21: FTP 23: Telnet 80: HTTP

Port 1 Port 2 Port 3 Port 65535

1024 ~ 5000
Port 80

Port 3333
4

TCP/IP Socket Socket API Socket Socket API

Socket


LINK

Socket Socket Interface, API) Socket

Berkeley UNIX

TCP/IP (Application Programming

Socket

Berkeley Socket Interface Socket interface TCP/IP

interface function

BSD ( Berkeley Software Distribution ) Socket WinSock

Socket
1 2 3 4

API (BSD Socket Win Sock)

TCP/IP

Socket


Socket

Socket

Socket Socket
TCP

TCP UDP port number

IP

Socket Descriptor
Socket( )

TCP 80 21

UDP

23 AP3

3333 AP3

AP1

AP2

10

TCP/IP Socket? Socket API Socket Socket API

11

Socket(
socket()

), TCP
socket()

bind() connect() listen() accept()

Client

Server

write()

read()

read()

write()
12

write()
write()

read()
read()

read()

write()

13

Socket(
socket()

), UDP
socket()

bind()

bind()

Client

sendto()

recvfrom()

Server

recvfrom()

sendto()
14

Client/Server

1.

Client
Client Client


socket connect

port

2.

TCP

3.

Client

read/write system call

socket

4.

socket

15

socket()

open socket Descriptor

Socket Client I/O

bind()

bind Port Optional

Client IP

local ( Port Client

IP Client

connect()

connect System Call Call read/write I/O HTML close TCP

TCP Server

Server connect IP Port

accept System

read() write()

HTTP

Socket Descriptor Protocol Server

close()

16

Client/Server

1.

Server
Server socket Server client socket socket socket port client read/write system call

2.

3.

4.

17

socket()

open socket Descriptor TCP bind Server

Socket Server

bind()

Server

local

IP

Port

listen()

listen accept System Call

Socket

accept()

Server accept System Call Client connect System Call Descriptor Descriptor Client I/O read/write HTML accept HTTP Protocol Descriptor Server I/O

read() write()

close()


close

TCP

18

(Client ) 1/2
// socket socket descriptor -1 error if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("Socket Error:%s\a\n", strerror(errno)); printf("Socket Error:%s\a\n", strerror(errno)); exit(1); } family, address, port 0( bzero() function) bzero((char*) &server_addr, sizeof(server_addr)); 140.134.26.50 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(portnumber); server_addr.sin_addr = *((struct in_addr *) host->h_addr); Most Significant byte first (Network Byte Order)

//

//

Server 140 134 26 50 if(connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) { printf("Connect Error:%s\a\n", strerror(errno)); Host Byte Order) exit(1); Least Significant byte first ( } 50 26 134 140

19

(Client ) 2/2
// (hello ) Server if(write(sockfd, hello, strlen(hello)) == -1) { printf("Write Error:%s\n", strerror(errno)); exit(1); } Server if((nbytes=read(sockfd, buffer, 1024)) == -1) { printf("Read Error:%s\n", strerror(errno)); exit(1); } buffer[nbytes] = '\0'; printf("I am Client. I have received from Server:%s\n", buffer);

//

close(sockfd); exit(0);

20

(Server ) 1/3
// socket socket descriptor -1 error if((sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1) { printf("Socket error:%s\n\a", strerror(errno)); exit(1); } family, address, port 0( bzero() function) bzero((char*)&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(portnumber); //port number, htons(16 bytes) network byte order server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //htonl is for 32-bytes, binary presentation

//

// if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) { printf("Bind error:%s\n\a", strerror(errno)); exit(1); }

21

(Server ) 2/3
// if(listen(sockfd,5) == -1) { printf("Listen error:%s\n\a", strerror(errno)); exit(1); } while(1) { sin_size = sizeof(struct sockaddr_in); // socket(new_fd) -1 // client address client_addr if((new_fd=accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1) { fprintf(stderr, "Accept error:%s\n\a", strerror(errno)); exit(1); } printf("Server get connection from %s\n", inet_ntoa(client_addr.sin_addr));

22

(Server ) 3/3
// buffer if((nbytes=read(new_fd, buffer, 1024)) == -1) { printf("Read Error:%s\n", strerror(errno)); exit(1); } buffer[nbytes] = '\0'; printf("I have received:%s\n", buffer); hello client if(write(new_fd, hello, strlen(hello)) == -1) { printf("Write Error:%s\n", strerror(errno)); exit(1); } close(new_fd); } close(sockfd); exit(0);

//

23

24

TCP/IP Socket? Socket API Socket Socket API

25

Socket
     

struct struct struct struct struct struct

sockaddr sockaddr_in in_addr hostent servent protoent

26

sockaddr
<sys/socket.h> // struct sockaddr { u_short sa_family; /* type of address, AF_INET, AF_INET (Internet protocol address family) */ char sa_data[14]; /* value of address, specifies the address value */ };

27

sockaddr_in
<netinet/in.h> //IPv4 Socket Address struct sockaddr_in { u_short sin_family; //specifies protocol to use, PF_INET for either of TCP or UDP u_port sin_port; //16 bits, network byte order struct in_addr sin_addr; //32 bits, network byte order char sin_zero[8]; //unused };

28

IPv4 Socket Address

29

in_addr
//Internet address structure struct in_addr { long s_addr; //32-bit( ) IPv4 address, network byte ordered };

30

struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; //hptr->h_addr = gethostbyname(140.134.26.21); }; #define h_addr h_addr_list[0]

hostent

h_addr_list

h_addr_list[0] h_addr_list[1]

31

servent
struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; };

32

protoent
struct protoent { char *p_name; char **p_aliases; int p_proto; };

33

TCP/IP Socket? Socket API Socket Socket API

34

Socket API


socket
socket socket socket client client client client client local IP client socket

Server

bind

listen

accept

recv/read
(TCP)

recvfrom
(UDP)

send/write
(TCP)

sendto
(UDP)

35

Socket API
Client


socket
socket server socket server server server server
36

connect

recv/read
(TCP)

recvfrom
(UDP)

send/write
(TCP)

sendto
(UDP)

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


family: Selects the protocol family to be used; defined in <sys/socket.h>; use PF_INET for TCP or UDP. PF_UNIX AF_INET IPv4 AF_INET6 IPv6 AF_LOCAL Unix domain protocols ~ IPC AF_ROUTE Routing sockets ~ appls and kernel AF_KEY Key socket AF_UNIX AF_IMPLINK type: Semantics of desired communication protocol; defined in <sys/socket.h>; SOCK_STREAM for TCP; SOCK_DGRAM for UDP; others are possible. SOCK_STREAM stream socket SOCK_DGRAM datagram socket SOCK_RAW raw socket SOCK_PACKET datalink (Linux) protocol: Normally only one per type; defined in /etc/protocols; 6 for TCP, 17 for UDP; Can use 0 (zero) and the socket call will figure out the right protocol to use for the specified type field.

Return: return a integer identifier called a handle. (nonnegative descriptor if OK, negative number on error)

Example: To create a TCP socket: int sock; sock = socket(PF_INET, SOCK_STREAM, 0);
37

int connect(int s, struct sockaddr *name, int namelen);


s: file descriptor of socket; returned by call to socket. name: address of the socket data structure; must fill-in this structure before calling connect. namelen: length of name data structure; sizeof(name).

Example: if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror(connect"); exit(1); }

38

int bind(int socket, const struct sockaddr *address, int addrlen);

Server TCP socket

UDP address (IP address & TCP/UDP port)

socket: Descriptor identifying an unbound socket. (returned by the socket function.) address: A pointer to a protocol-specific address addrlen: Length of the value in the name parameter, in bytes.

Example: if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror("bind"); exit(1); }

39

int listen(int s, int backlog);


if(listen(fd, 5) < 0) { perror(listen); exit(1); }

40

int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

s: the socket the Server is listening to. addr will be the address (IP + port) of the client that sent the connection request.

Return: (new) socket for communicating with this client. Example: newfd = accept(fd, (struct sockaddr*) &cli, &cli_len); if(newfd < 0) { perror("accept"); exit(1); }

41

int send (int socket, char *message, int msg_len, intflags) (TCP) int write(int socket, void *msg, int len); (TCP) int recv (int socket, char *buffer, int buf_len, int flags) (TCP)

42

int read(int socket, void *msg, int len); (TCP)

Example: int fd; /* socket descriptor */ char buf[512]; /* used by read() */ int nbytes; /* used by read() */ if((nbytes = read(newfd, buf, sizeof(buf))) < 0) { perror(read); exit(1); }

43

int sendto (int socket, void *msg, int len, int flags, struct sockaddr * to, int tolen ); (UDP)

socket: Descriptor identifying a bound socket. msg: Buffer containing the data to be transmitted. len: Length of the data in buf, in bytes. flags: Indicator specifying the way in which the call is made. (usually set to 0) to: Optional pointer to a sockaddr structure that contains the address of the target socket. tolen: Size of the address in to, in bytes.

Example: int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by sendto() */ srv.sin_family = AF_INET; srv.sin_port = htons(80); srv.sin_addr.s_addr = inet_addr(128.2.35.50); nbytes = sendto(fd, buf, sizeof(buf), 0 , (struct sockaddr*) &srv, sizeof(srv)); if(nbytes < 0) { perror(sendto); exit(1); }
44

int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr *from, int *fromlen); (UDP)

Example: int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ struct sockaddr_in cli; /* used by recvfrom() */ char buf[512]; /* used by recvfrom() */ int cli_len = sizeof(cli); /* used by recvfrom() */ int nbytes; /* used by recvfrom() */ nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */, (struct sockaddr*) &cli, &cli_len); if(nbytes < 0) { perror(recvfrom); }

45

unsigned long int htonl(unsigned long int hostlong); Host-to-network byte order for a long word (4 bytes) Host-tounsigned short int htons(unsigned short int hostshort); Host-to-network byte order for a short word (2 bytes) Host-tounsigned long int ntohl(unsigned long int netlong); Network-to Network-to-host byte order for a long word unsigned short int ntohs(unsigned short int netshort); Network-to Network-to-host byte order for a short word

46

struct hostent *gethostbyname (const char *hostname);

<netdb.h> struct hostent { char *h_name; /* official (canonical) name of host */ char **h_aliases; /* ptr to array of ptrs to alias names */ int h_addrtype; /* host addr type: AF_INET or AF_INET6 */ int h_length; /* length of address: 4 or 16 */ char **h_addr_list; /* ptr to array of ptrs with IPv4/IPv6 addrs */ }; #define h_addr h_addr_list[0] /* first address in list */

47

S-ar putea să vă placă și