Skip to content
Snippets Groups Projects
Commit 0b330662 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Notizen und Beispiele 9.11.2022

parent 5bdba7df
No related branches found
No related tags found
No related merge requests found
Hinweis zu Verschlüsselung: Firefox, Chrome, Safari, Opera, IE, Edge
unterstützen bei HTTP/2 keine unverschlüsselten Verbindungen, HTTP/3
ist nur für verschlüsselte Verbindungen definiert
-----------------------------------------------------------------------
streaming/usr/local/lib/pult# echo "vnc hide" | nc localhost 6076
streaming/usr/local/lib/pult# echo "vnc show" | nc localhost 6076
streaming/usr/local/lib/pult# echo "vnc max" | nc localhost 6076
streaming/usr/local/lib/pult# echo "janus-1 max" | nc localhost 6076
streaming/usr/local/lib/pult# echo "janus-1 min" | nc localhost 6076
streaming/usr/local/lib/pult#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define TARGET_HOST "ngc224.gerwinski.de"
#define PORT 80
#define COMMAND "GET /\r\n"
void error (char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (1);
}
int main (void)
{
int s;
struct sockaddr_in name;
if ((s = socket (PF_INET, SOCK_STREAM, 0)) < 0)
error ("cannot create socket");
memset (&name, 0, sizeof (name));
name.sin_family = AF_INET;
name.sin_port = htons (PORT);
name.sin_addr.s_addr = htonl (INADDR_ANY);
struct hostent *ho = gethostbyname (TARGET_HOST);
if (!ho)
{
close (s);
error ("name server lookup error");
}
if (ho->h_length > (int) sizeof (name.sin_addr))
ho->h_length = sizeof (name.sin_addr);
memcpy (&name.sin_addr, ho->h_addr, ho->h_length);
if (connect (s, (struct sockaddr *) &name, sizeof (name)) < 0)
{
close (s);
error ("cannot connect to socket");
}
send (s, COMMAND, strlen (COMMAND), 0);
ssize_t l;
do
{
char buffer[100];
l = recv (s, buffer, 100, 0);
if (l > 0)
write (1, buffer, l);
}
while (l > 0);
shutdown (s, SHUT_RDWR);
close (s);
return 0;
}
#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <array>
#include <string>
#include <iostream>
using namespace boost::asio;
using namespace boost::asio::ip;
io_service ioservice;
tcp::resolver resolv{ioservice};
tcp::socket tcp_socket{ioservice};
std::array<char, 4096> bytes;
void read_handler(const boost::system::error_code &ec,
std::size_t bytes_transferred)
{
if (!ec)
{
std::cout.write(bytes.data(), bytes_transferred);
tcp_socket.async_read_some(buffer(bytes), read_handler);
}
}
void connect_handler(const boost::system::error_code &ec)
{
if (!ec)
{
std::string r = "GET /\r\n";
write(tcp_socket, buffer(r));
tcp_socket.async_read_some(buffer(bytes), read_handler);
}
}
void resolve_handler(const boost::system::error_code &ec,
tcp::resolver::iterator it)
{
if (!ec)
tcp_socket.async_connect(*it, connect_handler);
}
int main()
{
tcp::resolver::query q{"ngc224.gerwinski.de", "80"};
resolv.async_resolve(q, resolve_handler);
ioservice.run();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 1234
#define MESSAGE "Hello, world!\n"
void error (char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (1);
}
int main (void)
{
int s;
struct sockaddr_in name;
if ((s = socket (PF_INET, SOCK_STREAM, 0)) < 0)
error ("cannot create socket");
memset (&name, 0, sizeof (name));
name.sin_family = AF_INET;
name.sin_port = htons (PORT);
name.sin_addr.s_addr = htonl (INADDR_ANY);
int on = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof (on));
if (bind (s, (struct sockaddr *) &name, sizeof (name)) < 0)
{
close (s);
error ("cannot bind socket");
}
if (listen (s, 16) < 0)
{
close (s);
error ("cannot listen on socket");
}
struct sockaddr_in clientname;
socklen_t size = sizeof (clientname);
s = accept (s, (struct sockaddr *) &clientname, &size);
if (s < 0)
error ("cannot accept connection");
char *host_address = inet_ntoa (clientname.sin_addr);
char *host_name;
struct hostent *hp = gethostbyaddr ((void *) &clientname.sin_addr, sizeof (clientname.sin_addr), clientname.sin_family);
if (hp)
host_name = hp->h_name;
else
host_name = inet_ntoa (clientname.sin_addr);
int remote_port = ntohs (clientname.sin_port);
printf ("connection from %s [%s], port %d\n",
host_name, host_address, remote_port);
send (s, MESSAGE, strlen (MESSAGE), 0);
shutdown (s, SHUT_RDWR);
close (s);
return 0;
}
#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <string>
using namespace boost::asio;
using namespace boost::asio::ip;
io_service ioservice;
tcp::endpoint tcp_endpoint{tcp::v4(), 1234};
tcp::acceptor tcp_acceptor{ioservice, tcp_endpoint};
tcp::socket tcp_socket{ioservice};
std::string data;
void write_handler(const boost::system::error_code &ec,
std::size_t bytes_transferred)
{
if (!ec)
tcp_socket.shutdown(tcp::socket::shutdown_send);
}
void accept_handler(const boost::system::error_code &ec)
{
if (!ec)
{
std::string data = "Hello, world!\n";
async_write(tcp_socket, buffer(data), write_handler);
}
}
int main()
{
tcp_acceptor.listen();
tcp_acceptor.async_accept(tcp_socket, accept_handler);
ioservice.run();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment