IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 基于SOCKET的TCP客户端服务器实现(多线程) -> 正文阅读

[网络协议]基于SOCKET的TCP客户端服务器实现(多线程)

客户端:
1、头文件
(1)clientReceive.h

#pragma once
#ifndef _ser_listen_h_
#define _ser_listen_h_

void* clientReceive(void* arg);

#endif 

(2)message.h

#pragma once
#ifndef _message_h_
#define _message_h_
#define PORT 8888

struct message
{
	char message[32];
	short int startData;
	short int sizeData;
	short int serverPort;
	int serverIP;
	short int clientPort;
	int clientIP;
	short int endData;
};

#endif 

2、源文件
(1)tcpClient.cpp:

#include <winsock2.h>
#include <iostream>
#include <Ws2tcpip.h> 
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
#include <STDIO.H>
#include <string>
# include <stdlib.h>
#include"message.h"
#include"clientReceive.h"

//using namespace std;

int main(int argc, char* argv[])
{
	pthread_t threadClientReceive;

	//初始化套接字
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA data;
	if (WSAStartup(sockVersion, &data) != 0)
	{
		return 0;
	}

	//创建客户端套接字
	SOCKET clientReceived = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (clientReceived == INVALID_SOCKET)
	{
		printf("invalid socket !");
		return 0;
	}
	//客户端绑定地址信息
	struct sockaddr_in client;
	client.sin_family = AF_INET;  //地址簇,表示接收本机或其他机器传输
	client.sin_port = htons(PORT);  //16位TCP/UDP端口号
	client.sin_addr.S_un.S_addr = inet_addr("127.0.1.1");  //设置ip地址,表示本机ip
	if (bind(clientReceived, (struct sockaddr*)&client, sizeof(client)) < 0) //绑定本机结构体
	{
		printf("bind error !");
	}
	//客户端套接字连接服务器
	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(clientReceived, (struct sockaddr*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
	{
		printf("connect error !");
		closesocket(clientReceived);
		return 0;
	}

	message messageClient =
	{
		{ '1','1','1','1' },
		0x5002,
		sizeof(messageClient.message),
		0x8888,
		inet_addr("127.0.1.1"),
		0x1818,
		inet_addr("127.0.0.1"),
		0x5003
	};

	//发送数据
	char dataClient[64];
	::memset(dataClient, 0, 64);
	::memcpy(dataClient, &messageClient.startData, 2);
	::memcpy(dataClient + 2, &messageClient.sizeData, 2);
	::memcpy(dataClient + 4, &messageClient.serverPort, 2);
	::memcpy(dataClient + 6, &messageClient.serverIP, 4);
	::memcpy(dataClient + 10, &messageClient.clientPort, 2);
	::memcpy(dataClient + 12, &messageClient.serverIP, 4);
	::memcpy(dataClient + 16, &messageClient.message, messageClient.sizeData);
	::memcpy(dataClient + 16 + messageClient.sizeData, &messageClient.endData, 2);
	const char* sendData = (char*)(dataClient);

	pthread_create(&threadClientReceive, NULL, clientReceive, (void*)clientReceived);
	
	while(1)
	{
	send(clientReceived, sendData, sizeof(dataClient), 0);
	Sleep(1000);
	}
	
	closesocket(clientReceived);
	WSACleanup();
	system("pause");
	return 0;
}

(2)clientRecive.cpp

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"clientReceive.h"

void* clientReceive(void* arg)
{
	int receiveClient = (int)arg;
	char recData[255];
	while (1)
	{
		int ret = recv(receiveClient, recData, 64, 0);
		if (ret > 0)
		{
			recData[ret] = 0x00;
			printf(recData);
		}
		Sleep(1000);
	}
	return  NULL;
}

服务器:
1、头文件
(1)serverReceive.h

#pragma once
#ifndef _ser_listen_h_
#define _ser_listen_h_
#define PORT 8888

void* serverReceive(void* arg);

#endif 

(2)serverReceiveData.h

#pragma once
#ifndef _serverReceiveData_h_
#define _serverReceiveData_h_

struct serverReceiveData
{
	unsigned short int startData;
	unsigned short int sizeData;
	unsigned short int serverPort;
	struct in_addr serverIP;
	unsigned short int clientPort;
	struct in_addr clientIP;
	char message[32];
	unsigned short int endData;
}dataSever;

#endif 

2、源文件
(1)tcpSever.cpp

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"serverReceive.h"

//using namespace std;

int main(int argc, char* argv[])
{
	pthread_t threadSeverRecive;

	//初始化WSA
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA wsaData;
	if (WSAStartup(sockVersion, &wsaData) != 0)
	{
		return 0;
	}

	//创建套接字
	SOCKET serverReceived = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (serverReceived == INVALID_SOCKET)
	{
		printf("socket error !");
		return 0;
	}

	//绑定IP和端口
	struct sockaddr_in sever;
	sever.sin_family = AF_INET;  //地址簇,表示接收本机或其他机器传输
	sever.sin_port = htons(PORT);  //16位TCP/UDP端口号
	sever.sin_addr.S_un.S_addr = INADDR_ANY;  //设置ip地址,表示本机ip
	if (bind(serverReceived, (struct sockaddr*)&sever, sizeof(sever)) < 0) //绑定本机结构体
	{
		printf("bind error !");
	}

	if (listen(serverReceived, 5) == SOCKET_ERROR)
	{
		printf("listen error !");
		return 0;
	}

	//开始监听
	SOCKET severClient;
	struct sockaddr_in remoteAddr;
	int nAddrlen = sizeof(remoteAddr);  //返回所占有的字节数
	printf("等待连接...\n");

	severClient = accept(serverReceived, (struct sockaddr*)&remoteAddr, &nAddrlen);
	if (severClient == INVALID_SOCKET)
	{
		printf("accept error !");
	}
	printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));

	pthread_create(&threadSeverRecive, NULL, serverReceive, (void*)severClient);

	while (1)
	{
		const char* sendData = (char*)"111111111111\n";
		send(severClient, sendData, strlen(sendData), 0);
		Sleep(1000);
	}
	
	closesocket(serverReceived);
	WSACleanup();
	system("pause");
	return 0;
}



