Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
csci315
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cdf009
csci315
Commits
4dac875f
Commit
4dac875f
authored
Sep 25, 2018
by
cdf009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
help
parent
3abb62f0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
272 additions
and
0 deletions
+272
-0
Labs/Lab4/echod.c
Labs/Lab4/echod.c
+143
-0
Labs/Lab4/echoreq.c
Labs/Lab4/echoreq.c
+129
-0
No files found.
Labs/Lab4/echod.c
0 → 100644
View file @
4dac875f
/*
* CSCI 315 Operating Systems Design
* Author: L. Felipe Perrone
* Date: 2010-02-16
* Copyright (c) 2011 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual
* or institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright
* and permission notice is maintained, intact, in all copies and
* supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 512 // length of message buffer
#define QLEN 6 // length of request queue
#define FOREVER 1
#define TRUE 1
#define FALSE 0
int
num_requests
=
0
;
// tally of client requests
/*------------------------------------------------------------------------
* Program: echod - a server for echo requests
*
* Purpose: repeatedly execute the following:
* (0) wait for a connection request from client
* (1) wait for a null-terminated string from client
* (2) send back the same string to client
* (3) close the connection
* (4) go back to step (0)
*
* Usage: echod [ port ]
*
* port - a port number in user space
*
*------------------------------------------------------------------------
*/
int
main
(
int
argc
,
char
*
argv
[])
{
struct
sockaddr_in
sad
;
// structure to hold server's address
struct
sockaddr_in
cad
;
// structure to hold client's address
int
sd
,
sd2
;
// socket descriptors
int
port
;
// protocol port number
socklen_t
alen
;
// length of address
char
in_msg
[
BUFFER_SIZE
];
// buffer for incoming message
// prepare address data structure
// The memset call is ESSENTIAL!
// if you don't do this every time you create a sockaddr struct, you will
// see some pretty strange behaviour
memset
((
char
*
)
&
sad
,
0
,
sizeof
(
sad
));
// zero out sockaddr structure
sad
.
sin_family
=
AF_INET
;
// set family to Internet
sad
.
sin_addr
.
s_addr
=
INADDR_ANY
;
// set the local IP address
// verify usage
if
(
argc
>
1
)
{
port
=
atoi
(
argv
[
1
]);
}
else
{
printf
(
"Usage: %s [ port ]
\n
"
,
argv
[
0
]);
exit
(
-
1
);
}
if
(
port
>
0
)
// test for illegal value
sad
.
sin_port
=
htons
((
u_short
)
port
);
else
{
// print error message and exit
fprintf
(
stderr
,
"ECHOD: bad port number %s
\n
"
,
argv
[
1
]);
exit
(
-
1
);
}
// create socket
sd
=
socket
(
PF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
if
(
sd
<
0
)
{
perror
(
"ECHOD: socket creation failed"
);
exit
(
-
1
);
}
// assign IP/port number to socket where connections come in
if
(
bind
(
sd
,
(
struct
sockaddr
*
)
&
sad
,
sizeof
(
sad
))
<
0
)
{
perror
(
"ECHOD: bind failed"
);
exit
(
-
1
);
}
// set up socket to receive incomming connections
if
(
listen
(
sd
,
QLEN
)
<
0
)
{
perror
(
"ECHOD: listen failed"
);
exit
(
-
1
);
}
// main server loop - accept and handle requests
while
(
FOREVER
)
{
alen
=
sizeof
(
cad
);
if
(
(
sd2
=
accept
(
sd
,
(
struct
sockaddr
*
)
&
cad
,
&
alen
))
<
0
)
{
perror
(
"ECHOD: accept failed
\n
"
);
exit
(
-
1
);
}
num_requests
++
;
// receive the string sent by client
strcpy
(
in_msg
,
""
);
recv
(
sd2
,
in_msg
,
BUFFER_SIZE
,
0
);
printf
(
"done"
);
// send the received string back to client
//send(sd, buf, len flags);
send
(
sd2
,
in_msg
,
BUFFER_SIZE
,
0
);
close
(
sd2
);
}
}
Labs/Lab4/echoreq.c
0 → 100644
View file @
4dac875f
/*CSCI 315 Operating Systems Design
* Author: L. Felipe Perrone
* Date: 2014-09-21
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual
* or institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright
* and permission notice is maintained, intact, in all copies and
* supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 512
#define TRUE 1
#define FALSE 0
/*------------------------------------------------------------------------
* Program: echoreq
*
* Purpose: allocate a socket, connect to a server, transfer requested
* file to local host, and print file contents to stdout
*
* Usage: echoreq [ host ] [ port ] [ string ]
*
* host - name of a computer on which server is executing
* port - protocol port number server is using
* string - a string in double quotes
*
*------------------------------------------------------------------------
*/
int
main
(
int
argc
,
char
*
argv
[])
{
struct
hostent
*
ptrh
;
// pointer to a host table entry
struct
sockaddr_in
sad
;
// structure to hold an IP address
int
sd
;
// socket descriptor
int
port
;
// protocol port number
char
*
host
;
// pointer to host name
char
in_msg
[
BUFFER_SIZE
];
// buffer for incoming message
int
ret_val
;
sad
.
sin_family
=
AF_INET
;
// set family to Internet
// verify usage
if
(
argc
<
4
)
{
printf
(
"Usage: %s [ host ] [ port ] [ string ]
\n
"
,
argv
[
0
]);
exit
(
-
1
);
}
host
=
argv
[
1
];
port
=
atoi
(
argv
[
2
]);
if
(
port
>
0
)
// test for legal value
sad
.
sin_port
=
htons
((
u_short
)
port
);
else
{
// print error message and exit
printf
(
"ECHOREQ: bad port number %s
\n
"
,
argv
[
2
]);
exit
(
-
1
);
}
// convert host name to equivalent IP address and copy to sad
ptrh
=
gethostbyname
(
host
);
if
(
((
char
*
)
ptrh
)
==
NULL
)
{
printf
(
"ECHOREQ: invalid host: %s
\n
"
,
host
);
exit
(
-
1
);
}
memcpy
(
&
sad
.
sin_addr
,
ptrh
->
h_addr
,
ptrh
->
h_length
);
// create socket
sd
=
socket
(
PF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
if
(
sd
<
0
)
{
printf
(
"ECHOREQ: socket creation failed
\n
"
);
exit
(
-
1
);
}
// connect the socket to the specified server
if
(
connect
(
sd
,
(
struct
sockaddr
*
)
&
sad
,
sizeof
(
sad
))
<
0
)
{
perror
(
"ECHOREQ: connect failed"
);
exit
(
-
1
);
}
// send message to server
send
(
sd
,
in_msg
,
BUFFER_SIZE
,
0
);
printf
(
"sent"
);
// send the received string back to client
printf
(
"
\n
"
);
// //send(sd, buf, len flags);
strcpy
(
in_msg
,
""
);
recv
(
sd
,
in_msg
,
BUFFER_SIZE
,
0
);
//
// receive message echoed back by server
printf
(
"ECHOREQ: from server= %s
\n
"
,
in_msg
);
// close the socket
close
(
sd
);
// terminate the client program gracefully
return
(
0
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment