牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Socket学习笔记之一(常用基本定义)

常用类型的定义:
(以下定义从Winsock2.h文件中截取)
/*
 * The new type to be used in all
 * instances which refer to sockets.
 */
typedef UINT_PTR        SOCKET;//SOCKET原来就是一个unsigned int的指针而已

/*
 * Select uses arrays of SOCKETs.  These macros manipulate such
 * arrays.  FD_SETSIZE may be defined by the user before including
 * this file, but the default here should be >= 64.
 *
 * CAVEAT IMPLEMENTOR and USER: THESE MACROS AND TYPES MUST BE
 * INCLUDED IN WINSOCK2.H EXACTLY AS SHOWN HERE.
 */
#ifndef FD_SETSIZE
#define FD_SETSIZE      64
#endif /* FD_SETSIZE */
typedef struct fd_set {
        u_int fd_count;               /* how many are SET? */
        SOCKET  fd_array[FD_SETSIZE];   /* an array of SOCKETs */
} fd_set;//fd_set是用来select的,表示一个集合,STL中不是有个模板类set么
//对fd_set结构,Winsock2.h中还定义了四个宏定义来操作fd_set
//FD_CLR(fd, set),FD_SET(fd, set),FD_ZERO(set),FD_ISSET(fd, set)
//一般用法是先定义一个fd_set,使用之前调用fd_zero,再将一个socket放到fd_set中
//经过一系列操作后,用fd_isset判断某个socket是否还在set中。
//SOCKET fd;
//......
//fd_set ReadFds;
//FD_ZERO(&ReadFds);
//FD_SET(fd,&ReadFds);
//.....
//if ( FD_ISSET(fd,&ReadFds) )
//.....
/*
 * Structure used in select() call, taken from the BSD file sys/time.h.
 */
struct timeval {
        long    tv_sec;         /* seconds */
        long    tv_usec;        /* and microseconds */
};//这个也是用于select函数中的,超时参数
struct  hostent {//表示主机的结构
        char    FAR * h_name;           /* official name of host */
        char    FAR * FAR * h_aliases;  /* alias list */
        short   h_addrtype;             /* host address type */
        short   h_length;               /* length of address */
        char    FAR * FAR * h_addr_list; /* list of addresses */
#define h_addr  h_addr_list[0]          /* address, for backward compat */
};
/*
 * Internet address (old style... should be updated)//晕,什么时候update?
 *///原来每次看到in_addr结构都不知道为什么叫in_addr,原来是这样。。。
struct in_addr {
        union {
                struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
                struct { u_short s_w1,s_w2; } S_un_w;
                u_long S_addr; //大部分都用这个变量就够了(一个ip地址32位刚好一个unsigned long)
        } S_un;
#define s_addr  S_un.S_addr //这样定义以后用的很方便,in_addr.s_addr就可以了。
                                /* can be used for most tcp & ip code */
#define s_host  S_un.S_un_b.s_b2
                                /* host on imp */
#define s_net   S_un.S_un_b.s_b1
                                /* network */
#define s_imp   S_un.S_un_w.s_w2
                                /* imp */
#define s_impno S_un.S_un_b.s_b4
                                /* imp # */
#define s_lh    S_un.S_un_b.s_b3
                                /* logical host */
};
//这b1,b2,b3,b4,w1,w2不知道有乜用,代表乜意思。。。
#endif
/*
 * Socket address, internet style.
 */
struct sockaddr_in {
        short   sin_family;//地址家族一般都是AF_INET或者PF_INET
        u_short sin_port;//端口
        struct  in_addr sin_addr;//ip地址
        char    sin_zero[8];
};
/*
 * Structure used by kernel to store most//计算机内核中处理用的是这种表示
 * addresses.
 */
struct sockaddr {
        u_short sa_family;              /* address family */
        char    sa_data[14];            /* up to 14 bytes of direct address */
};
//仔细算一下sockaddr_in和sockaddr两个结构的大小是一样,所以可以直接强制类型转换赋值
//比如accept函数其中一个参数类型struct sockaddr FAR * ,你如果有一个struct sockaddr_in myaddr
//就可以直接accept(..(struct sockaddr *)myaddr.....
/*
 * WinSock 2 extension -- WSABUF and QOS struct, include qos.h
 * to pull in FLOWSPEC and related definitions
 */
typedef struct _WSABUF {//缓冲区,一般ms在带WSA前缀的accept,receive,send等函数中才用到
    u_long      len;     /* the length of the buffer */
    char FAR *  buf;     /* the pointer to the buffer */
} WSABUF, FAR * LPWSABUF;
typedef struct WSAData {
        WORD                    wVersion;
        WORD                    wHighVersion;
#ifdef _WIN64
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
#else
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
#endif
} WSADATA, FAR * LPWSADATA;//其实这个结构不需要怎么了解,因为它在用的很少,就是在调用WSAStartup才用到一次

/*
 * Address families.
 */
