Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
net-lab-assignments
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
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
20041679 .
net-lab-assignments
Commits
ce303937
Commit
ce303937
authored
9 months ago
by
20041679
Browse files
Options
Downloads
Patches
Plain Diff
echo server advanced
parent
c16f267b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
echoServerAdvanced/client.c
+49
-39
49 additions, 39 deletions
echoServerAdvanced/client.c
echoServerAdvanced/server.c
+40
-6
40 additions, 6 deletions
echoServerAdvanced/server.c
with
89 additions
and
45 deletions
echoServerAdvanced/client.c
+
49
−
39
View file @
ce303937
...
...
@@ -21,57 +21,67 @@ int main(int argc, char *argv[]) {
exit
(
1
);
}
while
(
1
)
{
/* create a streaming socket */
simpleSocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
/* create a streaming socket */
simpleSocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
if
(
simpleSocket
==
-
1
)
{
if
(
simpleSocket
==
-
1
)
{
fprintf
(
stderr
,
"Could not create a socket!
\n
"
);
exit
(
1
);
fprintf
(
stderr
,
"Could not create a socket!
\n
"
);
exit
(
1
);
}
else
{
fprintf
(
stderr
,
"Socket created!
\n
"
);
}
}
else
{
fprintf
(
stderr
,
"Socket created!
\n
"
);
}
/* retrieve the port number for connecting */
simplePort
=
atoi
(
argv
[
2
]);
/* retrieve the port number for connecting */
simplePort
=
atoi
(
argv
[
2
]);
/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset
(
&
simpleServer
,
'\0'
,
sizeof
(
simpleServer
));
simpleServer
.
sin_family
=
AF_INET
;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
1
]);
simpleServer
.
sin_port
=
htons
(
simplePort
);
/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset
(
&
simpleServer
,
'\0'
,
sizeof
(
simpleServer
));
simpleServer
.
sin_family
=
AF_INET
;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
1
]);
simpleServer
.
sin_port
=
htons
(
simplePort
);
/* connect to the address and port with our socket */
returnStatus
=
connect
(
simpleSocket
,
(
struct
sockaddr
*
)
&
simpleServer
,
sizeof
(
simpleServer
));
/* connect to the address and port with our socket */
returnStatus
=
connect
(
simpleSocket
,
(
struct
sockaddr
*
)
&
simpleServer
,
sizeof
(
simpleServer
));
if
(
returnStatus
==
0
)
{
fprintf
(
stderr
,
"Connect successful!
\n
"
);
}
else
{
fprintf
(
stderr
,
"Could not connect to address!
\n
"
);
close
(
simpleSocket
);
exit
(
1
);
}
if
(
returnStatus
==
0
)
{
fprintf
(
stderr
,
"Connect successful!
\n
"
);
}
else
{
fprintf
(
stderr
,
"Could not connect to address!
\n
"
);
close
(
simpleSocket
);
exit
(
1
);
}
puts
(
"numero interazioni"
);
char
buffer
[
256
];
fgets
(
buffer
,
ARRAY_SIZE
(
buffer
),
stdin
);
write
(
simpleSocket
,
buffer
,
strlen
(
buffer
));
puts
(
"numero interazioni"
);
char
buffer
[
256
];
fgets
(
buffer
,
ARRAY_SIZE
(
buffer
),
stdin
);
write
(
simpleSocket
,
buffer
,
strlen
(
buffer
));
returnStatus
=
read
(
simpleSocket
,
buffer
,
sizeof
(
buffer
));
// no error checking on client
long
n_iterations
=
atol
(
buffer
);
for
(
long
i
=
0
;
i
<
n_iterations
;
++
i
)
{
fgets
(
buffer
,
ARRAY_SIZE
(
buffer
),
stdin
);
if
(
write
(
simpleSocket
,
buffer
,
strlen
(
buffer
))
<
0
)
{
break
;
}
ssize_t
returnStatus
=
read
(
simpleSocket
,
buffer
,
sizeof
(
buffer
));
if
(
returnStatus
<
0
)
{
break
;
}
if
(
returnStatus
>
0
)
{
buffer
[
ARRAY_SIZE
(
buffer
)
-
1
]
=
'\0'
;
p
rintf
(
"%s"
,
buffer
);
buffer
[
returnStatus
-
1
]
=
'\0'
;
p
uts
(
buffer
);
}
close
(
simpleSocket
);
}
close
(
simpleSocket
);
return
0
;
}
This diff is collapsed.
Click to expand it.
echoServerAdvanced/server.c
+
40
−
6
View file @
ce303937
...
...
@@ -10,6 +10,19 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
int
parse_long
(
char
*
s
,
long
*
ret
)
{
if
(
!
ret
)
{
return
-
1
;
}
char
*
end
=
s
;
long
valid
=
strtol
(
s
,
&
end
,
10
);
if
(
!
end
)
{
return
-
1
;
}
*
ret
=
valid
;
return
0
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
simpleSocket
=
0
;
int
simplePort
=
0
;
...
...
@@ -78,16 +91,37 @@ int main(int argc, char *argv[]) {
puts
(
"connection accepted"
);
char
buffer
[
256
];
// reading n_iterations
ssize_t
returnStatus
=
read
(
simpleChildSocket
,
buffer
,
sizeof
(
buffer
));
if
(
returnStatus
<
0
)
{
continue
;
if
(
returnStatus
<
=
0
)
{
goto
CLOSE
;
}
if
(
returnStatus
>
0
)
{
buffer
[
ARRAY_SIZE
(
buffer
)
-
1
]
=
'\0'
;
printf
(
"read %s"
,
buffer
);
buffer
[
returnStatus
-
1
]
=
'\0'
;
printf
(
"read %s
\n
"
,
buffer
);
long
n_iterations
;
if
(
parse_long
(
buffer
,
&
n_iterations
)
<
0
)
{
puts
(
"invalid number"
);
goto
CLOSE
;
}
write
(
simpleChildSocket
,
buffer
,
returnStatus
);
for
(
long
i
=
0
;
i
<
n_iterations
;
++
i
)
{
ssize_t
returnStatus
=
read
(
simpleChildSocket
,
buffer
,
sizeof
(
buffer
));
if
(
returnStatus
<
0
)
{
break
;
}
printf
(
"read "
);
if
(
returnStatus
>
0
)
{
buffer
[
returnStatus
-
1
]
=
'\0'
;
printf
(
"%s
\n
"
,
buffer
);
}
else
{
puts
(
"nothing"
);
}
if
(
write
(
simpleChildSocket
,
buffer
,
returnStatus
)
<
0
)
{
break
;
}
}
CLOSE:
close
(
simpleChildSocket
);
}
...
...
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