You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
4.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<iostream>
#include<winsock.h>
#include <Windows.h>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"opencv_world3410.lib")
#pragma comment(lib,"opencv_world3410d.lib")
#pragma warning(disable:4996)
#define IMG_WIDTH 640 // 需传输图像的宽
#define IMG_HEIGHT 480 // 需传输图像的高
#define BUFFER_SIZE IMG_WIDTH*IMG_HEIGHT*3/32 //默认格式为CV_8UC3
using namespace cv;
using namespace std;
struct recvbuf
{
char buf[BUFFER_SIZE];
int flag;
};
void initialization();
int receive(cv::Mat& image, SOCKET s_accept)
{
static struct recvbuf data;
cv::Mat img(IMG_HEIGHT, IMG_WIDTH, CV_8UC3, cv::Scalar(0));
int needRecv = sizeof(recvbuf);
int count = 0;
while (1)
{
for (int i = 0; i < 32; i++)
{
int pos = 0;
int len0 = 0;
while (pos < needRecv)
{
len0 = recv(s_accept, (char*)(&data) + pos, needRecv - pos, 0);
if (len0 < 0)
{
printf("Server Recieve Data Failed!\n");
return -1;
}
pos += len0;
}
count = count + data.flag;
int num1 = IMG_HEIGHT / 32 * i;
for (int j = 0; j < IMG_HEIGHT / 32; j++)
{
int num2 = j * IMG_WIDTH * 3;
uchar* ucdata = img.ptr<uchar>(j + num1);
for (int k = 0; k < IMG_WIDTH * 3; k++)
{
ucdata[k] = data.buf[num2 + k];
}
}
if (data.flag == 2)
{
if (count == 33)
{
image = img;
return 1;
count = 0;
}
else
{
count = 0;
i = 0;
}
}
}
}
return 0;
}//</uchar>
void findIP(char* ip, int size)
{
WORD v = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup(v, &wsaData); // 加载套接字库
struct hostent* phostinfo = gethostbyname("");
char* p = inet_ntoa(*((struct in_addr*)(*phostinfo->h_addr_list)));
strncpy_s(ip, size - 1, p, size - 1);
ip[size - 1] = '\0';
WSACleanup();
}
int main() {
//定义长度变量
int send_len = 0;
int recv_len = 0;
int Y;
//定义发送缓冲区和接受缓冲区
char send_buf[100];
char recv_buf[100];
char ip[20] = { 0 };
char M[2];
findIP(ip, sizeof(ip));//找自己的ip
cout << "您当前的ip地址" << ip << endl;
//定义服务端套接字,接受请求套接字
SOCKET s_server;
//服务端地址客户端地址
SOCKADDR_IN server_addr;
initialization();
//填充服务端信息
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = inet_addr("192.168.204.1");
server_addr.sin_port = htons(1234);
//创建套接字
s_server = socket(AF_INET, SOCK_STREAM, 0);
if (connect(s_server, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
cout << "服务器连接失败!" << endl;
WSACleanup();
}
else {
cout << "服务器连接成功!" << endl;
}
//发送,接收数据
cout << "视频按1聊天按2录屏按3" << endl;
while (1)
{
cin >> M;
Y = send(s_server, M, 1, 0);
if (Y > 0)
{
cout << "发送成功!" << endl;
break;
}
}
while (1)
{
if (M[0] == '1')
{
cv::Mat image;
while (1)
{
if (receive(image, s_server) > 0)
{
cv::imshow("接受到图像:", image);
cv::waitKey(3);
}
}
}
if (M[0] == '2')
{
cout << "请输入发送信息:";
cin >> send_buf;
send_len = send(s_server, send_buf, 100, 0);
if (send_len < 0) {
cout << "发送失败!" << endl;
break;
}
recv_len = recv(s_server, recv_buf, 100, 0);
if (recv_len < 0) {
cout << "接受失败!" << endl;
break;
}
else {
cout << "服务端信息:" << recv_buf << endl;
}
}
if (M[0] == '3')
{
cv::Mat image;
while (1)
{
if (receive(image, s_server) > 0)
{
cv::imshow("接受到图像:", image);
cv::waitKey(3);
}
}
}
}
//关闭套接字
closesocket(s_server);
//释放DLL资源
WSACleanup();
return 0;
}
void initialization() {
//初始化套接字库
WORD w_req = MAKEWORD(2, 2);//版本号
WSADATA wsadata;
int err;
err = WSAStartup(w_req, &wsadata);
if (err != 0) {
cout << "初始化套接字库失败!" << endl;
}
else {
cout << "初始化套接字库成功!" << endl;
}
//检测版本号
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
cout << "套接字库版本号不符!" << endl;
WSACleanup();
}
else {
cout << "套接字库版本正确!" << endl;
}
//填充服务端地址信息
}