visual_studio编译C代码使用tcp需要添加ws2_32.lib

使用visual_studio调试C中TCP

需要在代码中加入window库:如下:

#pragma comment(lib,"ws2_32.lib")

C中调试使用tcp的目的是:通过tcp发送指令,模拟嵌入式中的按键操作.
使用方法是:
vs运行后
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
数据通路建立,之后的操作,自己弄个协议就可以了,让嵌入式摆脱了实际按键测试.还是比较方便的

完整代码如下:



#define  _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#include "struct_link.h"
#define SERVER_PORT 6789 //侦听端口

#pragma comment(lib,"ws2_32.lib")

int K1 = -1, K2 = -1, K3 = -1, K4 = -1, K5 = -1;
void MenuOperate(void);

int main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int ret, length;
	int recv_len = 0;
	SOCKET sListen, sServer; //侦听套接字,连接套接字
	struct sockaddr_in saServer, saClient; //地址信息
	char* ptr;//用于遍历信息的指针
	//WinSock初始化
	wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL 的版本
	ret = WSAStartup(wVersionRequested, &wsaData);
	if (ret != 0)
	{
		printf("WSAStartup() failed!\n");
		return -1;
	}
	//创建Socket,使用TCP协议
	sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sListen == INVALID_SOCKET)
	{
		WSACleanup();
		printf("socket() faild!\n");
		return -1;
	}
	//构建本地地址信息
	saServer.sin_family = AF_INET; //地址家族
	saServer.sin_port = htons(SERVER_PORT); //注意转化为网络字节序
	saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用INADDR_ANY 指示任意地址

	//绑定
	ret = bind(sListen, (struct sockaddr*) & saServer, sizeof(saServer));
	if (ret == SOCKET_ERROR)
	{
		printf("bind() faild! code:%d\n", WSAGetLastError());
		closesocket(sListen); //关闭套接字
		WSACleanup();
		return -1;
	}

	//侦听连接请求
	ret = listen(sListen, 5);
	if (ret == SOCKET_ERROR)
	{
		printf("listen() faild! code:%d\n", WSAGetLastError());
		closesocket(sListen); //关闭套接字
		return -1;
	}

	printf("Waiting for client connecting!\n");
	printf("Tips: Ctrl+c to quit!\n");
	//阻塞等待接受客户端连接
	while (1)//循环监听客户端
	{
		length = sizeof(saClient);
		sServer = accept(sListen, (struct sockaddr*) & saClient, &length);
		if (sServer == INVALID_SOCKET)
		{
			printf("accept() faild! code:%d\n", WSAGetLastError());
			closesocket(sListen); //关闭套接字
			WSACleanup();
			return -1;
		}
		printf("accept is success!\n");
		printf("receive connect ip is %s ", inet_ntoa(saClient.sin_addr));
#if 0
		char receiveMessage[1024];
		ptr = (char*)&receiveMessage;
#else
		char* receiveMessage = (char*)calloc(2048, 0);
		ptr = receiveMessage;
#endif
		while (1)
		{
			//接收数据
			recv_len = recv(sServer, ptr, 1024, 0);
			switch (recv_len)
			{
			case 3:
				K1 = 0;
				printf("date is K1 ");
				break;
			case 4:
				K2 = 0;
				printf("date is K2 ");
				break;
			case 5:
				K3 = 0;
				printf("date is K3 ");
				break;
			default:
				K4 = 0;
				printf("date is too long\r\n ");
				break;
			}

			receiveMessage[recv_len] = 0;
			if (recv_len == SOCKET_ERROR)
			{
				printf("recv() failed!\n");
				return -1;
			}
			if (recv_len == 0) //客户端已经关闭连接
			{
				printf("Client has closed the connection\n");
				break;
			}
			char* sendData = "hello callback send data";
			send(sServer, sendData, strlen(sendData), 0);
			//            printf("recv is %s\n", ptr);
			memset(receiveMessage, 0, sizeof(receiveMessage));
		}
	}
}

好的,以下是一个简单的聊天室服务端的C++代码: ```c++ #include <iostream> #include <winsock2.h> //ws2_32库头文件 #pragma comment(lib,"ws2_32.lib") //ws2_32库链接 using namespace std; int main() { //初始化Winsock WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { cout << "WSAStartup failed: " << iResult << endl; return 1; } //创建监听socket SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { cout << "socket failed: " << WSAGetLastError() << endl; WSACleanup(); return 1; } //绑定端口 sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(1234); iResult = bind(listenSocket, (SOCKADDR*)&service, sizeof(service)); if (iResult == SOCKET_ERROR) { cout << "bind failed: " << WSAGetLastError() << endl; closesocket(listenSocket); WSACleanup(); return 1; } //开始监听 iResult = listen(listenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { cout << "listen failed: " << WSAGetLastError() << endl; closesocket(listenSocket); WSACleanup(); return 1; } //接受客户端连接 sockaddr_in clientAddr; int addrLen = sizeof(clientAddr); SOCKET clientSocket = accept(listenSocket, (SOCKADDR*)&clientAddr, &addrLen); if (clientSocket == INVALID_SOCKET) { cout << "accept failed: " << WSAGetLastError() << endl; closesocket(listenSocket); WSACleanup(); return 1; } cout << "Client connected: " << inet_ntoa(clientAddr.sin_addr) << endl; //接收客户端消息 char recvbuf[1024]; int recvbuflen = 1024; iResult = recv(clientSocket, recvbuf, recvbuflen, 0); if (iResult > 0) { cout << "Received: " << recvbuf << endl; } else if (iResult == 0) { cout << "Connection closing..." << endl; } else { cout << "recv failed: " << WSAGetLastError() << endl; closesocket(clientSocket); closesocket(listenSocket); WSACleanup(); return 1; } //发送消息给客户端 const char* sendbuf = "Welcome to the chat room!"; int sendbuflen = strlen(sendbuf); iResult = send(clientSocket, sendbuf, sendbuflen, 0); if (iResult == SOCKET_ERROR) { cout << "send failed: " << WSAGetLastError() << endl; closesocket(clientSocket); closesocket(listenSocket); WSACleanup(); return 1; } //关闭socket和Winsock closesocket(clientSocket); closesocket(listenSocket); WSACleanup(); return 0; } ``` 注意事项: 1. 该代码只支持单个客户端连接,若要支持多个客户端,则需要用多线程等技术进行改进。 2. 要在Windows系统上编译运行该代码需要使用Visual Studio等IDE,并在项目属性中添加ws2_32.lib库链接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值