Win32 网络编程基本函数(2)

时间:2022-12-15 09:22:41

 

一.获取主机网络服务数据

如ftp,http,smtp等信息

getservbyport && getservbyname

main()
{
struct servent *s;
s=getservbyname("telnet", "tcp");
printf("%s %d/%s \n", s->s_name, ntohs(s->s_port), s->s_proto);
}
main()
{
struct servent *s;
s=getservbyport(htons(23), "tcp");
printf("%s %d/%s\n", s->s_name, ntohs(s->s_port), s->s_proto);
}

二.获取协议数据getprotobynumber和getprotobyname

int number;
struct protoent *protocol;
for(number=0; number<100; number++)
{
protocol = getprotobynumber(number);
if(protocol == (struct protoent * ) NULL) continue;
printf("%2d: %-10s: %-10s\n", protocol->p_proto, protocol->p_name, protocol->p_aliases[0]);
}

输出结果 
0: ip        : IP
1: icmp      : ICMP
3: ggp       : GGP
6: tcp       : TCP
8: egp       : EGP
12: pup       : PUP
17: udp       : UDP

三.getsockname && getpeername

getsockname 用于获取本地名字,适用于已连接未绑定.getpeername用于获取远程连接名字.

其对应于.net下socket对象的LocakEndPoint和RemoteEndPoint属性

参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.localendpoint(VS.80).aspx

http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.remoteendpoint(VS.80).aspx

四.六个异步获取数据的函数

WSAAsyncGetHostByAddr,WSAAsyncGetHostByName,WSAAsyncGetProtoByName,
WSAAsyncGetProtoByNumber,WSAAsyncGetServByName,WSAAsyncGetServByPort

以发消息的方式

char s[100];
WSAAsyncGetServByName(hWnd,WM_SOCKET1,"ftp","tcp",s,100);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SOCKET1:
{

return 0;
}
...

五.sockaddr和string互转

sockaddr sin2;
INT size=sizeof(sockaddr);
WSAStringToAddress("192.168.0.1:1200",AF_INET,NULL,&sin2,&size);
char b2[14];
DWORD size2=sizeof(sockaddr)+1;
WSAAddressToString(&sin2,sizeof(sockaddr_in),NULL,b2,&size2);

六.WSAHtonl,WSAHtons,WSANtohl,WSANtohs

 对应htonl,htons,ntohl,ntohs版本,属于windows下的扩展版本,使用WSA函数需要依赖Winsock DLL,同时返回值还进行的错误提示.小写的函数属于posix标准函数,可以跨操作系统

七.加载扩展函数

// 加载扩展函数AcceptEx
GUID GuidAcceptEx = WSAID_ACCEPTEX;
DWORD dwBytes;
WSAIoctl(pListen->s,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&GuidAcceptEx,
sizeof(GuidAcceptEx),
&pListen->lpfnAcceptEx,
sizeof(pListen->lpfnAcceptEx),
&dwBytes,
NULL,
NULL);

用WSAIoctl方法加载扩展函数

1.AcceptEx:The AcceptEx function accepts a new connection, returns the local and remote address, and receives the first block of data sent by the client application.

BOOL AcceptEx(
  __in   SOCKET sListenSocket,
  __in   SOCKET sAcceptSocket,
  __in   PVOID lpOutputBuffer,
  __in   DWORD dwReceiveDataLength,
  __in   DWORD dwLocalAddressLength,
  __in   DWORD dwRemoteAddressLength,
  __out  LPDWORD lpdwBytesReceived,
  __in   LPOVERLAPPED lpOverlapped
);

2.ConnectEx

The ConnectEx function establishes a connection to a specified socket, and optionally sends data once the connection is established. The ConnectEx function is only supported on connection-oriented sockets.

BOOL PASCAL ConnectEx(
  __in      SOCKET s,
  __in      const struct sockaddr *name,
  __in      int namelen,
  __in_opt  PVOID lpSendBuffer,
  __in      DWORD dwSendDataLength,
  __out     LPDWORD lpdwBytesSent,
  __in      LPOVERLAPPED lpOverlapped
);

typedef void (*
            LPFN_CONNECTEX)( );
         

3.DisconnectEx

The DisconnectEx function closes a connection on a socket, and allows the socket handle to be reused.

BOOL DisconnectEx(
  __in  SOCKET hSocket,
  __in  LPOVERLAPPED lpOverlapped,
  __in  DWORD dwFlags,
  __in  DWORD reserved
);

socket本来没有Disconnect方法,必须close、create之后才能connect,此处做了一个封装

4.getaddrinfo和freeaddrinfo的Unicode版本(GetAddrInfoW和FreeAddrInfoW)

int WSAAPI GetAddrInfoW(
  __in_opt  PCWSTR pNodeName,
  __in_opt  PCWSTR pServiceName,
  __in_opt  const ADDRINFOW *pHints,
  __out     PADDRINFOW *ppResult
);

void WSAAPI FreeAddrInfoW(
  __in  PADDRINFOW pAddrInfo
);

结构体也随之变化

typedef struct addrinfoW {
  int              ai_flags;
  int              ai_family;
  int              ai_socktype;
  int              ai_protocol;
  size_t           ai_addrlen;
  PWSTR            ai_canonname;
  struct sockaddr *ai_addr;
  struct addrinfoW *ai_next;
} ADDRINFOW, *PADDRINFOW;

5.InetNtop&& InetPton

The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form its numeric binary form. The ANSI version of this function is inet_pton.

Linux下IP地址转换函数,可以在将IP地址在“点分十进制”和“整数”之间转换 而且,inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6。算是比较新的函数了。

InetPton是Unicode版本

6.InetNtop && inet_ntop

The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The ANSI version of this function is inet_ntop.