Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <string.h>
- #include "assign1.h"
- //feel free to include file headers you need that's not already in assign1.h
- /**
- * Student Task 1: Fill in the infos below.
- */
- char * get_student_name() {
- return "Chase_Delgadillo";
- }
- char * get_student_id() {
- //make sure it there is no trailing spaces
- return "891706806";
- }
- int get_class_section_number() {
- return 5;
- }
- /**
- * Student Task 2: implement socket server
- * Listen for TCP connection at loopback address 127.0.0.1
- * You must use the port defined by the macro: STUDENT_ANSWER_SOCKET_PORT
- * When the client connects, send the message string to the client.
- * You must perform proper cleanups such as closing sockets after the one client has been served.
- *
- * @param message the message to send when the client connects
- * @return 0 if success otherwise return non-zero
- */
- int socket_server(char message[MAX_FILE_LENGTH]) {
- //TODO: implement
- int serversockfd, newsockfd;
- struct sockaddr_in serv_addr;
- struct sockaddr_storage serv_storage;
- socklen_t addr_size;
- /**Create socket
- * int socket(int domain, int type, int protocol)
- * domain : protocol family used for communication [defined in <sys/socket.h>]
- * AF_INET: Address Family, IPv4 Internet Protocols
- * PF_INET: Protocol Family, IPv4 Internet Protocols
- * type : specifies communication semantics
- * SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte
- * streams
- * Must be connected (connect()) before data can be
- * sent (send()) or received (recv())
- * protocol : specifies particular protocol to be used with socket
- * Normally, only a single protocol exists to support a socket type
- * within a given protocol family, in which case, set to 0
- */
- serversockfd = socket(PF_INET, SOCK_STREAM, 0);
- //Configure server address struct settings
- // family: required to be set to AF_INET: Address Family, IPv4 Internet Protocols
- serv_addr.sin_family = AF_INET;
- // port: STUDENT_ANSWER_SOCKET_PORT, based off definition in header
- // htons() converts unsigned integer hostlong from host byte order to network byte order
- serv_addr.sin_port = htons(STUDENT_ANSWER_SOCKET_PORT);
- // ip address: TCP connection to loopback address
- serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- // Avoid bugs by padding buffer with sin_zero
- // Ref: https://silviocesare.wordpress.com/2007/10/22/setting-sin_zero-to-0-in-struct-sockaddr_in/
- memset(&serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));
- /**Bind address/assigns a name to the socket
- * By assigning a name to the socket,
- * we cannot bind socket to the port twice for multiple tests
- * int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
- * sockfd : socket to be assigned address
- * addr : address to assign socket
- * addrlen : size of address structure, addr
- */
- if (bind(serversockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
- printf("ERROR binding...\n");
- /**Listen on socket, max connections requests in queue: 5
- * int listen(int sockfd, int backlog)
- * sockfd : socket to be marked as passive socket
- * backlog : max length of pending connections for sockfd to queue
- */
- if (listen(serversockfd, 5) < 0)
- printf("ERROR listening...\n");
- /**Accept call creates new socket for incoming connection
- * int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
- * sockfd : the created, bound, and listening socket
- * addr : pointer to sockaddr structure filled with peer socket addresses
- * addrlen : size of sockaddr structure, addr
- */
- addr_size = sizeof(serv_storage);
- newsockfd = accept(serversockfd, (struct sockaddr *) &serv_storage, &addr_size);
- //Send message to socket of incoming connection
- strcpy(message, "Hello World\n");
- send(newsockfd, message, MAX_FILE_LENGTH, 0);
- close(newsockfd);
- close(serversockfd);
- return 0;
- }
- /**
- * Student Task 3: implement socket client
- * Make a TCP connection to loopback address 127.0.0.1
- * You must use the port defined by the macro: STUDENT_ANSWER_SOCKET_PORT
- *
- * Upon connection completion, read a message string from the server.
- * Close the connection and make sure the content of the message is
- * written or copied to the in parameter message
- * @return 0 if success otherwise return non-zero
- */
- int socket_client(char message[MAX_FILE_LENGTH]) {
- //TODO: implement
- int clientsockfd;
- struct sockaddr_in serv_addr;
- socklen_t addr_size;
- /**Create socket
- * int socket(int domain, int type, int protocol)
- * domain : protocol family used for communication [defined in <sys/socket.h>]
- * AF_INET: Address Family, IPv4 Internet Protocols
- * PF_INET: Protocol Family, IPv4 Internet Protocols
- * type : specifies communication semantics
- * SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte
- * streams
- * Must be connected (connect()) before data can be
- * sent (send()) or received (recv())
- * protocol : specifies particular protocol to be used with socket
- * Normally, only a single protocol exists to support a socket type
- * within a given protocol family, in which case, set to 0
- */
- clientsockfd = socket(PF_INET, SOCK_STREAM, 0);
- //Configure server address struct settings
- // family: required to be set to AF_INET: Address Family, IPv4 Internet Protocols
- serv_addr.sin_family = AF_INET;
- // port: STUDENT_ANSWER_SOCKET_PORT, based off definition in header
- // htons() converts unsigned integer hostlong from host byte order to network byte order
- serv_addr.sin_port = htons(STUDENT_ANSWER_SOCKET_PORT);
- // ip address: TCP connection to loopback address
- serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- // Avoid bugs by padding buffer with sin_zero
- // Ref: https://silviocesare.wordpress.com/2007/10/22/setting-sin_zero-to-0-in-struct-sockaddr_in/
- memset(&serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));
- /**Connect socket to server
- * int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
- * sockfd : socket being connected
- * addr : address we connect socket to
- * addrlen : size of address, addr
- */
- addr_size = sizeof(serv_addr);
- if (connect(clientsockfd, (struct sockaddr *) &serv_addr, addr_size) < 0)
- printf("ERROR connecting...\n");
- /**Read the message from the server into the message parameter given
- * ssize_t recv(int sockfd, void *buf, size_t len, int flags)
- * sockfd : connected socket receiving message
- * buf : char buffer for recieved message
- * len : size of char buffer, buf
- * flags : message flags for info provided with recv() call
- * Set to 0 due to lack of use, but to avoid zero-length datagram pending issue
- */
- recv(clientsockfd, message, MAX_FILE_LENGTH, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement