1、
2、
>>> int socket(int af, int type,int protocol);//创建套接字,返回从文件描述表中取出新的索引号(int);AF_INET,SOCK_DGRAM/SOCK_STREAM,0;阻塞式socket
int close(int d);/int shutdown(int socket,int direction);//关闭
//将int s套接字转成nonblock
void nonblock(int s)
{
int flags;
flags = fcntl(s, F_GETEL,0);//获取
flags != O_NONBLOCK;
if(fcntl(s,F_SETFL,flags)<0) exit(1);//设置
}
struct sockaddr{
u_short sa_family;
char sa_data[14];
}
struct in_addr{
u_long s_addr;
};
struct sockaddr_in{
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}
>>> int connect(int s, const struct sock* name, socklen_t namelen);
int connectToServer(int sock)
{
struct sockaddr_in s_addr_in;
s_addr_in.sin_family = AF_INET;
s_addr_in.sin_port = htos(8001);
s_addr_in.sin_addr.s_addr = inet_addr(“127.0.0.1”);//ip转为usigned long 二进步数,相反 char* inet_ntoa(struct in_addr in);
if(connect(sock,(struct sockaddr*)&s_addr_in,sizeof(s_addr_in))<0)
return 0;
return 1;
}
//域名转成long 类型二进制地址
unsigned long getaddrbydomain(char* addr)
{
struct hostent *ph;
struct in_addr in;
ph = gethostbyname(addr);
memcpy((char**)&(in),ph->_addr,ph->h_length);
return (in.s_addr);
}
struct hostent
{
char h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;//名字服务器上的地址列表
}
>>> int bind(int s, const struct sockaddr* addr, socklen_t addrlen);
>>> int listen(int s, int backing);
>>> int accept(int s, struct sockaddr *addr, socklen_t *addrlen);//返回分配到的套接字
>>> ssize_t send(int s,const void *msg,size_t len, int flags);
>>> int recv(int s, void *buf, size_t len, int flags);//buf 接收到的数据,返收的数据长度;flags{MSG_OOB,MSG_PEEK/*从receive_queue取出数据到buf后不删除原来的*/}
>>> int write(int s, void *buf, size_t len);//与send相同
>>> int read(int s, void *buf ,size_t len);//recv
>>> int sendto (int s,const void *msg,size_t len, int flags, const struct sockaddr* to, socklen_t tolen);//相当于connect 与send组合
>>> int recvfrom(int s, void *buf ,size_t len, int flags, struct sockaddr* from, socklen_t * fromlen);//相当于accept与recv合并
>>> int select(int nfds, fd_set *readfds, fd_set * writefds, fd_set *exceptfds, struct timeval * timeout);
>>> int setsockopt(int s,int level, int optname, const void* optval, unsigned int optlen);/getsockopt
>>> void (*signal(int signum,void(*handler)(int)))int);
>>> errno
3. winsock
>>> int WASStartup(WORD wVersionRequest,LPWSADATA lpWSAData);
>>> int WSACleanup();
struct WSAData { WORD wVersion; WORD wHighVersion; char szDescription[WSADESCRIPTION_LEN+1]; char szSystemStatus[WSASYSSTATUS_LEN+1]; unsigned short iMaxSockets; unsigned short iMaxUdpDg; char FAR * lpVendorInfo;
};
>>> SOCKET socket(int af,int type,int protocol);
>>> int closesocket(SOCKET s);
>>> int ioctlsocket(SOCKET s,long cmd, u_long FAR* argp);//fcntl/ioctl
>>> int connect(SOCKET s, const struct sockaddr FAR* name,int namelen);
>>> 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 send ( SOCKET s, const char FAR * buf, int len, int flags );
>>> int recv ( SOCKET s, char FAR* buf, int len, int flags );
>>> sendto /recvfrom
>>> select/WSAAsyncSelect/WSAEventSelect
>>> getsockopt/setsockopt
>>> WSAGetLastError