基于C++和TCP的Socket通信

很长时间没写博客了,希望自己可以坚持下去,从小白开始成长吧,也希望各位读者能给点鼓励。

本文实现的功能是基于C++和TCP进行服务端客户端进行通信,希望给你给读者一定的帮助。

一、服务端

具体步骤:

1 初始化scoket 资源,注意,一定要在同一段代码中实现,不然可能会出问题。

2 构造服务端socket

3 绑定监听接口

4 开始监听

5 接收客户端连接,进行通信,继续监听。

#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include "pch.h"
#include <iostream>
#include <Winsock2.h>

using namespace std;

int main01()
{
	WSADATA wsa;
	/*初始化socket资源*/
	if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
	{
		return -1;   //代表失败
	}

	/*构造服务端socket*/
	SOCKET Listen_Sock = socket(AF_INET, SOCK_STREAM, 0);

	/*配置监听地址和端口*/
	SOCKADDR_IN serverAddr;
	ZeroMemory((char *)&serverAddr, sizeof(serverAddr));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(1234);           /*本地监听端口:1234*/
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /*有IP*/

	/*绑定监听接口*/
	bind(Listen_Sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
	listen(Listen_Sock, 5);

	SOCKADDR_IN addrClient;
	int len = sizeof(SOCKADDR);

	char nullBuf[64];
	for (int i = 0; i < 64; i++)
	{
		nullBuf[i] = 0;
	}
	cout << "等待连接" << endl;
	while (true)
	{
		SOCKET sockConn = accept(Listen_Sock, (SOCKADDR*)&addrClient, &len);
		cout << "连接成功!" << endl;

		while (true)
		{
			char sendBuf[64];
			char recvBuf[64];
			int sendLen;

			memcpy(recvBuf, nullBuf, 64);
			recv(sockConn, recvBuf, 64, 0);

			if (recvBuf[0] == '#')
			{
				cout << "关闭连接" << endl;
				break;
			}

			cout << "舒辰:";
			printf("%s\n", recvBuf);

			memcpy(sendBuf, nullBuf, 64);
			cout << "宝宝:" ;
			cin >> sendBuf;
			
			sendLen = 0;
			for (int i = 0; i < 64; i++)
			{
				if (sendBuf[i] == '\0')
				{
					sendLen = i;
					break;
				}
			}
			send(sockConn, sendBuf, sendLen+1, 0);
		}
		
		closesocket(sockConn);
	}

	WSACleanup();
    std::cout << "Hello World!\n"; 
	return 0;
}

2 客户端

1 初始化scoket 资源,注意,一定要在同一段代码中实现,不然可能会出问题。

2 构造服务端socket

3 输入IP地址和端口进行连接

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include "pch.h"
#include <iostream>
#include <sstream>
#include <Winsock2.h>
#include< Ws2tcpip.h>


using namespace std;

int main()
{
	WSADATA wsa;
	/*初始化socket资源*/
	if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
	{
		return -1;   //代表失败
	}

	/*构造客户端socket*/
	SOCKET Client_Sock = socket(AF_INET, SOCK_STREAM, 0);

	SOCKADDR_IN addrSrv;
	int res = inet_pton(AF_INET,"127.0.0.1",&addrSrv.sin_addr.S_un.S_addr); //inet_addr
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(1234);
	connect(Client_Sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	cout << "连接成功!" << endl;

	char nullBuf[64];
	int sendLen;
	for (int i = 0; i < 64; i++)
	{
		nullBuf[i] = 0;
	}
	char sendBuf[64];
	char recvBuf[64];

	while (true)
	{
		memcpy(sendBuf, nullBuf, 64);
		cout << "舒辰:";
		cin >> sendBuf;
		if (sendBuf[0] == '#')
		{
			cout << "关闭连接" << endl;
			send(Client_Sock, sendBuf, sendLen + 1, 0);
			break;
		}
		sendLen = 0;
		for (int i = 0; i < 64; i++)
		{
			if (sendBuf[i] == '\0')
			{
				sendLen = i;
				break;
			}
		}
		send(Client_Sock, sendBuf, sendLen + 1, 0);
		
		memcpy(recvBuf, nullBuf, 64);
		recv(Client_Sock, recvBuf, 64, 0);

		cout << "宝宝:";
		printf("%s\n", recvBuf);
	}
	closesocket(Client_Sock);

	WSACleanup();
    std::cout << "Hello World!\n"; 
	return 0;
}

具体的代码就这些,希望能帮到你。

发布了11 篇原创文章 · 获赞 13 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览