1 / 12

Winsock programming

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().

arnaud
Download Presentation

Winsock programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Winsock programming

  2. TCP/IP • UDP • TCP • Winsock • #include <winsock.h> • wsock32.lib

  3. int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); • int WSACleanup (void); • int WSAGetLastError (void); • #define h_errno WSAGetLastError()

  4. 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 );

  5. 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 */ };

  6. 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);

  7. 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);

  8. 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)

  9. References • WinSock 網路程式設計之鑰 • 親手打造網際網路四大服務 • Unix Socket Programming • MSDN

  10. 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

More Related