Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp30023-2018-project-1
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
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Neeserg Parajuli
comp30023-2018-project-1
Commits
73c3732e
Commit
73c3732e
authored
7 years ago
by
Neeserg Parajuli
Browse files
Options
Downloads
Patches
Plain Diff
A simple multihtreaded server sending hello world
parent
2c0fed23
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server
+0
-0
0 additions, 0 deletions
server
server.c
+80
-19
80 additions, 19 deletions
server.c
with
80 additions
and
19 deletions
server
+
0
−
0
View file @
73c3732e
No preview for this file type
This diff is collapsed.
Click to expand it.
server.c
+
80
−
19
View file @
73c3732e
...
@@ -7,10 +7,17 @@
...
@@ -7,10 +7,17 @@
#include
<netdb.h>
#include
<netdb.h>
#include
<netinet/in.h>
#include
<netinet/in.h>
#include
<unistd.h>
#include
<unistd.h>
# include <pthread.h>
#define MAXUSER 5
void
*
handleclient
(
void
*
newsockfd
);
int
main
(
int
argc
,
char
*
argv
[]){
int
main
(
int
argc
,
char
*
argv
[]){
struct
addrinfo
info
,
*
res
,
*
p
;
struct
addrinfo
info
,
*
res
,
*
p
;
int
status
;
int
sockfd
,
newsockfd
;
struct
sockaddr_storage
clien_addr
;
socklen_t
clien_size
;
char
ipstr
[
INET6_ADDRSTRLEN
];
char
ipstr
[
INET6_ADDRSTRLEN
];
if
(
argc
<
2
){
if
(
argc
<
2
){
...
@@ -19,40 +26,94 @@ int main(int argc, char *argv[]){
...
@@ -19,40 +26,94 @@ int main(int argc, char *argv[]){
}
}
memset
(
&
info
,
0
,
sizeof
(
info
));
memset
(
&
info
,
0
,
sizeof
(
info
));
info
.
ai_family
=
AF_UNSPEC
;
///sets it to
IPv4
info
.
ai_family
=
AF_UNSPEC
;
///sets it to
unspecified
info
.
ai_socktype
=
SOCK_STREAM
;
//TCP connection stream oriented
info
.
ai_socktype
=
SOCK_STREAM
;
//TCP connection stream oriented
info
.
ai_flags
=
AI_PASSIVE
;
//use my current IP adress
info
.
ai_flags
=
AI_PASSIVE
;
//listen on on all ip interface of machine
info
.
ai_protocol
=
0
;
//set this to zero as default
if
((
status
=
getaddrinfo
(
NULL
,
argv
[
1
],
&
info
,
&
res
)
!=
0
)){
if
((
getaddrinfo
(
NULL
,
argv
[
1
],
&
info
,
&
res
)
!=
0
)){
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if getaddrinfo gets any addrinfos
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if getaddrinfo gets any addrinfos
exit
(
1
);
exit
(
1
);
}
}
for
(
p
=
res
;
p
!=
NULL
;
p
=
p
->
ai_next
)
for
(
p
=
res
;
p
!=
NULL
;
p
=
p
->
ai_next
)
{
{
void
*
addr
;
if
((
sockfd
=
socket
(
p
->
ai_family
,
p
->
ai_socktype
,
p
->
ai_protocol
))
<
0
){
char
*
ipver
;
perror
(
"ERROR opening socket"
);
continue
;
}
if
((
bind
(
sockfd
,
p
->
ai_addr
,
p
->
ai_addrlen
))
<
0
){
perror
(
"ERROR on binding"
);
continue
;
}
break
;
}
if
(
p
==
NULL
)
{
perror
(
"ERROR on creating and binding"
);
return
2
;
}
if
(
p
->
ai_family
==
AF_INET
)
{
// IPv4
if
(
listen
(
sockfd
,
MAXUSER
)
<
0
){
struct
sockaddr_in
*
ipv4
=
(
struct
sockaddr_in
*
)
p
->
ai_addr
;
perror
(
"ERROR listening"
);
addr
=
&
(
ipv4
->
sin_addr
);
return
2
;
ipver
=
"IPv4"
;
}
else
{
// IPv6
struct
sockaddr_in6
*
ipv6
=
(
struct
sockaddr_in6
*
)
p
->
ai_addr
;
addr
=
&
(
ipv6
->
sin6_addr
);
ipver
=
"IPv6"
;
}
}
inet_ntop
(
p
->
ai_family
,
addr
,
ipstr
,
sizeof
ipstr
);
printf
(
" %s: %s
\n
"
,
ipver
,
ipstr
);
while
(
1
){
clien_size
=
sizeof
(
clien_addr
);
if
((
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
clien_addr
,
&
clien_size
))
<
0
){
perror
(
"ERROR accepting"
);
return
3
;
}
pthread_t
tid
;
if
(
pthread_create
(
&
tid
,
NULL
,
handleclient
,
&
newsockfd
)
<
0
)
{
perror
(
"ERROR creating thread"
);
return
3
;
}
pthread_detach
(
tid
);
}
}
freeaddrinfo
(
res
);
return
0
;
return
0
;
}
}
void
*
handleclient
(
void
*
newsockfd
){
int
*
sockfd_ptr
=
(
int
*
)
newsockfd
;
int
numbytes
;
int
sockfd
=
*
sockfd_ptr
;
char
buffer
[
256
];
bzero
(
buffer
,
256
);
if
((
numbytes
=
recv
(
sockfd
,
buffer
,
255
,
0
))
<
0
)
{
perror
(
"ERROR recieving"
);
return
NULL
;
}
printf
(
"%s
\n
"
,
buffer
);
if
(
send
(
sockfd
,
"hello world"
,
13
,
0
)
<
0
)
{
perror
(
"ERROR sending"
);
return
NULL
;
}
close
(
sockfd
);
}
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