zoukankan      html  css  js  c++  java
  • 网络编程这结构体发送

    client

    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    #define _CRT_SECURE_NO_WARNINGS
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    #include <memory>
    
    #pragma pack(1)
    #pragma comment(lib,"ws2_32.lib")
    
    typedef struct MSG_def {
    	int num;
    	char buf[256];
    	MSG_def() {
    		num = 0;
    		memset(buf, 0, 256);
    	}
    }MyMsg;
    
    
    int main(int argc, char* argv[])
    {
    	WORD sockVersion = MAKEWORD(2, 2);
    	WSADATA data;
    	if (WSAStartup(sockVersion, &data) != 0)
    	{
    		return 0;
    	}
    
    	SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (sclient == INVALID_SOCKET)
    	{
    		printf("invalid socket !");
    		return 0;
    	}
    
    	struct sockaddr_in serAddr;
    	serAddr.sin_family = AF_INET;
    	serAddr.sin_port = htons(8888);
    	serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    	if (connect(sclient, (struct sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
    	{
    		printf("connect error !");
    		closesocket(sclient);
    		return 0;
    	}
    	MyMsg msg;
    	msg.num = 25;
    	sscanf("测式文本","%s",msg.buf);
    
    	send(sclient, (char*)&msg, sizeof(MSG), 0);
    	printf("%s","发送完成
    ");
    	closesocket(sclient);
    	WSACleanup();
    	return 0;
    }
    

    server

    #include <winsock2.h>
    #include <stdio.h>
    #include <memory>
    
    #pragma pack(1)
    #pragma comment(lib,"ws2_32.lib")
    
    typedef struct MSG_def{
    	int num;
    	char buf[256];
    	MSG_def(){
    		num = 0;
    		memset(buf, 0, 256);
    	}
    }MyMsg;
    
    int main(int argc, char* argv[])
    {
    	//初始化WSA
    	WORD sockVersion = MAKEWORD(2, 2);
    	WSADATA wsaData;
    	if (WSAStartup(sockVersion, &wsaData) != 0){
    		return 0;
    	}
    
    	//创建套接字
    	SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (slisten == INVALID_SOCKET){
    		printf("socket error !");
    		return 0;
    	}
    
    	//绑定IP和端口
    	struct sockaddr_in sin;
    	sin.sin_family = AF_INET;
    	sin.sin_port = htons(8888);
    	sin.sin_addr.S_un.S_addr = INADDR_ANY;
    	if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR){
    		printf("bind error !");
    	}
    
    	//开始监听
    	if (listen(slisten, 5) == SOCKET_ERROR){
    		printf("listen error !");
    		return 0;
    	}
    
    	//循环接收数据
    	SOCKET sClient;
    	struct sockaddr_in remoteAddr;
    	int nAddrlen = sizeof(remoteAddr);
    	printf("等待连接...
    ");
    	sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
    	while (1){
    		char revData[2048];
    		if (sClient == INVALID_SOCKET){
    			printf("accept error !");
    			continue;
    		}
    
    		//接收数据
    		printf("等待接收...
    ");
    		int ret = recv(sClient, revData, 2048, 0);
    		//printf(ret);
    		if (ret > 0){
    			MyMsg* pmsg = (MyMsg*)revData;
    			printf("num -> %d
    ",pmsg->num);
    			printf("buf -> %s
    ",pmsg->buf);
    
    		}
    		printf("数据接收完成...
    ");
    
    		printf("等待连接...
    ");
    		sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
    	}
    	closesocket(sClient);
    	closesocket(slisten);
    	WSACleanup();
    	return 0;
    }
    
  • 相关阅读:
    vue脚手架搭建项目步骤(转载)
    使用css样式设置按钮禁用
    如何设置页面文本不可复制?
    System.IO.IOException:需要的特权不被客户端持有。
    vs中 git fetch与git pull的区别
    如何获取按钮开关选中的状态?
    Uncaught TypeError: XXX is not a function...
    解决bootstrap缩小布局会乱的问题,缩小一定程度后出现横向滚动条
    日期(年月日)与时间戳互转换
    Java中的Finally
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537542.html
Copyright © 2011-2022 走看看