Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cs283
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
Joey Le
Cs283
Commits
9ebef86a
Commit
9ebef86a
authored
2 months ago
by
Joey Le
Browse files
Options
Downloads
Patches
Plain Diff
WHOLE LOT OF DEBUG
parent
9c50e99c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment-06/starter/rsh_server.c
+72
-31
72 additions, 31 deletions
Assignment-06/starter/rsh_server.c
with
72 additions
and
31 deletions
Assignment-06/starter/rsh_server.c
+
72
−
31
View file @
9ebef86a
...
...
@@ -46,26 +46,33 @@
* IF YOU IMPLEMENT THE MULTI-THREADED SERVER FOR EXTRA CREDIT YOU NEED
* TO DO SOMETHING WITH THE is_threaded ARGUMENT HOWEVER.
*/
int
start_server
(
char
*
ifaces
,
int
port
,
int
is_threaded
){
int
start_server
(
char
*
ifaces
,
int
port
,
int
is_threaded
)
{
(
void
)
is_threaded
;
int
svr_socket
;
int
rc
;
printf
(
"SERVER: Starting server on %s:%d...
\n
"
,
ifaces
,
port
);
svr_socket
=
boot_server
(
ifaces
,
port
);
if
(
svr_socket
<
0
)
{
int
err_code
=
svr_socket
;
//server socket will carry error code
int
err_code
=
svr_socket
;
printf
(
"SERVER: Failed to boot server. Error code: %d
\n
"
,
err_code
);
return
err_code
;
}
printf
(
"SERVER: Server booted successfully. Socket: %d
\n
"
,
svr_socket
);
rc
=
process_cli_requests
(
svr_socket
);
printf
(
"SERVER: Stopping server...
\n
"
);
stop_server
(
svr_socket
);
printf
(
"SERVER: Server stopped. Return code: %d
\n
"
,
rc
);
return
rc
;
}
/*
* stop_server(svr_socket)
* svr_socket: The socket that was created in the boot_server()
...
...
@@ -74,9 +81,10 @@ int start_server(char *ifaces, int port, int is_threaded){
* This function simply returns the value of close() when closing
* the socket.
*/
int
stop_server
(
int
svr_socket
)
{
printf
(
"SERVER: Closing server socket: %d
\n
"
,
svr_socket
);
return
close
(
svr_socket
);
}
/*
...
...
@@ -112,59 +120,93 @@ int stop_server(int svr_socket){
* bind(), or listen() call fails.
*
*/
int
boot_server
(
char
*
ifaces
,
int
port
)
{
int
svr_socket
;
int
ret
;
struct
sockaddr_in
addr
;
// TODO set up the socket - this is very similar to the demo code
printf
(
"SERVER: Booting server on %s:%d...
\n
"
,
ifaces
,
port
);
svr_socket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
svr_socket
==
-
1
)
{
perror
(
"
socket
"
);
perror
(
"
SERVER: socket failed
"
);
return
ERR_RDSH_SERVER
;
}
printf
(
"SERVER: Socket created. Socket: %d
\n
"
,
svr_socket
);
int
enable
=
1
;
if
(
setsockopt
(
svr_socket
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
enable
,
sizeof
(
int
))
<
0
)
{
perror
(
"setsockopt"
);
perror
(
"
SERVER:
setsockopt
failed
"
);
close
(
svr_socket
);
return
ERR_RDSH_SERVER
;
}
printf
(
"SERVER: Socket options set.
\n
"
);
addr
.
sin_family
=
AF_INET
;
if
(
inet_pton
(
AF_INET
,
ifaces
,
&
addr
.
sin_addr
)
<=
0
)
{
perror
(
"inet_pton"
);
perror
(
"
SERVER:
inet_pton
failed
"
);
close
(
svr_socket
);
return
ERR_RDSH_SERVER
;
}
addr
.
sin_port
=
htons
(
port
);
printf
(
"SERVER: Address structure configured.
\n
"
);
ret
=
bind
(
svr_socket
,
(
const
struct
sockaddr
*
)
&
addr
,
sizeof
(
struct
sockaddr_in
));
if
(
ret
==
-
1
)
{
perror
(
"
bin
d"
);
perror
(
"
SERVER: bind faile
d"
);
close
(
svr_socket
);
return
ERR_RDSH_SERVER
;
}
printf
(
"SERVER: Socket bound to address.
\n
"
);
/*
* Prepare for accepting connections. The backlog size is set
* to 20. So while one request is being processed other requests
* can be waiting.
*/
ret
=
listen
(
svr_socket
,
20
);
if
(
ret
==
-
1
)
{
perror
(
"
listen
"
);
perror
(
"
SERVER: listen failed
"
);
close
(
svr_socket
);
return
ERR_RDSH_COMMUNICATION
;
}
printf
(
"SERVER: Listening for connections...
\n
"
);
printf
(
"SERVER
BOOTED
on %s:%d
\n
"
,
ifaces
,
port
);
printf
(
"SERVER
: Server booted successfully
on %s:%d
\n
"
,
ifaces
,
port
);
return
svr_socket
;
}
/*
* process_cli_requests(svr_socket)
*/
int
process_cli_requests
(
int
svr_socket
)
{
int
cli_socket
;
pthread_t
thread_id
;
printf
(
"SERVER: Waiting for client connections...
\n
"
);
while
(
1
)
{
printf
(
"SERVER: Waiting for a client to connect...
\n
"
);
cli_socket
=
accept
(
svr_socket
,
NULL
,
NULL
);
if
(
cli_socket
==
-
1
)
{
perror
(
"SERVER: accept failed"
);
return
ERR_RDSH_COMMUNICATION
;
}
printf
(
"SERVER: Client connected. Socket: %d
\n
"
,
cli_socket
);
if
(
pthread_create
(
&
thread_id
,
NULL
,
exec_client_requests
,
(
void
*
)
&
cli_socket
)
<
0
)
{
perror
(
"SERVER: Failed to create thread"
);
close
(
cli_socket
);
return
ERR_RDSH_COMMUNICATION
;
}
printf
(
"SERVER: Thread created for client. Thread ID: %lu
\n
"
,
(
unsigned
long
)
thread_id
);
pthread_detach
(
thread_id
);
printf
(
"SERVER: Thread detached.
\n
"
);
}
return
OK
;
}
/*
* process_cli_requests(svr_socket)
* svr_socket: The server socket that was obtained from boot_server()
...
...
@@ -397,8 +439,9 @@ int process_cli_requests(int svr_socket) {
*/
int
send_message_eof
(
int
cli_socket
)
{
printf
(
"SERVER: Sending EOF to client. Socket: %d
\n
"
,
cli_socket
);
if
(
send
(
cli_socket
,
&
RDSH_EOF_CHAR
,
1
,
0
)
<
0
)
{
perror
(
"
sen
d"
);
perror
(
"
SERVER: send faile
d"
);
return
ERR_RDSH_COMMUNICATION
;
}
return
OK
;
...
...
@@ -424,19 +467,17 @@ int send_message_eof(int cli_socket) {
* ERR_RDSH_COMMUNICATION: The send() socket call returned an error or if
* we were unable to send the message followed by the EOF character.
*/
int
send_message_string
(
int
cli_socket
,
char
*
buff
){
int
bytes_sent
;
bytes_sent
=
send
(
cli_socket
,
buff
,
strlen
(
buff
),
0
);
int
send_message_string
(
int
cli_socket
,
char
*
buff
)
{
printf
(
"SERVER: Sending message to client. Socket: %d
\n
"
,
cli_socket
);
int
bytes_sent
=
send
(
cli_socket
,
buff
,
strlen
(
buff
),
0
);
if
(
bytes_sent
<
0
)
{
perror
(
"
sen
d"
);
perror
(
"
SERVER: send faile
d"
);
return
ERR_RDSH_COMMUNICATION
;
}
return
OK
;
}
/*
* rsh_execute_pipeline(int cli_sock, command_list_t *clist)
* cli_sock: The server-side socket that is connected to the client
...
...
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