Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp1-ProgDev-mertens-guilherme
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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mertens De Andrade Guilherme
tp1-ProgDev-mertens-guilherme
Commits
b81c93cf
Commit
b81c93cf
authored
5 months ago
by
Mertens De Andrade Guilherme
Browse files
Options
Downloads
Patches
Plain Diff
client with socket
parent
ce3bbe8b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
client_6.c
+45
-0
45 additions, 0 deletions
client_6.c
with
45 additions
and
0 deletions
client_6.c
0 → 100644
+
45
−
0
View file @
b81c93cf
#include
<iostream>
#include
<cstdlib>
#include
<cstring>
#include
<sys/socket.h>
#include
<arpa/inet.h>
#include
<unistd.h>
#define PORT 8080
int
main
()
{
int
sock
=
0
;
struct
sockaddr_in
serv_addr
;
int
random_number
;
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
<
0
)
{
std
::
cerr
<<
"Socket creation error"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_port
=
htons
(
PORT
);
if
(
inet_pton
(
AF_INET
,
"127.0.0.1"
,
&
serv_addr
.
sin_addr
)
<=
0
)
{
std
::
cerr
<<
"Wrong address"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
std
::
cerr
<<
"Connection failed"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
while
(
true
)
{
if
(
read
(
sock
,
&
random_number
,
sizeof
(
random_number
))
>
0
)
{
std
::
cout
<<
"Received: "
<<
random_number
<<
std
::
endl
;
}
else
{
std
::
cerr
<<
"Failed to read from server "
<<
std
::
endl
;
close
(
sock
);
exit
(
EXIT_FAILURE
);
}
}
close
(
sock
);
return
0
;
}
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