150 likes | 370 Views
Winsock programming. TCP/IP UDP TCP Winsock # include <winsock.h> wsock32.lib. int WSAStartup ( WORD wVersionRequested , LPWSADATA lpWSAData ); int WSACleanup (void); int WSAGetLastError (void); # define h_errno WSAGetLastError().
E N D
TCP/IP • UDP • TCP • Winsock • #include <winsock.h> • wsock32.lib
int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); • int WSACleanup (void); • int WSAGetLastError (void); • #define h_errno WSAGetLastError()
SOCKET socket( intaf, inttype, intprotocol ); • typedef u_int SOCKET; • Address families • #define AF_INET 2 /* internetwork: UDP, TCP, etc. */ • Protocol families, same as address families for now. • #define PF_INET AF_INET • Types • #define SOCK_STREAM 1 /* stream socket */ • #define SOCK_DGRAM 2 • intbind( SOCKETs, const struct sockaddr FAR*name, intnamelen ); • intlisten(SOCKET s, int backlog); • SOCKETaccept( SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen); • int connect( SOCKET s, const struct sockaddr FAR *name, intnamelen );
typedef struct sockaddr_in SOCKADDR_IN; • struct sockaddr_in { short sin_family; // 2 u_short sin_port; // 2 struct in_addr sin_addr; // 4 char sin_zero[8]; // 8 }; • struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; // 4 struct { u_short s_w1,s_w2; } S_un_w; // 4 u_long S_addr; // 4 } S_un; • #define s_addrS_un.S_addr • #define INADDR_ANY (u_long)0x00000000 • struct sockaddr { u_short sa_family; /* address family */ char sa_data[14]; /* up to 14 bytes of direct address */ };
Data conversion functions • char FAR * inet_ntoa( struct in_addr in ); • unsigned long inet_addr( const char FAR *cp ); • u_long htonl( u_long hostlong ); • u_long ntohl( u_long netlong ); • u_short ntohs( u_short netshort ); • u_short htons( u_short hostshort ); • Socket information functions • int getpeername( SOCKET s, struct sockaddr FAR *name, int FAR *namelen); • int getsockname( SOCKET s, struct sockaddr FAR *name, int FAR *namelen);
int send( SOCKET s, const char FAR *buf, int len, int flags ); • int recv( SOCKET s, char FAR *buf, int len, int flags ); • int sendto( SOCKET s, const char FAR *buf, int len, int flags, const struct sockaddr FAR *to, int tolen); • int recvfrom( SOCKET s, char FAR* buf, int len, int flags, struct sockaddr FAR *from, int FAR *fromlen);
Select • int PASCAL FAR select (int nfds, fd_set FAR *readfds, fd_set FAR *writefds, fd_set FAR *exceptfds, const struct timeval FAR *timeout); • #define FD_SETSIZE 64 • typedef struct fd_set { u_int fd_count; /* how many are SET? */ SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */ } fd_set; • typedef struct fd_set FD_SET; • struct timeval { long tv_sec; /* seconds */ long tv_usec; /* and microseconds */ }; • typedef struct timeval TIMEVAL; • FD_CLR(fd, set) • FD_SET(fd, set) • FD_ZERO(set) • FD_ISSET(fd, set)
References • WinSock 網路程式設計之鑰 • 親手打造網際網路四大服務 • Unix Socket Programming • MSDN
Exercise • File transfer • Send a file to server or receive a file from server • Using TCP • Read until a block • Write until a block • Timeout