Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
asn1Demo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王思远
asn1Demo
Commits
b6f96457
Commit
b6f96457
authored
Nov 25, 2025
by
王思远
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
UTC:2025-11-25 8:46:57 :
parent
43896687
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
18 deletions
+39
-18
main.cpp
source/main.cpp
+39
-18
No files found.
source/main.cpp
View file @
b6f96457
...
...
@@ -343,19 +343,19 @@ int main(int argc, char *argv[])
::
std
::
uint16_t
listenPort
=
static_cast
<::
std
::
uint16_t
>
(
::
std
::
strtoul
(
argv
[
4
],
nullptr
,
10
));
// Create socket.
int
f
d
=
::
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
f
d
<
0
)
{
// Create
receive
socket.
int
recvF
d
=
::
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
recvF
d
<
0
)
{
int
err
=
errno
;
::
std
::
cerr
<<
"Failed to create socket: "
<<
::
strerror
(
err
)
<<
::
std
::
endl
;
return
-
1
;
}
::
std
::
unique_ptr
<
int
,
void
(
*
)(
int
*
)
>
autoClose
Fd
(
&
fd
,
[](
int
*
fd
)
->
void
{
::
close
(
*
fd
);
*
fd
=
-
1
;
});
::
std
::
unique_ptr
<
int
,
void
(
*
)(
int
*
)
>
autoClose
RecvFd
(
&
recvFd
,
[](
int
*
fd
)
->
void
{
::
close
(
*
fd
);
*
fd
=
-
1
;
});
// Bind.
::
sockaddr_in
bindAddress
;
...
...
@@ -366,8 +366,9 @@ int main(int argc, char *argv[])
return
-
1
;
}
bindAddress
.
sin_port
=
htons
(
listenPort
);
auto
bindResult
=
::
bind
(
fd
,
reinterpret_cast
<::
sockaddr
*>
(
&
bindAddress
),
sizeof
(
bindAddress
));
auto
bindResult
=
::
bind
(
recvFd
,
reinterpret_cast
<::
sockaddr
*>
(
&
bindAddress
),
sizeof
(
bindAddress
));
if
(
bindResult
<
0
)
{
int
err
=
errno
;
::
std
::
cerr
<<
"Failed to bind listen port: "
<<
::
strerror
(
err
)
...
...
@@ -375,6 +376,26 @@ int main(int argc, char *argv[])
return
-
1
;
}
// Reuse.
int
opt
=
1
;
::
setsockopt
(
recvFd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
opt
,
sizeof
(
opt
));
opt
=
1
;
::
setsockopt
(
recvFd
,
SOL_SOCKET
,
SO_REUSEPORT
,
&
opt
,
sizeof
(
opt
));
// Create send socket.
int
sendFd
=
::
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
sendFd
<
0
)
{
int
err
=
errno
;
::
std
::
cerr
<<
"Failed to create socket: "
<<
::
strerror
(
err
)
<<
::
std
::
endl
;
return
-
1
;
}
::
std
::
unique_ptr
<
int
,
void
(
*
)(
int
*
)
>
autoCloseSendFd
(
&
sendFd
,
[](
int
*
fd
)
->
void
{
::
close
(
*
fd
);
*
fd
=
-
1
;
});
// Make send address.
::
sockaddr_in
targetAddress
;
::
memset
(
&
targetAddress
,
0
,
sizeof
(
targetAddress
));
...
...
@@ -385,7 +406,7 @@ int main(int argc, char *argv[])
}
targetAddress
.
sin_port
=
htons
(
targetPort
);
auto
connectResult
=
::
connect
(
f
d
,
reinterpret_cast
<::
sockaddr
*>
(
&
targetAddress
),
=
::
connect
(
sendF
d
,
reinterpret_cast
<::
sockaddr
*>
(
&
targetAddress
),
sizeof
(
targetAddress
));
if
(
connectResult
<
0
)
{
int
err
=
errno
;
...
...
@@ -395,10 +416,10 @@ int main(int argc, char *argv[])
}
// Reuse.
int
opt
=
1
;
::
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
opt
,
sizeof
(
opt
));
opt
=
1
;
::
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEPORT
,
&
opt
,
sizeof
(
opt
));
::
setsockopt
(
sendFd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
opt
,
sizeof
(
opt
));
opt
=
1
;
::
setsockopt
(
sendFd
,
SOL_SOCKET
,
SO_REUSEPORT
,
&
opt
,
sizeof
(
opt
));
// Create quit event.
static
int
quitEvent
=
-
1
;
...
...
@@ -430,7 +451,7 @@ int main(int argc, char *argv[])
auto
now
=
::
std
::
chrono
::
steady_clock
::
now
();
if
(
now
-
nextTimestamp
>
::
std
::
chrono
::
seconds
(
1
))
{
// Send.
writeData
(
f
d
);
writeData
(
sendF
d
);
nextTimestamp
+=
::
std
::
chrono
::
seconds
(
1
);
if
(
now
>
nextTimestamp
)
{
nextTimestamp
=
now
+
::
std
::
chrono
::
seconds
(
1
);
...
...
@@ -439,7 +460,7 @@ int main(int argc, char *argv[])
// Wait for data.
::
std
::
array
<::
pollfd
,
2
>
fds
{
::
pollfd
{.
fd
=
f
d
,
.
events
=
POLLIN
,
.
revents
=
0
},
::
pollfd
{.
fd
=
recvF
d
,
.
events
=
POLLIN
,
.
revents
=
0
},
::
pollfd
{.
fd
=
quitEvent
,
.
events
=
POLLIN
,
.
revents
=
0
}};
now
=
::
std
::
chrono
::
steady_clock
::
now
();
auto
pollResult
=
::
poll
(
...
...
@@ -452,8 +473,8 @@ int main(int argc, char *argv[])
if
(
pollResult
>
0
)
{
for
(
auto
&
status
:
fds
)
{
if
(
status
.
revents
&
POLLIN
)
{
if
(
status
.
fd
==
f
d
)
{
::
readData
(
f
d
);
if
(
status
.
fd
==
recvF
d
)
{
::
readData
(
recvF
d
);
}
else
if
(
status
.
fd
==
quitEvent
)
{
::
std
::
cout
<<
"Quit event received."
<<
::
std
::
endl
;
return
0
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment