mirror of
https://github.com/Noratrieb/survey.git
synced 2026-01-14 08:30:12 +01:00
handle connection
This commit is contained in:
parent
e5484affdf
commit
43df60f510
1 changed files with 15 additions and 7 deletions
|
|
@ -1,10 +1,18 @@
|
|||
use std::mem::MaybeUninit;
|
||||
use std::{io, mem};
|
||||
|
||||
const PORT: libc::in_port_t = 1234;
|
||||
const PORT: libc::in_port_t = 1112;
|
||||
|
||||
const SOCKADDR_IN_SIZE: libc::socklen_t = mem::size_of::<libc::sockaddr_in>() as _;
|
||||
|
||||
macro_rules! check_zero {
|
||||
($result:expr) => {
|
||||
if $result != 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn listener() -> io::Result<()> {
|
||||
unsafe {
|
||||
let socket = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0);
|
||||
|
|
@ -17,7 +25,7 @@ pub fn listener() -> io::Result<()> {
|
|||
|
||||
let addr = libc::sockaddr_in {
|
||||
sin_family: libc::AF_INET.try_into().unwrap(),
|
||||
sin_port: PORT,
|
||||
sin_port: PORT.to_be(),
|
||||
sin_addr: libc::in_addr {
|
||||
s_addr: libc::INADDR_ANY,
|
||||
},
|
||||
|
|
@ -32,21 +40,21 @@ pub fn listener() -> io::Result<()> {
|
|||
|
||||
println!("Bound socket ({socket}) on port {PORT}");
|
||||
|
||||
let result = libc::listen(socket, 5);
|
||||
if result != 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
check_zero!(libc::listen(socket, 5));
|
||||
|
||||
println!("Listening on socket ({socket})");
|
||||
|
||||
let mut peer_sockaddr = MaybeUninit::uninit();
|
||||
let mut sockaddr_size = 0;
|
||||
let connection = libc::accept(socket, peer_sockaddr.as_mut_ptr(), &mut sockaddr_size);
|
||||
if connection < 0 {
|
||||
if connection == -1 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
|
||||
println!("Received connection! (connfd={connection})");
|
||||
|
||||
check_zero!(libc::close(connection));
|
||||
check_zero!(libc::close(socket));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue