Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp2023-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
Saleh Ahmed Khan
comp2023-2018-project-1
Commits
fd081b11
Commit
fd081b11
authored
Apr 15, 2018
by
Saleh Ahmed Khan
Browse files
Options
Downloads
Patches
Plain Diff
partially working server
parent
53a222bf
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
.gitignore
+4
-0
4 additions, 0 deletions
.gitignore
get.c
+45
-0
45 additions, 0 deletions
get.c
get.h
+10
-0
10 additions, 0 deletions
get.h
home/file.css
+0
-0
0 additions, 0 deletions
home/file.css
makefile
+1
-1
1 addition, 1 deletion
makefile
server.c
+11
-23
11 additions, 23 deletions
server.c
with
71 additions
and
24 deletions
.gitignore
0 → 100644
+
4
−
0
View file @
fd081b11
/.vscode
client
server
debug
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.c
0 → 100644
+
45
−
0
View file @
fd081b11
#include
"get.h"
#include
<stdio.h>
#include
<string.h>
void
valid_get
(
char
*
buffer
)
{
// checks if the request is valid
// const char* path_start = strchr(buffer, ' ');
// const char* path_end = strrchr(req, ' ');
// printf("gucci\n");
}
void
parse
(
char
*
req
,
char
*
root
)
{
// Extract just the file path from the request
// message into the char array 'path'.
// printf("%s\n", req);
const
char
*
path_start
=
strchr
(
req
,
' '
)
+
1
;
// printf("%c start\n", *path_start);
const
char
*
path_end
=
strrchr
(
req
,
' '
);
// printf("%c final\n", *path_end);
// int path_len = path_start - path_end;
char
path
[
path_end
-
path_start
];
strncpy
(
path
,
path_start
,
path_end
-
path_start
);
path
[
sizeof
(
path
)]
=
'\0'
;
// null terminator
// printf("root %s\n", root);
// printf("path %s\n", path);
char
full_path
[
sizeof
(
path
)
+
sizeof
(
root
)];
strncpy
(
full_path
,
root
,
sizeof
(
root
));
strcat
(
full_path
,
path
);
// full_path[sizeof(full_path)-1] = 9;
printf
(
"waiittt %s end
\n
"
,
full_path
);
find_file
(
full_path
);
}
void
find_file
(
char
*
path_to_file
)
{
FILE
*
fp
;
fp
=
fopen
(
path_to_file
,
"r"
);
if
(
fp
!=
NULL
)
{
printf
(
"success...
\n
"
);
fclose
(
fp
);
}
else
{
printf
(
"fail
\n
"
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.h
0 → 100644
+
10
−
0
View file @
fd081b11
/* the include guards */
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED
/* Prototypes for the functions */
void
valid_get
(
char
*
buffer
);
void
parse
(
char
*
req
,
char
*
root
);
void
find_file
(
char
*
path_to_file
);
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
home/file.css
0 → 100644
+
0
−
0
View file @
fd081b11
This diff is collapsed.
Click to expand it.
makefile
+
1
−
1
View file @
fd081b11
server
:
server.c
server
:
server.c
gcc
-o
server server.c
gcc
-o
server server.c
get.c
client
:
client.c
client
:
client.c
gcc
-o
client client.c
gcc
-o
client client.c
...
...
This diff is collapsed.
Click to expand it.
server.c
+
11
−
23
View file @
fd081b11
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include
<netinet/in.h>
#include
<netinet/in.h>
#include
<unistd.h>
#include
<unistd.h>
#include
"get.h"
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
...
@@ -19,18 +20,14 @@ int main(int argc, char *argv[])
...
@@ -19,18 +20,14 @@ int main(int argc, char *argv[])
socklen_t
clilen
;
socklen_t
clilen
;
int
n
;
int
n
;
if
(
argc
<
2
)
if
(
argc
<
2
)
{
{
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
/* Create TCP socket */
/* Create TCP socket */
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
{
if
(
sockfd
<
0
)
{
perror
(
"ERROR opening socket"
);
perror
(
"ERROR opening socket"
);
exit
(
1
);
exit
(
1
);
}
}
...
@@ -43,64 +40,55 @@ int main(int argc, char *argv[])
...
@@ -43,64 +40,55 @@ int main(int argc, char *argv[])
/* Create address we're going to listen on (given port number)
/* Create address we're going to listen on (given port number)
- converted to network byte order & any IP address for
- converted to network byte order & any IP address for
this machine */
this machine */
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_port
=
htons
(
portno
);
// store in machine-neutral format
serv_addr
.
sin_port
=
htons
(
portno
);
// store in machine-neutral format
/* Bind address to the socket */
/* Bind address to the socket */
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
perror
(
"ERROR on binding"
);
perror
(
"ERROR on binding"
);
exit
(
1
);
exit
(
1
);
}
}
/* Listen on socket - means we're ready to accept connections -
/* Listen on socket - means we're ready to accept connections -
incoming connection requests will be queued */
incoming connection requests will be queued */
listen
(
sockfd
,
5
);
listen
(
sockfd
,
5
);
clilen
=
sizeof
(
cli_addr
);
clilen
=
sizeof
(
cli_addr
);
/* Accept a connection - block until a connection is ready to
/* Accept a connection - block until a connection is ready to
be accepted. Get back a new file descriptor to communicate on. */
be accepted. Get back a new file descriptor to communicate on. */
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
&
clilen
);
if
(
newsockfd
<
0
)
if
(
newsockfd
<
0
)
{
{
perror
(
"ERROR on accept"
);
perror
(
"ERROR on accept"
);
exit
(
1
);
exit
(
1
);
}
}
bzero
(
buffer
,
256
);
bzero
(
buffer
,
256
);
/* Read characters from the connection,
/* Read characters from the connection,
then process */
then process */
n
=
read
(
newsockfd
,
buffer
,
255
);
n
=
read
(
newsockfd
,
buffer
,
255
);
if
(
n
<
0
)
if
(
n
<
0
)
{
{
perror
(
"ERROR reading from socket"
);
perror
(
"ERROR reading from socket"
);
exit
(
1
);
exit
(
1
);
}
}
/* using the get.c functionality here: */
char
*
root
=
argv
[
2
];
parse
(
buffer
,
root
);
printf
(
"Here is the message: %s
\n
"
,
buffer
);
printf
(
"Here is the message: %s
\n
"
,
buffer
);
n
=
write
(
newsockfd
,
"I got your message"
,
18
);
n
=
write
(
newsockfd
,
"I got your message"
,
18
);
if
(
n
<
0
)
if
(
n
<
0
)
{
{
perror
(
"ERROR writing to socket"
);
perror
(
"ERROR writing to socket"
);
exit
(
1
);
exit
(
1
);
}
}
/* close socket */
/* close socket */
close
(
sockfd
);
close
(
sockfd
);
return
0
;
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