Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Socket Programming

Similar presentations


Presentation on theme: "Introduction to Socket Programming"— Presentation transcript:

1 Introduction to Socket Programming
Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010

2 Socket Interface 是一種應用程式介面(API),介於application層與transport層之間,並且提供標準的函式以符合不同的網路傳輸規格。 最早的Socket Interface於1982年由柏克萊大學為支援UNIX作業系統上的TCP/IP應用所開發的Socket介面,稱為Berkeley Socket Interface,而其軟體則稱為Berkeley Software Distribution(BSD)。 Windows Sockets,簡稱Winsock,是以UNIX系統上Berkeley Sockets的函式為基礎,並加上了一些符合視窗環境特性的函式。

3 Socket Interface (con.)

4 Connection-oriented socket (TCP)

5 socket() Create an endpoint for communication
int socket( int domain, int type, int protocol ); domain PF_INET for IPv4 PF_INET6 for IPv6 type SOCK_STREAM for reliable TCP sockets SOCK_DGRAM for unreliable fast UDP sockets protocol default: 0

6 bind() Bind a name to a socket
int bind( int s, const struct sockaddr * name, socklen_t namelen ); s The file descriptor to be bound. name A pointer to the sockaddr structure that holds the address to be bound to the socket. namelen The length of the sockaddr structure pointed to by name.

7 bind() (con.) sa_data struct sockaddr {
sa_family_t sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; sa_family represents address family sa_data contains data about address

8 bind() (con.) struct in_addr { uint32_t s_addr;
// 32bit IPv4 address (4 bytes) // network byte ordered }; struct sockaddr_in { sa_family_t sin_family; // Address family (2 bytes) in_port_t sin_port; // Port number (2 bytes) struct in_addr sin_addr; // Internet address (4 bytes) char sin_zero[8]; // Empty (for padding) (8 bytes) }

9 bind() (con.) Example: struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); Bind (sockfd, (struct sockaddr *) & servaddr, sizeof(servaddr) );

10 connect() Initiate a connection on a socket
int connect( int s, const struct sockaddr * name, socklen_t namelen ); s The descriptor of the socket on which to initiate the connection. name The name of the socket to connect to for a SOCK_STREAM connection. namelen The length of the name, in bytes.

11 listen() Listen for connections on a socket
int listen( int s, int backlog ); s The descriptor for the socket that you want to listen on. You can create a socket by calling socket(). backlog The maximum length that the queue of pending connections may grow to. “backlog” = max # of ”pending connections” = handshake in progress or complete

12 accept() Accept a connection on a socket
int accept( int s, struct sockaddr * addr, socklen_t * addrlen ); s A socket that's been created with socket(). addr A result parameter that's filled in with the address of the connecting entity, as known to the communications layer. addrlen It should initially contain the amount of space pointed to by addr; on return it contains the actual length (in bytes) of the address returned.

13 send() Send a message to a connected socket
ssize_t send( int s, const void * msg, size_t len, int flags ); msg A pointer to the message that you want to send. len The length of the message. flags MSG_OOB MSG_DONTROUTE

14 recv() Receive a message from a socket
ssize_t recv( int s, void * buf, size_t len, int flags ); buf A pointer to a buffer where the function can store the message. len The size of the buffer. flags MSG_OOB MSG_PEEK MSG_WAITALL

15 close() Close a socket descriptor int close(int s);
Windows users: the function you need to use is called closesocket(), not close().

16 Connectionless socket (UDP)

17 sendto() Send a message to a socket at a specific address
ssize_t sendto( int s, const void * msg, size_t len, int flags, const struct sockaddr * to, socklen_t tolen ); to A pointer to a sockaddr object that specifies the address of the target. tolen A socklen_t object that specifies the size of the to address.

18 recvfrom() Receive a message from the socket at a specified address
ssize_t recvfrom( int s, void * buff, size_t len, int flags, struct sockaddr * from, socklen_t * fromlen ); from NULL, or a pointer to a sockaddr object where the function can store the source address of the message. fromlen A pointer to a socklen_t object that specifies the size of the from buffer. The function stores the actual size of the address in this object.

19 htons(), htonl(), ntohs(), ntohl()
Convert multi-byte integer types from host byte order to network byte order uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); htons() host to network short htonl() host to network long ntohs() network to host short ntohl() network to host long

20 Demo Enviroment OS: CentOS 5.5 Client Server Socket TCP (IPv4)
Socket UDP (IPv4) Socket TCP (IPv6)

21 Reference http://beej.us/guide/bgnet/output/html/multipage/index.html
Programming_&_IPv6_Translation Middleware.pdf


Download ppt "Introduction to Socket Programming"

Similar presentations


Ads by Google