Documente Academic
Documente Profesional
Documente Cultură
cyt@pmlab.iecs.fcu.edu.tw
TCP / IP
Network
Network Access
Port(
65536 ports
port 0 ~ port 65535
Port 0
ports
20, 21: FTP 23: Telnet 80: HTTP
1024 ~ 5000
Port 80
Port 3333
4
Socket
LINK
Berkeley UNIX
Socket
interface function
Socket
1 2 3 4
TCP/IP
Socket
Socket
Socket
Socket Socket
TCP
IP
Socket Descriptor
Socket( )
TCP 80 21
UDP
23 AP3
3333 AP3
AP1
AP2
10
11
Socket(
socket()
), TCP
socket()
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
socket
4.
socket
15
socket()
bind()
Client IP
IP Client
connect()
TCP Server
accept System
read() write()
HTTP
close()
16
Client/Server
1.
Server
Server socket Server client socket socket socket port client read/write system call
2.
3.
4.
17
socket()
Socket Server
bind()
Server
local
IP
Port
listen()
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
25
Socket
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
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
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)
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
38
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.
39
40
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
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
<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