udp 网络应用程序

Ws2_32.lib
#include <WinSock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 1, 1 );
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    return;
}
 
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */
 
if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    WSACleanup( );
    return; 
}
 
/* The WinSock DLL is acceptable. Proceed. */
 SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM, 0 );
 SOCKADDR_IN addrSrv ;
 addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
 addrSrv.sin_family = AF_INET;
 addrSrv.sin_port = htons(6000);
 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
 SOCKADDR_IN addrClient;
 int len = sizeof(SOCKADDR);
 char recvBuf[100];
 recvfrom(sockSrv, recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
 printf("%s/n",recvfrom);
 closesocket(sockSrv);
 WSACleanup();
}
 --------------------------------------
WSAStartup ()  用于加载套接字  1 加载套接字库 2 进行套接字库的版本协商 

socket

The socket function creates a socket that is bound to a specific service provider.

SOCKET socket(
  int af,       
  int type,     
  int protocol  
); 创建套接字, 第一个参数 指点地址组,AF——INET(TCPIP) 第二是 socket 的类型 (socket——stream or socket——dgram)
第三个是 与特定的地址家族相关的协议 
 
Return Values

If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

-----------------------------

The bind function associates a local address with a socket.

int bind(
  SOCKET s,     要指定的绑定的 socket                       
  const struct sockaddr FAR *name, 指定 套接字的本地地址信息   
  int namelen  本地地址信息  的大小
                      
);

inet_addr

The inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDR structure.

unsigned long inet_addr(
  const char   FAR *cp  指定一个以点分十进制根式表示的ip地址
);   返回一个适合分配给s_addr 的u ——long 类型的数值

Parameters

cp
[in] Null-terminated character string representing a number expressed in the Internet standard ".'' (dotted) notation.

The listen function places a socket a state where it is listening for an incoming connection.

int listen(
  SOCKET s,    套接字描述符
  int backlog  等待连接队列的最大长度
);

accept

The accept function permits an incoming connection attempt on a socket.

SOCKET accept(
  SOCKET s, 套接字已经通过listen 函数将其设为监听状态
  struct sockaddr FAR *addr,指向一个缓冲区的指针 该缓冲区用来接收实体地址也就是当客户想服务器发起连接的时候 服务器接收这个连接,保存发起连接的这个客户端的IP 地址信息的端口信息
  int FAR *addrlen
);
The send function sends data on a connected socket. 
int send(
  SOCKET s,              
  const char FAR *buf,  
  int len,               
  int flags              
);
   
   
   
   

Parameters

   
   
   
   
s
[in] Descriptor identifying a connected socket.
buf
[in] Buffer containing the data to be transmitted.
len
[in] Length of the data in buf.
flags
[in] Indicator specifying the way in which the call is made.

recv

The recv function receives data from a connected or bound socket.

int recv(
  SOCKET s,       
  char FAR *buf,  
  int len,        
  int flags       
);

Parameters

s
[in] Descriptor identifying a connected socket.
buf
[out] Buffer for the incoming data.
len
[in] Length of buf.
flags
[in] Flag specifying the way in which the call is made.

connect

The connect function establishes a connection to a specified socket.

int connect(
  SOCKET s,     即将在其上建立连接的那个套接字                     
  const struct sockaddr FAR *name,   设定连接的服务器端的地址信息
  int namelen                        
);

Parameters

s
[in] Descriptor identifying an unconnected socket.
name
[in] Name of the socket to which the connection should be established.
namelen
[in] Length of name.

recvfrom

The recvfrom function receives a datagram 数据报and stores 保存 the source address.

int recvfrom(
  SOCKET s,                   
  char FAR* buf,              
  int len,                    
  int flags,                  
  struct sockaddr FAR *from,  
  int FAR *fromlen            
);

sendto

The sendto function sends data to a specific destination.

int sendto(
  SOCKET s,                        
  const char FAR *buf,            
  int len,                         
  int flags,                       
  const struct sockaddr FAR *to,  
  int tolen                        
); 
htons  把一个ushort (16)类型的值 从主机字节顺序转换为tcpip 的网络字节序
htonl  -----ulong (32)-----------
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 1, 1 );
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    return;
}
 
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */
 
if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    WSACleanup( );
    return; 
}
 
/* The WinSock DLL is acceptable. Proceed. */
 SOCKET sockClient= socket(AF_INET,SOCK_DGRAM, 0 );
 SOCKADDR_IN addrSrv ;
 addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
 addrSrv.sin_family = AF_INET;
 addrSrv.sin_port = htons(6000);
 sendto(sockClient,"hello",strlen("hello")+1,0,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
 closesocket(sockClient);
 WSACleanup();
}

你可能感兴趣的:(udp 网络应用程序)