Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
c-stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Cowan
c-stuff
Commits
7f8af790
Commit
7f8af790
authored
5 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Add POSIX UDP server example
parent
8a292082
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2333
passed
5 years ago
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
meson.build
+4
-0
4 additions, 0 deletions
meson.build
posix/meson.build
+1
-0
1 addition, 0 deletions
posix/meson.build
posix/udp-server.c
+55
-0
55 additions, 0 deletions
posix/udp-server.c
with
60 additions
and
0 deletions
meson.build
+
4
−
0
View file @
7f8af790
...
...
@@ -43,3 +43,7 @@ endif
if
thread_dep
.
found
()
subdir
(
'pthread'
)
endif
if
host_machine
.
system
()
!=
'windows'
subdir
(
'posix'
)
endif
This diff is collapsed.
Click to expand it.
posix/meson.build
0 → 100644
+
1
−
0
View file @
7f8af790
executable
(
'udp-server'
,
'udp-server.c'
)
This diff is collapsed.
Click to expand it.
posix/udp-server.c
0 → 100644
+
55
−
0
View file @
7f8af790
#include
<stdbool.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<arpa/inet.h>
#include
<netinet/in.h>
#include
<sys/socket.h>
#include
<unistd.h>
#define PORT 46123
#define BUF_LENGTH 1024
int
main
(
void
)
{
int
sock
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
sock
==
-
1
)
{
perror
(
"socket"
);
exit
(
EXIT_FAILURE
);
}
struct
sockaddr_in
addr
;
memset
(
&
addr
,
0
,
sizeof
(
struct
sockaddr_in
));
addr
.
sin_family
=
AF_INET
;
addr
.
sin_port
=
htons
(
PORT
);
addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
if
(
bind
(
sock
,
(
struct
sockaddr
*
)
&
addr
,
sizeof
(
addr
))
==
-
1
)
{
perror
(
"bind"
);
close
(
sock
);
exit
(
EXIT_FAILURE
);
}
while
(
true
)
{
char
buf
[
BUF_LENGTH
];
struct
sockaddr_in
src_addr
;
socklen_t
addrlen
=
sizeof
(
struct
sockaddr_in
);
char
str
[
INET_ADDRSTRLEN
];
ssize_t
bytes
=
recvfrom
(
sock
,
buf
,
BUF_LENGTH
,
0
,
(
struct
sockaddr
*
)
&
src_addr
,
&
addrlen
);
if
(
inet_ntop
(
AF_INET
,
&
src_addr
.
sin_addr
,
str
,
INET_ADDRSTRLEN
)
==
NULL
)
{
fprintf
(
stderr
,
"Could not convert address to string
\n
"
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Received %zd bytes from %s
\n
"
,
bytes
,
str
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment