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

Lehrmaterialien und Beispiele 22.11.2018

parent 4e864e93
No related branches found
No related tags found
No related merge requests found
Showing
with 1061 additions and 0 deletions
File added
File added
File added
File added
../common/Zeichen_123.pdf
\ No newline at end of file
File added
This diff is collapsed.
../common/kompass-messung.png
\ No newline at end of file
../common/kompassmodul-an-roboter.jpg
\ No newline at end of file
../common/kompassmodul.jpg
\ No newline at end of file
../common/logo-hochschule-bochum-cvh-text-v2.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
../common/motherboard-anschluesse.jpg
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
File added
#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 /\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();
}
...@@ -21,6 +21,7 @@ Vortragsfolien: ...@@ -21,6 +21,7 @@ Vortragsfolien:
* [18.10.2018: Unix: Umgang mit Dateisystemen](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181018/es-20181018.pdf) * [18.10.2018: Unix: Umgang mit Dateisystemen](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181018/es-20181018.pdf)
* [25.10.2018: Unix: grep, Ein- und Ausgabeströme, Pipes, Verzweigungen und Schleifen](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181025/es-20181025.pdf) * [25.10.2018: Unix: grep, Ein- und Ausgabeströme, Pipes, Verzweigungen und Schleifen](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181025/es-20181025.pdf)
* [08.11.2018: TCP/IP in der Praxis: Netzzugang, IP-Adressen, Ports, Protokolle](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181108/es-20181108.pdf) * [08.11.2018: TCP/IP in der Praxis: Netzzugang, IP-Adressen, Ports, Protokolle](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181108/es-20181108.pdf)
* [22.11.2018: TCP/IP in der Praxis: SSH, X11, GNU screen, Programmierung; Bus-Systeme](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/20181122/es-20181122.pdf)
* [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/es-slides-2018ws.pdf) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/es/raw/master/es-slides-2018ws.pdf)
Tafelbilder: Tafelbilder:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment