feat: Add udp stream server
This commit is contained in:
parent
ff8754ad37
commit
92f2529ec3
51
server.cc
51
server.cc
@ -1,12 +1,27 @@
|
||||
#include <cstring>
|
||||
// #include <stdio.h>
|
||||
// #include <cstring>
|
||||
// #include <sys/types.h>
|
||||
// #include <sys/socket.h>
|
||||
// #include <netdb.h>
|
||||
// #include <arpa/inet.h>
|
||||
// #include <netinet/in.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
namespace net
|
||||
{
|
||||
|
||||
class TCPServer
|
||||
class Server
|
||||
{
|
||||
private:
|
||||
int status;
|
||||
@ -14,30 +29,38 @@ private:
|
||||
struct addrinfo *servinfo; // Points to results
|
||||
|
||||
public:
|
||||
TCPServer(TCPServer&) = delete;
|
||||
~TCPServer()
|
||||
Server(Server&) = delete;
|
||||
Server()
|
||||
{
|
||||
this->stop();
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
memset(&hints, 0, sizeof hints);
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
if(!is_valid_struct(&hints, &servinfo))
|
||||
{
|
||||
|
||||
}
|
||||
// if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0)
|
||||
// {
|
||||
// fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// if(!is_valid_struct(&hints, &servinfo))
|
||||
// {
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
bool is_valid_struct(addrinfo hints, addrinfo servinfo)
|
||||
{
|
||||
uint8_t result = getaddrinfo(NULL, "1337", &hints, &servinfo);
|
||||
return
|
||||
// bool is_valid_struct(addrinfo hints, addrinfo servinfo)
|
||||
// {
|
||||
// uint8_t result = getaddrinfo(NULL, "1337", &hints, &servinfo);
|
||||
// return
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
|
||||
void stop()
|
||||
|
69
showip.cc
Normal file
69
showip.cc
Normal file
@ -0,0 +1,69 @@
|
||||
// #include <stdio.h>
|
||||
// #include <cstring>
|
||||
// #include <sys/types.h>
|
||||
// #include <sys/socket.h>
|
||||
// #include <netdb.h>
|
||||
// #include <arpa/inet.h>
|
||||
// #include <netinet/in.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct addrinfo hints, *res, *p;
|
||||
int status;
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "usage: showip hostname\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0)
|
||||
{
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
||||
return 2;
|
||||
}
|
||||
|
||||
printf("IP addresses for %s: \n\n", argv[1]);
|
||||
|
||||
for (p = res; p != nullptr; p = p->ai_next)
|
||||
{
|
||||
void *addr;
|
||||
char *ipver;
|
||||
|
||||
// get the pointer to the address
|
||||
if (p->ai_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *ipv4 = (struct sockaddr_in*)p->ai_addr;
|
||||
addr = &(ipv4->sin_addr);
|
||||
ipver = "IPv4";
|
||||
} else
|
||||
{
|
||||
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)p->ai_addr;
|
||||
addr = &(ipv6->sin6_addr);
|
||||
ipver = "IPv6";
|
||||
}
|
||||
inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
|
||||
printf(" %s: %s\n", ipver, ipstr);
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
|
||||
return 0;
|
||||
}
|
83
stream_server.cc
Normal file
83
stream_server.cc
Normal file
@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define PORT "1337"
|
||||
|
||||
#define BACKLOG 10 // pending connection queue
|
||||
|
||||
void sigchld_handler(int s)
|
||||
{
|
||||
int saved_errno = errno;
|
||||
|
||||
while (waitpid(-1, NULL, WNOHANG) > 0);
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
void *get_in_addr(struct sockaddr *sa)
|
||||
{
|
||||
if (sa->sa_family == AF_INET)
|
||||
{
|
||||
return &(((struct sockaddr_in*)sa)->sin_addr);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int sockfd, new_fd;
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
struct sockaddr_storage their_addr;
|
||||
socklen_t sin_size;
|
||||
struct sigaction sa;
|
||||
int yes = 1;
|
||||
char s[INET6_ADDRSTRLEN];
|
||||
int rv;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0)
|
||||
{
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Loop through all results until we bind
|
||||
for (p = servinfo; p != NULL; p = p->ai_next)
|
||||
{
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
|
||||
{
|
||||
perror("server: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
|
||||
{
|
||||
perror("setsockopt");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
|
||||
{
|
||||
close(sockfd);
|
||||
perror("server: bind");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user