Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.

Similar presentations


Presentation on theme: "Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib."— Presentation transcript:

1 Winsock programming

2  TCP/IP UDP TCP  Winsock #include wsock32.lib

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

4  SOCKET socket( int af, int type, int protocol );  typedefu_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  int bind( SOCKET s, const struct sockaddr FAR *name, int namelen );  int listen( SOCKET s, int backlog );  SOCKET accept( SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen );  int connect( SOCKET s, const struct sockaddr FAR *name, int namelen ); socket descriptor Protocol Local IP address Local port Remote IP address Remote port

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

9

10  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)

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

12 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


Download ppt "Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib."

Similar presentations


Ads by Google