zoukankan      html  css  js  c++  java
  • 学习:WinSock UDP 短连接

    服务端:socket -> bind -> sento/recvfrom -> close
    客户端: socket -> sendto/recvfrom -> close


    服务端:

    #include<WinSock2.h>
    #include<iostream>
    #pragma comment(lib, "ws2_32.lib")
    
    using namespace std;
    
    int main(int argc, char * argv[]) {
    	const int BUFSIZE = 1024;
    	SOCKET		ServerSocket;
    	SOCKADDR_IN ServerAddr;
    	SOCKADDR_IN ClientAddr;
    	WSADATA		wsaData;
    	char		buf[BUFSIZE];
    	int ret;
    	
    	if ((ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {
    		cout << "WSAStartup调用失败" << endl;
    		return -1;
    	}
    
    	//创建套接字socket
    
    	if((ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
    		cout << "创建套接字失败" << WSAGetLastError() << endl;
    		WSACleanup();
    		return -1;
    	}
    
    	//绑定套接字
    	ServerAddr.sin_family = AF_INET;
    	ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    	ServerAddr.sin_port = htons(atoi(argv[1]));
    	
    	if(bind(ServerSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) {
    		cout << "绑定套接字失败 " << WSAGetLastError() << endl;
    		closesocket(ServerSocket);
    		WSACleanup();
    		return -1;
    	}
    	int ClientLen = sizeof(ClientAddr);
    	cout << "端口:" << argv[1] << "正在接收数据...." << endl;
    	while (true) {
    		//接收数据
    		ZeroMemory(buf, sizeof(buf));
    		if ((ret = recvfrom(ServerSocket, buf, sizeof(buf), 0, (SOCKADDR *)&ClientAddr, &ClientLen)) == SOCKET_ERROR) {
    			cout << "接收数据失败" << WSAGetLastError() << endl;
    			closesocket(ServerSocket);
    			WSACleanup();
    			return -1;
    		}
    		else {
    			//发送数据
    			cout << "服务端: " << buf << endl;
    			sendto(ServerSocket, buf, sizeof(buf), 0, (SOCKADDR *)&ClientAddr, ClientLen);
    		}
    	}
    
    	closesocket(ServerSocket);
    	WSACleanup();
    	return 0;
    }
    

    客户端:

    #include<WinSock2.h>
    #include<iostream>
    #pragma comment(lib, "ws2_32.lib")
    #pragma warning(disable:4996) //忽略旧函数使用缺陷的警告
    
    using namespace std;
    
    int main(int argc, char * argv[]) {
    	const int BUFSIZE = 1024;
    	WSADATA		WSAData;
    	SOCKET		ClientSocket;
    	SOCKADDR_IN ClientAddr;
    	char		buf[BUFSIZE];
    	
    	WSAStartup(MAKEWORD(2, 2), &WSAData);
    
    
    	//创建套接字
    	ClientSocket = socket(AF_INET,SOCK_DGRAM , IPPROTO_UDP);
    	
    	ClientAddr.sin_addr.s_addr = inet_addr(argv[1]);
    	ClientAddr.sin_port = htons(atoi(argv[2]));
    	ClientAddr.sin_family = AF_INET;
    	
    	int ClientAddrLen = sizeof(ClientAddr);
    	//发送数据
    	while (true) {
    		ZeroMemory(buf, sizeof(buf));
    		cout << "客户端: ";
    		cin >> buf;
    		sendto(ClientSocket, buf, sizeof(buf), 0, (SOCKADDR *)&ClientAddr, sizeof(ClientAddr));
    		recvfrom(ClientSocket, buf, sizeof(buf), 0, (SOCKADDR *)&ClientAddr, &ClientAddrLen);
    
    		cout << "服务端: " << buf << endl;
    	}
    
    	closesocket(ClientSocket);
    	WSACleanup();
    	return 0;
    }
    
  • 相关阅读:
    转义字符:html、mysql、postgresql、json、php
    php_mysql、php_mysqli 与 pdo_mysql 的区别与选择
    一件小事测试各个搜索引擎:谷歌、bing、有道、百度、搜狗、360
    一键安装lnmp:自动检测最新稳定版、无需root权限
    apache graceful 与 cpu占用率
    互联网创业的准备——版本控制与上线
    201212互联网创意、创业项目整理
    【图】游东天目山
    那些在11gR2中可能惹祸的新特性,一张列表帮助你摆脱升级11gR2带来的烦恼
    【转】卡巴斯基安全公告称甲骨文数据库存在加密漏洞
  • 原文地址:https://www.cnblogs.com/zpchcbd/p/12177554.html
Copyright © 2011-2022 走看看