#define AF_UNSPEC       0               /* unspecified */
/*
 * Although  AF_UNSPEC  is  defined for backwards compatibility, using
 * AF_UNSPEC for the "af" parameter when creating a socket is STRONGLY
 * DISCOURAGED.    The  interpretation  of  the  "protocol"  parameter
 * depends  on the actual address family chosen.  As environments grow
 * to  include  more  and  more  address families that use overlapping
 * protocol  values  there  is  more  and  more  chance of choosing an
 * undesired address family when AF_UNSPEC is used.
 */
#define AF_UNIX         1               /* local to host (pipes, portals) */
#define AF_INET         2               /* internetwork: UDP, TCP, etc. */
#define AF_IMPLINK      3               /* arpanet imp addresses */
#define AF_PUP          4               /* pup protocols: e.g. BSP */
#define AF_CHAOS        5               /* mit CHAOS protocols */
#define AF_NS           6               /* XEROX NS protocols */
#define AF_IPX          AF_NS           /* IPX protocols: IPX, SPX, etc. */
#define AF_ISO          7               /* ISO protocols */
#define AF_OSI          AF_ISO          /* OSI is ISO */
#define AF_ECMA         8               /* european computer manufacturers */
#define AF_DATAKIT      9               /* datakit protocols */
#define AF_CCITT        10              /* CCITT protocols, X.25 etc */
#define AF_SNA          11              /* IBM SNA */
#define AF_DECnet       12              /* DECnet */
#define AF_DLI          13              /* Direct data link interface */
#define AF_LAT          14              /* LAT */
#define AF_HYLINK       15              /* NSC Hyperchannel */
#define AF_APPLETALK    16              /* AppleTalk */
#define AF_NETBIOS      17              /* NetBios-style addresses */
#define AF_VOICEVIEW    18              /* VoiceView */
#define AF_FIREFOX      19              /* Protocols from Firefox */
#define AF_UNKNOWN1     20              /* Somebody is using this! */
#define AF_BAN          21              /* Banyan */
#define AF_ATM          22              /* Native ATM Services */
#define AF_INET6        23              /* Internetwork Version 6 */
#define AF_CLUSTER      24              /* Microsoft Wolfpack */
#define AF_12844        25              /* IEEE 1284.4 WG AF */
#define AF_IRDA         26              /* IrDA */
#define AF_NETDES       28              /* Network Designers OSI & gateway
                                           enabled protocols */
#define AF_TCNPROCESS   29
#define AF_TCNMESSAGE   30
#define AF_ICLFXBM      31

#define AF_MAX          32

/*
 * Protocol families, same as address families for now.
 */
#define PF_UNSPEC       AF_UNSPEC
#define PF_UNIX         AF_UNIX
#define PF_INET         AF_INET
#define PF_IMPLINK      AF_IMPLINK
#define PF_PUP          AF_PUP
#define PF_CHAOS        AF_CHAOS
#define PF_NS           AF_NS
#define PF_IPX          AF_IPX
#define PF_ISO          AF_ISO
#define PF_OSI          AF_OSI
#define PF_ECMA         AF_ECMA
#define PF_DATAKIT      AF_DATAKIT
#define PF_CCITT        AF_CCITT
#define PF_SNA          AF_SNA
#define PF_DECnet       AF_DECnet
#define PF_DLI          AF_DLI
#define PF_LAT          AF_LAT
#define PF_HYLINK       AF_HYLINK
#define PF_APPLETALK    AF_APPLETALK
#define PF_VOICEVIEW    AF_VOICEVIEW
#define PF_FIREFOX      AF_FIREFOX
#define PF_UNKNOWN1     AF_UNKNOWN1
#define PF_BAN          AF_BAN
#define PF_ATM          AF_ATM
#define PF_INET6        AF_INET6

#define PF_MAX          AF_MAX


下面这些函数属于基本常用函数,下次学习
u_long htonl(u_long hostlong)
u_short htons(u_short hostshort)
u_long ntohl(u_long netlong)
u_short ntohs(u_short netshort)
unsigned long inet_addr(const char* cp);
int getpeername(SOCKET s,struct sockaddr* name,int* namelen);
int getsockname(SOCKET s,struct sockaddr* name,int* namelen);
char* FAR inet_ntoa(struct in_addr in);
struct HOSTENT* FAR gethostbyaddr(const char* addr,int len,int type);
struct hostent* FAR gethostbyname(const char* name);
int gethostname(char* name,int namelen);
int WSAGetLastError(void);
int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData);
int ioctlsocket(SOCKET s,long cmd,u_long* argp);
int listen(SOCKET s,int backlog);
SOCKET accept(SOCKET s,struct sockaddr* addr,int* addrlen);
int send(SOCKET s,const char* buf,int len,int flags);
int recv(SOCKET s,char* buf,int len,int flags);



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=541158

posted on 2006-04-20 17:28 杨粼波 阅读(1654) 评论(0)  编辑 收藏 引用 所属分类: 网络编程


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理