(2)serverReceive.cpp

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"serverReceiveData.h"
#include"serverReceive.h"

void* serverReceive(void* arg)
{
	char revData[255];
	int ret = 0;
	int serverClient = (int)arg; // 传过来的已连接套接字  

	//接收数据
	while (1)
	{
		ret = recv(serverClient, revData, 255, 0);
		if(ret>0)
		{ 
			revData[ret] = 0x00;

			memset(&dataSever, 0, sizeof(dataSever));
			memcpy(&dataSever.startData, revData, 2);
			memcpy(&dataSever.sizeData, revData + 2, 2);
			memcpy(&dataSever.serverPort, revData + 4, 2);
			memcpy(&dataSever.serverIP, revData + 6, 4);
			memcpy(&dataSever.clientPort, revData + 10, 2);
			memcpy(&dataSever.clientIP, revData + 12, 4);
			memcpy(&dataSever.message, revData + 16, 32);
			memcpy(&dataSever.endData, revData + 16 + 32, 2);

			printf("帧起始:%02x\n", dataSever.startData);
			printf("帧长度:%d\n", dataSever.sizeData);
			printf("源端口号:%02x\n", dataSever.serverPort);
			printf("源ip:%s\n", inet_ntoa(dataSever.serverIP));
			printf("目的端口号:%02x\n", dataSever.clientPort);
			printf("目的ip:%s\n", inet_ntoa(dataSever.clientIP));
			printf("传输数据:%s\n", dataSever.message);
			printf("帧结束:%02x\n", dataSever.endData);
		}
	Sleep(1000);
	}
	return  NULL;
}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-12-10 11:24:18  更:2021-12-10 11:24:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/8 5:51:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码