Sponge
CS144's user-space TCP library
doctests
socket_example_3.cc
Go to the documentation of this file.
1
// create a pair of stream sockets
2
std::array<int, 2>
fds
{};
3
SystemCall
(
"socketpair"
, ::
socketpair
(AF_UNIX, SOCK_STREAM, 0,
fds
.data()));
4
LocalStreamSocket
pipe1
{
FileDescriptor
(
fds
[0])},
pipe2
{
FileDescriptor
(
fds
[1])};
5
6
pipe1
.
write
(
"hi there"
);
7
auto
recvd
=
pipe2
.
read
();
8
9
pipe2
.
write
(
"hi yourself"
);
10
auto
recvd2
=
pipe1
.
read
();
11
12
if
(
recvd
!=
"hi there"
||
recvd2
!=
"hi yourself"
) {
13
throw
std::runtime_error
(
"wrong data received"
);
14
}
FileDescriptor::write
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
Definition:
file_descriptor.hh:65
recvd2
auto recvd2
Definition:
socket_example_3.cc:10
SystemCall
SystemCall("socketpair", ::socketpair(AF_UNIX, SOCK_STREAM, 0, fds.data()))
FileDescriptor
A reference-counted handle to a file descriptor.
Definition:
file_descriptor.hh:12
FileDescriptor::read
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
pipe2
LocalStreamSocket pipe2
Definition:
socket_example_3.cc:4
fds
std::array< int, 2 > fds
Definition:
socket_example_3.cc:2
man2::socketpair
socketpair
pipe1
LocalStreamSocket pipe1
Definition:
socket_example_3.cc:4
LocalStreamSocket
A wrapper around Unix-domain stream sockets.
Definition:
socket.hh:113
std::array
std::runtime_error
recvd
auto recvd
Definition:
socket_example_3.cc:7
Generated by
1.8.17