Udp

基于无连接的Udp编程.

代码注释,如下:

  
  
  
  
  1. //Server.cpp  
  2. #include <stdio.h>  
  3. #include <winsock2.h>  
  4. #pragma comment(lib,"WS2_32.lib")  
  5.  
  6. #define BUF_SIZE 64  
  7.  
  8. int main()  
  9. {  
  10.     WSADATA wsadata; //包含DLL期望程序使用的版本和支持的最高版本等字段  
  11.     int retVal;  
  12.  
  13.     SOCKET sServer;  
  14.     char buf[BUF_SIZE];  
  15.  
  16.     //加载套接字DLL  
  17.     retVal=WSAStartup(MAKEWORD(2,2),&wsadata);  
  18.     if(retVal != 0)  
  19.     {  
  20.         printf("WSAStartup failed!\n");  
  21.     }  
  22.  
  23.     //创建套接字  
  24.     sServer=socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);  
  25.     if(sServer == INVALID_SOCKET)  
  26.     {  
  27.         printf("socket failed!\n");  
  28.  
  29.         WSACleanup();  
  30.         return -1;  
  31.     }  
  32.  
  33.     //绑定套接字(绑定套接字时,需要明确指出服务器的各个地址字段)  
  34.     SOCKADDR_IN addrServ;  
  35.     addrServ.sin_family=AF_INET; //套接字属于协议族TCP/IP  
  36.     addrServ.sin_port=htons(4999);  
  37.     addrServ.sin_addr.S_un.S_addr=INADDR_ANY;  
  38.  
  39.     retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));   
  40.     if(retVal == SOCKET_ERROR)  
  41.     {  
  42.         printf("bind failed!\n");  
  43.  
  44.         closesocket(sServer);  
  45.         WSACleanup();  
  46.         return -1;  
  47.     }  
  48.  
  49.       
  50.  
  51.     //接收数据  
  52.     SOCKADDR_IN addrClient;  //接收数据时,系统自动填充地址字段  
  53.     int len=sizeof(SOCKADDR_IN);  
  54.  
  55.     ZeroMemory(buf,BUF_SIZE);  
  56.  
  57.     retVal=recvfrom(sServer,buf,BUF_SIZE,0,(LPSOCKADDR)&addrClient,&len);  
  58.     if(retVal == SOCKET_ERROR)  
  59.     {  
  60.         printf("recvfrom failed!\n");  
  61.  
  62.         closesocket(sServer);  
  63.         WSACleanup();  
  64.         return -1;  
  65.     }  
  66.  
  67.     //  
  68.     printf("Server receives data: ");  
  69.     printf("%s\n",buf);  
  70.  
  71.     //  
  72.     closesocket(sServer);  
  73.     WSACleanup();  
  74.     return 0;  
  75. }  

 

  
  
  
  
  1. //Client.cpp  
  2. #include <stdio.h>  
  3. #include <winsock2.h>  
  4. #pragma comment(lib,"WS2_32.lib")  
  5.  
  6. #define BUF_SIZE 64  
  7.  
  8. int main()  
  9. {  
  10.     WSADATA wsadata;  
  11.     int retVal;  
  12.  
  13.     SOCKET sClient;  
  14.     char buf[BUF_SIZE];  
  15.  
  16.  
  17.     //加载套接字DLL  
  18.     retVal=WSAStartup(MAKEWORD(2,2),&wsadata);  
  19.     if(retVal != 0)  
  20.     {  
  21.         printf("WSAStartup failed!\n");  
  22.     }  
  23.  
  24.     //创建套接字  
  25.     sClient=socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);  
  26.     if(sClient == INVALID_SOCKET)  
  27.     {  
  28.         printf("socket failed!\n");  
  29.  
  30.         WSACleanup();  
  31.         return -1;  
  32.     }  
  33.  
  34.       
  35.     //发送数据(发送数据时,需要明确指出服务器的各个地址字段)  
  36.     SOCKADDR_IN addrServ;  
  37.     addrServ.sin_family=AF_INET; //套接字属于协议族TCP/IP  
  38.     addrServ.sin_port=htons(4999);  
  39.     addrServ.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");  
  40.  
  41.     ZeroMemory(buf,BUF_SIZE);  
  42.     strcpy(buf,"Hello Udp!");  
  43.  
  44.     retVal=sendto(sClient,buf,BUF_SIZE,0,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));  
  45.     if(retVal == SOCKET_ERROR)  
  46.     {  
  47.         printf("sendto failed!\n");  
  48.  
  49.         closesocket(sClient);  
  50.         WSACleanup();  
  51.         return -1;  
  52.     }  
  53.  
  54.     //  
  55.     closesocket(sClient);  
  56.     WSACleanup();  
  57.     return 0;  

 

*************************************************************************

补充说明:

1.

#define INVALID_SOCKET  (SOCKET)(~0)
#define SOCKET_ERROR            (-1)

#define INADDR_ANY              (u_long)0x00000000

 

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib") //#pragma comment(Comment-Type,"CommentString")

①头文件,只有函数的声明;(编译阶段连接进去)②库文件,如果有DLL,则充当索引的作用;如果没有,则包含函数的实现代码;(链接阶段)③DLL,在运行阶段,把函数的实现代码连接进去.

2.

 

typedef struct WSAData {
WORD wVersion; //系统希望程序使用的库版本
WORD wHighVersion; //系统支持的最高版本
char szDescription[WSADESCRIPTION_LEN+1];
char szSystemStatus[WSASYS_STATUS_LEN+1];
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char FAR* lpVendorInfo; } WSADATA,
*LPWSADATA;

3.

 

int WSAStartup(
  WORD wVersionRequested,
  LPWSADATA lpWSAData
);

Return Values

The WSAStartup function returns zero if successful. Otherwise, it returns one of the error codes listed in the following.(WSASYSNOREADY | WSAEFAULT)

 4.

对于main()函数而言,返0,则表示成功执行;返其它,则表示程序执行异常.

*************************************************************************

 

函数原型:

1.

int sendto(
  SOCKET s,
  const char* buf,
  int len,
  int flags,
  const struct sockaddr* to,
  int tolen
);

int flags, //MSG_OOB | MSG_DONTROUTE

 

int flags, //MSG_OOB | MSG_PEEK

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

你可能感兴趣的:(编程,职场,comment,休闲)