Socket通信原理(3)

本文主要内容为树莓派与PC在局域网内的基于TCP的Socket通信,由于树莓派是Linux系统,而PC是Windows系统,所以要注意一些区别

这里将树莓派作为服务器端,PC作为客户端,连接后服务端向客户端发送信息

服务端-树莓派 socket_server_sms.cpp

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std; 
#define USEPORT 1234

int main()
{
//****创建套接字
	int serverSock = socket(AF_INET, SOCK_STREAM, 0); 
	//Windows中,AF_INET==PF_INET
	//Linux中,不同的版本这两者有微小差别.对于BSD是AF,对于POSIX是PF
	if (serverSock < 0)
	{
		cout << "socket creation failed" << endl; 
		exit(-1); 
	}
	cout << "socket creation successfully" << endl; 
//****绑定ip和端口
	struct sockaddr_in serverAddr; 
	memset(&serverAddr, 0, sizeof(serverAddr)); 
	serverAddr.sin_family = AF_INET; 
	serverAddr.sin_port = htons(USEPORT); 
	//INADDR_ANY绑定所有IP
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); 
//****绑定套接字
	if (bind(serverSock, 
		(struct sockaddr*)&serverAddr, 
		sizeof(struct sockaddr)) == -1)
	{
		cout << "Bind error, Port["<< USEPORT << "]" << endl; 
	       exit(-1); 
    }
	cout << "Bind successfully" << endl; 
	
//****监听
	if (listen(serverSock, 10) == -1)
	{
		cout << "Listen error!" << endl; 
	}
	cout << "Listening on port[" << USEPORT << "]" << endl; 

//****开始接收accept()
	struct sockaddr clientAddr; 
	int size = sizeof(struct sockaddr); 

	int clientSock = accept(serverSock, (struct sockaddr*)&clientAddr, (socklen_t*)&size); 

	cout << "****NEW client touched****" << endl; 

//****通信
	while (1)
	{
		string input; 
		cout << "input something ('quit' to shutdown)" << endl; 
		cin >> input; 
		send(clientSock, input.c_str(), input.length(), 0); 
		if (input == "quit")
		{
			cout << "shutdown"<<endl;
			break; 
		}
	}
	
	close(serverSock); 
	return 0; 
}

客户端-PC socket_client_raspi.cpp

#include <iostream>
#include <string>
#include <WinSock2.h> //包含WinSock2.h头文件
using namespace std;
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll 
#pragma warning(disable:4996)

int main()
{
//****初始化
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);		
	
//****创建套接字
	SOCKET sock = socket(PF_INET, SOCK_STREAM, 0);

//****创建sockAddr结构体
	sockaddr_in sockAddr;						
	memset(&sockAddr, 0, sizeof(sockAddr));	
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_port = htons(1234);
	sockAddr.sin_addr.s_addr = inet_addr(RASPI_IP); //树莓派的局域网IP

//****建立连接
	connect(sock, (SOCKADDR*)& sockAddr, sizeof(SOCKADDR));		
	cout << "客户端发送链接请求" << endl;

//****接收服务器传回的数据	
	while (1)
	{
		cout << "等待服务端发送信息.." << endl;
		
		char tack[MAXBYTE] = { 0 };
		recv(sock, tack, MAXBYTE, NULL); 
		
		//输出接收到的数据
		cout << "服务器: " << tack << endl;
		
		if (strcmp(tack, "quit") == 0)
		{
			cout << "shutdown" << endl; 
			break;
		}
	}

	//关闭套接字、终止使用 DLL
	closesocket(sock);
	WSACleanup();

	return 0;
}

运行测试

在树莓派编译运行socket_server_sms.cpp,开始监听

cmake .
make
./socket_server_sms

raspi-server

PC编译运行socket_client_raspi.cpp

pc-client

服务端发送信息测试

test

测试成功


树莓派与计算机简单通信成功,接下来是通过树莓派传送摄像头图像和使用灰度重心法找到的光斑中心信息到计算机

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus