UDP之socket编程实例
生活随笔
收集整理的這篇文章主要介紹了
UDP之socket编程实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
不講解了,要求讀者:
1. 知道UDP是什么
2. 索求Windows環境下的socket編程函數
3. 已經配置好了編譯環境——在Tools-option-中的設置
客戶端代碼
#include <stdio.h> #include <winsock2.h> #include <iostream> #pragma comment(lib, "ws2_32.lib") using namespace std;bool BindSocket(SOCKET& socket,short port ,const char* Ip){sockaddr_in address;address.sin_family=AF_INET;address.sin_addr.s_addr =inet_addr(Ip);address.sin_port=htons(port);if(SOCKET_ERROR == bind(socket,(const struct sockaddr*)&address,sizeof(struct sockaddr)) ){return false;} return true; }int main() {WORD wVersionRequested;WSADATA wData;int err;int port; //客戶端端口號 int Otherport; //服務器端口號 char IPaddr[18]; //客戶端Ip char OtherIP[18]; //服務器Ip wVersionRequested = MAKEWORD( 1, 1 ); // 請求WinSock庫err = WSAStartup( wVersionRequested, &wData );if(err!=0) {return -1;} // 返回值為零時表示成功WSAStartupSOCKET sockCli = socket(AF_INET, SOCK_DGRAM, 0); //創建Socket if(sockCli < 0){cout << "socket creation failed" << endl;return 0; } cout << "socket created" << endl;cout << "請輸入本機IP:" << endl; //輸入客戶端IP和端口號 gets(IPaddr);printf("請輸入本機端口號:\n");cin >> port;if(!BindSocket(sockCli,port,IPaddr)){ //將本地IP地址和端口號綁定到所創建的套接字上cout << "Bind failed" << endl;return 0; } cout << "Bind Socket success " << endl;sockaddr_in address; //服務器的IP和端口號 cout << "請輸入服務器的IP" << endl;cin >> OtherIP;cout << "請輸入服務器的端口號" << endl;cin >> Otherport;address.sin_family=AF_INET;address.sin_addr.s_addr=inet_addr(OtherIP); address.sin_port=htons(Otherport);char talk[100]="t"; for(int i=0;i<100;i++){ //發送100個數據包 sendto(sockCli,talk, sizeof(talk), 0,(const struct sockaddr*)&address,sizeof(struct sockaddr) );}closesocket(sockCli); WSACleanup(); return 0; }服務器代碼:
#include <stdio.h> #include <iostream> // c++ include lib from windows #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") using namespace std;bool BindSocket(SOCKET& socket,short port ,const char* Ip){sockaddr_in address;// set address of socket from socket family AF_INET;address.sin_family=AF_INET;address.sin_addr.s_addr =inet_addr(Ip);// set machine IP; address.sin_port=htons(port); // set machine port// all above belong to net words;if(SOCKET_ERROR == bind(socket,(const struct sockaddr*)&address,sizeof(struct sockaddr)) ){return false;} return true; }int main() {WORD wVersionRequested;WSADATA wsaData;int err;int port; //服務器端口號 char IPaddr[18]; //服務器Ip sockaddr_in address;int n=sizeof(struct sockaddr);char talk[100]={0};char buffer[100] = {0};wVersionRequested = MAKEWORD( 1, 1 ); // 請求WinSock庫 err = WSAStartup( wVersionRequested, &wsaData );if(err!=0) {return -1;} SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0); //創建Socket /* Something about socket return value. */if(sockSrv < 0){cout << "socket creation failed" << endl;return 0;}cout << "socket created" << endl;cout << "請輸入本機IP:" << endl;cin >> IPaddr;printf("請輸入本機端口號:\n");cin >> port;//cout << IPaddr << endl << port << endl;while(! BindSocket(sockSrv,port,IPaddr)){ //將本地IP地址和端口號綁定到所創建的套接字上 cout << "Bind fail" << endl;cout << "請輸入本機IP:" << endl;cin >> IPaddr;printf("請輸入本機端口號:\n");cin >> port;}cout << "Bind Socket success " << endl;/*這里不知道對不對*/ int num =0;// over-time setting, and ACKpp setting;// more about recvfrom function;while(1){ //接受來自客戶端的數據包 if(recvfrom(sockSrv,buffer, 100,0,(struct sockaddr*)&address,&n) == SOCKET_ERROR ) {cout << "接收失敗" << endl; }else{num++;}cout << num << endl;// output situation;}closesocket(sockSrv); WSACleanup(); return 0; }總結
以上是生活随笔為你收集整理的UDP之socket编程实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android pad刷机,安卓平板刷机
- 下一篇: eXtremeComponents简单应