wait for client

This commit is contained in:
ktyl 2023-02-14 23:30:29 +00:00
parent 5920133571
commit f9aef71a94
2 changed files with 76 additions and 14 deletions

View File

@ -8,6 +8,13 @@
#include <iostream> #include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
const double ASPECT_RATIO = 16.0 / 9.0; const double ASPECT_RATIO = 16.0 / 9.0;
const int WIDTH = 1920; const int WIDTH = 1920;
const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO); const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO);
@ -112,8 +119,62 @@ hittable_list random_scene()
return world; return world;
} }
void error(const char* message)
{
perror(message);
exit(1);
}
void wait_for_client()
{
int sockfd, newsockfd;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
// open socket and await connection from client
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR creating socket");
}
bzero((char*)&serv_addr, sizeof(serv_addr));
// we successfully created the socket, configure it for binding
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(64999); // convert number from host to network byte order
// bind the socket
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR binding socket");
}
// successfully bound socket, start listening for connections
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
if (newsockfd < 0)
{
error("ERROR accepting client");
}
printf("got a connection!\n");
// close sockets we opened
close(newsockfd);
close(sockfd);
// once connection is acquired, exit
exit(0);
}
int main() int main()
{ {
wait_for_client();
std::cout << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n"; std::cout << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n";
hittable_list world = random_scene(); hittable_list world = random_scene();

View File

@ -2,19 +2,20 @@ fantasy telescope idea
use rti1w as a base use rti1w as a base
server waits for connection * [ ] move rendering out of main.cpp
client establishes connection * [x] server waits for connection
client sends receiving port to server * [x] client establishes connection
client application sends command to send image * [ ] client sends receiving port to server
server receives imaging command * [ ] client application sends command to send image
server renders image * [ ] server receives imaging command
server sends image data stream to client * [ ] server renders image
client application receives image * [ ] server sends image data stream to client
client requests resend of lost packets * [ ] client application receives image
client displays image * [ ] client requests resend of lost packets
client saves image to file * [ ] client displays image
server sends telemetry to client (what telemetry?) * [ ] client saves image to file
server compresses image before sending to client * [ ] server sends telemetry to client (what telemetry?)
client specifies image parameters * [ ] server compresses image before sending to client
* [ ] client specifies image parameters