Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include "assign1.h"
  7.  
  8.  
  9. //feel free to include file headers you need that's not already in assign1.h
  10.  
  11. /**
  12.  * Student Task 1: Fill in the infos below.
  13.  */
  14. char * get_student_name() {
  15.     return "Chase_Delgadillo";
  16. }
  17. char * get_student_id() {
  18.     //make sure it there is no trailing spaces
  19.     return "891706806";
  20. }
  21. int get_class_section_number() {
  22.     return 5;
  23. }
  24.  
  25. /**
  26.  * Student Task 2: implement socket server
  27.  * Listen for TCP connection at loopback address 127.0.0.1
  28.  * You must use the port defined by the macro: STUDENT_ANSWER_SOCKET_PORT
  29.  * When the client connects, send the message string to the client.
  30.  * You must perform proper cleanups such as closing sockets after the one client has been served.
  31.  *
  32.  * @param message the message to send when the client connects
  33.  * @return 0 if success otherwise return non-zero
  34.  */
  35. int socket_server(char message[MAX_FILE_LENGTH]) {
  36.     //TODO: implement
  37.     int serversockfd, newsockfd;
  38.     struct sockaddr_in serv_addr;
  39.     struct sockaddr_storage serv_storage;
  40.     socklen_t addr_size;
  41.  
  42.     /**Create socket
  43.      * int socket(int domain, int type, int protocol)
  44.      * domain   : protocol family used for communication [defined in <sys/socket.h>]
  45.      *      AF_INET: Address Family, IPv4 Internet Protocols
  46.      *      PF_INET: Protocol Family, IPv4 Internet Protocols
  47.      * type     : specifies communication semantics
  48.      *      SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte
  49.      *      streams
  50.      *          Must be connected (connect()) before data can be
  51.      *          sent (send()) or received (recv())
  52.      * protocol : specifies particular protocol to be used with socket
  53.      *      Normally, only a single protocol exists to support a socket type
  54.      *      within a given protocol family, in which case, set to 0
  55.      */
  56.     serversockfd = socket(PF_INET, SOCK_STREAM, 0);
  57.  
  58.     //Configure server address struct settings
  59.     // family: required to be set to AF_INET: Address Family, IPv4 Internet Protocols
  60.     serv_addr.sin_family = AF_INET;
  61.     // port: STUDENT_ANSWER_SOCKET_PORT, based off definition in header
  62.     //  htons() converts unsigned integer hostlong from host byte order to network byte order
  63.     serv_addr.sin_port = htons(STUDENT_ANSWER_SOCKET_PORT);
  64.     // ip address: TCP connection to loopback address
  65.     serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  66.     // Avoid bugs by padding buffer with sin_zero
  67.     // Ref: https://silviocesare.wordpress.com/2007/10/22/setting-sin_zero-to-0-in-struct-sockaddr_in/
  68.     memset(&serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));
  69.  
  70.     /**Bind address/assigns a name to the socket
  71.      *  By assigning a name to the socket,
  72.      *  we cannot bind socket to the port twice for multiple tests
  73.      * int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  74.      * sockfd  : socket to be assigned address
  75.      * addr    : address to assign socket
  76.      * addrlen : size of address structure, addr
  77.      */
  78.     if (bind(serversockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  79.         printf("ERROR binding...\n");
  80.  
  81.     /**Listen on socket, max connections requests in queue: 5
  82.      * int listen(int sockfd, int backlog)
  83.      * sockfd  : socket to be marked as passive socket
  84.      * backlog : max length of pending connections for sockfd to queue
  85.      */
  86.     if (listen(serversockfd, 5) < 0)
  87.         printf("ERROR listening...\n");
  88.    
  89.     /**Accept call creates new socket for incoming connection
  90.      * int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
  91.      * sockfd  : the created, bound, and listening socket
  92.      * addr    : pointer to sockaddr structure filled with peer socket addresses
  93.      * addrlen : size of sockaddr structure, addr
  94.      */
  95.     addr_size = sizeof(serv_storage);
  96.     newsockfd = accept(serversockfd, (struct sockaddr *) &serv_storage, &addr_size);
  97.  
  98.     //Send message to socket of incoming connection
  99.     strcpy(message, "Hello World\n");
  100.     send(newsockfd, message, MAX_FILE_LENGTH, 0);
  101.  
  102.     close(newsockfd);
  103.     close(serversockfd);
  104.     return 0;
  105. }
  106.  
  107. /**
  108.  * Student Task 3: implement socket client
  109.  * Make a TCP connection to loopback address 127.0.0.1
  110.  * You must use the port defined by the macro: STUDENT_ANSWER_SOCKET_PORT
  111.  *
  112.  * Upon connection completion, read a message string from the server.
  113.  * Close the connection and make sure the content of the message is
  114.  * written or copied to the in parameter message
  115.  * @return 0 if success otherwise return non-zero
  116.  */
  117. int socket_client(char message[MAX_FILE_LENGTH]) {
  118.     //TODO: implement
  119.     int clientsockfd;
  120.     struct sockaddr_in serv_addr;
  121.     socklen_t addr_size;
  122.  
  123.     /**Create socket
  124.      * int socket(int domain, int type, int protocol)
  125.      * domain   : protocol family used for communication [defined in <sys/socket.h>]
  126.      *      AF_INET: Address Family, IPv4 Internet Protocols
  127.      *      PF_INET: Protocol Family, IPv4 Internet Protocols
  128.      * type     : specifies communication semantics
  129.      *      SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte
  130.      *      streams
  131.      *          Must be connected (connect()) before data can be
  132.      *          sent (send()) or received (recv())
  133.      * protocol : specifies particular protocol to be used with socket
  134.      *      Normally, only a single protocol exists to support a socket type
  135.      *      within a given protocol family, in which case, set to 0
  136.      */
  137.     clientsockfd = socket(PF_INET, SOCK_STREAM, 0);
  138.  
  139.  
  140.     //Configure server address struct settings
  141.     // family: required to be set to AF_INET: Address Family, IPv4 Internet Protocols
  142.     serv_addr.sin_family = AF_INET;
  143.     // port: STUDENT_ANSWER_SOCKET_PORT, based off definition in header
  144.     //  htons() converts unsigned integer hostlong from host byte order to network byte order
  145.     serv_addr.sin_port = htons(STUDENT_ANSWER_SOCKET_PORT);
  146.     // ip address: TCP connection to loopback address
  147.     serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  148.     // Avoid bugs by padding buffer with sin_zero
  149.     // Ref: https://silviocesare.wordpress.com/2007/10/22/setting-sin_zero-to-0-in-struct-sockaddr_in/
  150.     memset(&serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));
  151.  
  152.  
  153.     /**Connect socket to server
  154.      * int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  155.      * sockfd  : socket being connected
  156.      * addr    : address we connect socket to
  157.      * addrlen : size of address, addr
  158.      */
  159.     addr_size = sizeof(serv_addr);
  160.     if (connect(clientsockfd, (struct sockaddr *) &serv_addr, addr_size) < 0)
  161.         printf("ERROR connecting...\n");
  162.  
  163.  
  164.     /**Read the message from the server into the message parameter given
  165.      * ssize_t recv(int sockfd, void *buf, size_t len, int flags)
  166.      * sockfd : connected socket receiving message
  167.      * buf    : char buffer for recieved message
  168.      * len    : size of char buffer, buf
  169.      * flags  : message flags for info provided with recv() call
  170.      *      Set to 0 due to lack of use, but to avoid zero-length datagram pending issue
  171.      */
  172.     recv(clientsockfd, message, MAX_FILE_LENGTH, 0);
  173.  
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement