Outstanding changes left since forever ago
This commit is contained in:
parent
626c54e66a
commit
4c883e91ee
71
server1.c
71
server1.c
@ -12,21 +12,20 @@
|
|||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
#define BUFFERT 512
|
#define BUFFER_LENGTH 512
|
||||||
#define BACKLOG 1
|
#define BACKLOG 1
|
||||||
|
|
||||||
void usage(char* programName);
|
void usage(char* programName);
|
||||||
int create_server_socket(int port);
|
int create_server_socket(int port);
|
||||||
struct sockaddr_in sock_serv, sock_clt;
|
struct sockaddr_in socket_server, socket_client;
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int sfd, fd;
|
int sfd, fd;
|
||||||
unsigned int length = sizeof(struct sockaddr_in);
|
|
||||||
long int n, m, count = 0;
|
long int n, m, count = 0;
|
||||||
unsigned int nsid;
|
unsigned int nsid;
|
||||||
ushort clt_port;
|
ushort client_port;
|
||||||
char buffer[BUFFERT], filename[256];
|
char buffer[BUFFER_LENGTH], filename[256];
|
||||||
char dst[INET_ADDRSTRLEN];
|
char dst[INET_ADDRSTRLEN];
|
||||||
time_t intps;
|
time_t intps;
|
||||||
struct tm* tmi;
|
struct tm* tmi;
|
||||||
@ -64,57 +63,56 @@ int main(int argc, char** argv)
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set socket options
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 )
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
|
||||||
{
|
{
|
||||||
perror("setsockopt error");
|
perror("setsockopt error");
|
||||||
exit(5);
|
exit(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
int l = sizeof(struct sockaddr_in);
|
// Clear out the server socket address struct
|
||||||
bzero(&sock_serv, l);
|
int sockaddr_size = sizeof(struct sockaddr_in);
|
||||||
|
bzero(&socket_server, sockaddr_size);
|
||||||
|
|
||||||
/* The following assignment should use; Internet version 4 protocols constant */
|
// Define properties of server socket address
|
||||||
sock_serv.sin_family =/* Q) What goes here? */;
|
socket_server.sin_family = AF_INET;
|
||||||
/* See; man socket */
|
socket_server.sin_port = htons(port);
|
||||||
|
socket_server.sin_addr.s_addr = htonl(0); // <-- 0.0.0.0, binds to all interfaces
|
||||||
|
|
||||||
sock_serv.sin_port = htons(atoi(argv[1]));
|
// Bind the socket to the address
|
||||||
|
if (bind(sfd, (struct sockaddr*) &socket_server, sockaddr_size) == -1)
|
||||||
/* Set the IP address to allow connections on any network interface on the server */
|
|
||||||
sock_serv.sin_addr.s_addr = htonl(/* Q) What goes here? */);
|
|
||||||
/* See; man 4 ip */
|
|
||||||
|
|
||||||
if (bind(sfd, (struct sockaddr*) &sock_serv, l) == -1)
|
|
||||||
{
|
{
|
||||||
perror("bind fail");
|
perror("bind fail");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
bzero(buffer,BUFFERT);
|
// Clear message buffer
|
||||||
listen(sfd,BACKLOG);
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
|
|
||||||
/* accept() takes "struct sockaddr_in" and returns non-negative integer that is a descriptor for the accepted socket.
|
// Start listening for connections, allowing a maximum of BACKLOG waiting connections
|
||||||
* The missing arguement in the line below will need to have the *address* of the variable required, as the value is updated when the socket is update. eg, &variable
|
listen(sfd, BACKLOG);
|
||||||
*/
|
|
||||||
nsid = accept(sfd, (struct sockaddr*) &sock_clt, /* Q) What goes here? */);
|
|
||||||
/* See; man 2 accept */
|
|
||||||
|
|
||||||
|
// Block and accept incoming connection
|
||||||
|
// TODO: Figure out what socklen actually is and why it's here
|
||||||
|
socklen_t socklen;
|
||||||
|
nsid = accept(sfd, (struct sockaddr*) &socket_client, &socklen);
|
||||||
|
|
||||||
|
//
|
||||||
if (nsid == -1)
|
if (nsid == -1)
|
||||||
{
|
{
|
||||||
perror("accept fail");
|
perror("accept fail");
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
if(inet_ntop(AF_INET, &socket_client.sin_addr, dst, INET_ADDRSTRLEN) == NULL)
|
||||||
if(inet_ntop(AF_INET, &sock_clt.sin_addr, dst, INET_ADDRSTRLEN) == NULL)
|
|
||||||
{
|
{
|
||||||
perror("socket error");
|
perror("socket error");
|
||||||
exit (4);
|
exit (4);
|
||||||
}
|
}
|
||||||
|
|
||||||
clt_port = ntohs(sock_clt.sin_port);
|
client_port = ntohs(socket_client.sin_port);
|
||||||
printf("connection : %s:%d\n", dst, clt_port);
|
printf("connection : %s:%d\n", dst, client_port);
|
||||||
|
|
||||||
bzero(filename, 256);
|
bzero(filename, 256);
|
||||||
|
|
||||||
@ -132,9 +130,9 @@ int main(int argc, char** argv)
|
|||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
bzero(buffer, BUFFERT);
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
|
|
||||||
n = recv(nsid, buffer, BUFFERT, 0);
|
n = recv(nsid, buffer, BUFFER_LENGTH, 0);
|
||||||
|
|
||||||
while(n)
|
while(n)
|
||||||
{
|
{
|
||||||
@ -152,17 +150,16 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
count += m;
|
count += m;
|
||||||
|
|
||||||
bzero(buffer, BUFFERT);
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
|
|
||||||
n = recv(nsid, buffer, BUFFERT, 0);
|
n = recv(nsid, buffer, BUFFER_LENGTH, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sfd);
|
close(sfd);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
|
||||||
|
|
||||||
close(nsid);
|
close(nsid);
|
||||||
printf("End of transmission with %s.%d\n", dst, clt_port);
|
printf("End of transmission with %s.%d\n", dst, client_port);
|
||||||
|
|
||||||
/* Select the appropriate variable in the following function call argument */
|
/* Select the appropriate variable in the following function call argument */
|
||||||
printf("Number of octets received : %ld \n", /* Q) What goes here? */);
|
printf("Number of octets received : %ld \n", /* Q) What goes here? */);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user