first attempt at networking (does not work)

This commit is contained in:
2025-03-26 15:28:16 -05:00
parent a134513b9c
commit c85581749b
5 changed files with 126 additions and 31 deletions

View File

@@ -1,20 +1,55 @@
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/core/mat.hpp>
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <packets/ImagePacket.h>
using namespace std;
int main() {
// TODO: read image data from socket instead of VideoCapture
cv::VideoCapture cap = cv::VideoCapture(0);
bool running = true;
// creating socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
// specifying the address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;
// binding socket.
bind(serverSocket, (struct sockaddr*)&serverAddress,
sizeof(serverAddress));
// listening to the assigned socket
listen(serverSocket, 5);
// accepting connection request
int clientSocket
= accept(serverSocket, nullptr, nullptr);
// TODO: handle multiple images
cv::Mat image = cv::Mat::zeros(480, 640, CV_8UC3);
// setup packet buffer
char buffer[IMAGEPACKET_SIZE] = { 0 };
ImagePacket packet = ImagePacket(&image, 0);
bool running = true;
// TODO: make this asynchronous. probably do that in tandem with setting up networking
while (running) {
cap.read(image);
// receive data
recv(clientSocket, buffer, sizeof(buffer), 0);
packet.deserialize(buffer);
cout << "received packet with index " << packet.getBegin() << endl;
packet.apply(&image);
imshow("image", image);
running = cv::waitKey(30) != 27;
}
close(serverSocket);
return 0;
}
}