Created
January 30, 2020 21:54
-
-
Save donfreiday/2d08aca0ab31ae6888f06aee721fb407 to your computer and use it in GitHub Desktop.
esu networking class example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netdb.h> | |
#include <string.h> | |
#include <unistd.h> | |
void error(char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int main(int argc, char *argv[]) { | |
int sockfd, portno, n; | |
if (argc<3) { | |
fprintf(stdout, "Usage: %s hostname port", argv[0]); | |
exit(0); | |
} | |
portno = atoi(argv[2]); | |
const char *hostname = argv[1]; | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd <0) error("Error opening client socket"); | |
// The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() | |
// functions are obsolete. Applications should use getaddrinfo(3), | |
// getnameinfo(3), and gai_strerror(3) instead. | |
// struct hostent { | |
// char *h_name; /* official name of host */ | |
// char **h_aliases; /* alias list */ | |
// int h_addrtype; /* host address type */ | |
// int h_length; /* length of address */ | |
// char **h_addr_list; /* list of addresses */ | |
// } | |
// #define h_addr h_addr_list[0] /* for backward compatibility */ | |
// http://man7.org/linux/man-pages/man3/gethostbyname.3.html | |
struct hostent *server = gethostbyname(hostname); // resolve IP from hostname | |
if (server ==NULL) error("No such host"); | |
// Open server socket and connect | |
struct sockaddr_in serv_addr; | |
bzero((char*)&serv_addr, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
bcopy((char*)server->h_addr_list[0], (char*)&serv_addr.sin_addr.s_addr, server->h_length); | |
serv_addr.sin_port = htons(portno); | |
//while (1) { | |
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) != 0) error("Error connecting"); | |
// Read user message | |
printf("Please enter the message: "); | |
char buffer[256]; | |
bzero(buffer, 256); | |
fgets(buffer, 255, stdin); | |
// Send the message to server | |
n = write(sockfd, buffer, strlen(buffer)); | |
if (n <0) error("Error writing to socket"); | |
// Read response from server | |
bzero(buffer, 256); | |
n = read(sockfd, buffer, 255); | |
if (n < 0) error("Error reading from socket"); | |
printf("Server responded: %s\n", buffer); | |
//} | |
close(sockfd); | |
return 0; | |
} | |
// This was adapted from https://www.thegeekstuff.com/2011/12/c-socket-programming/?utm_source=feedburner | |
// Do not use this in production :) | |
// int main(int argc, char *argv[]) | |
// { | |
// if (argc <= 2) | |
// { | |
// printf("\n Usage: %s <ip of server> <optional message>\n", argv[0]); | |
// return 1; | |
// } | |
// int client_socket = 0; | |
// if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
// { | |
// printf("\n Error : Could not create socket \n"); | |
// return 1; | |
// } | |
// struct sockaddr_in serv_addr; | |
// memset(&serv_addr, '0', sizeof(serv_addr)); | |
// serv_addr.sin_family = AF_INET; | |
// serv_addr.sin_port = htons(5000); // htons: host to network byte order (short) | |
// // Here we set the server address in our struct | |
// // The inet_pton() function converts an address in its | |
// // standard text presentation form into its numeric | |
// // binary form. argv[1] is the first command line argument. | |
// if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) | |
// { | |
// printf("\nClient inet_pton error occurred: couldn't convert %s to IP\n", argv[1]); | |
// return 1; | |
// } | |
// // The connect() function shall attempt to make a connection on our socket. | |
// printf("Client: Attempting to connect to server\n"); | |
// while (connect(client_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) | |
// { | |
// printf("\nClient: Error connecting to %s: %s, retrying in 7 seconds.", argv[1], strerror(errno)); | |
// for (int i = 0; i < 7; i++) | |
// { | |
// sleep(1); | |
// printf("."); | |
// fflush(stdout); | |
// } | |
// printf("\n"); | |
// } | |
// printf("Client: Connected to server.\n"); | |
// char *msg = 0; | |
// if (argc >= 3) | |
// msg = argv[2]; | |
// else | |
// msg = "Hello I am a client!"; | |
// send(client_socket, msg, strlen(msg), 0); | |
// char buffer[1024]; | |
// read(client_socket, buffer, sizeof(buffer)); | |
// printf("Client: The server says: \"%s\"\n", buffer ); | |
// return 0; | |
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <time.h> | |
#include <unistd.h> | |
void error(char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int main(int argc, char **argv) { | |
const int MAX_CLIENTS = 5; | |
const int BUF_SIZE = 256; | |
char buffer[BUF_SIZE]; | |
struct sockaddr_in serv_addr, cli_addr; | |
if (argc < 2) { | |
fprintf(stderr, "ERROR NO PORT PROVIDED\n"); | |
exit(1); | |
} | |
int portno = atoi(argv[1]); | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd <0) error("Error opening socket"); | |
bzero((char*)&serv_addr, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(portno); | |
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // IPv4 address | |
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) error("error on binding"); | |
//while (1) { | |
listen(sockfd, MAX_CLIENTS); | |
socklen_t clilen = sizeof(cli_addr); | |
int newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen); | |
if (newsockfd < 0) error("ERROR on accept"); | |
bzero(buffer, BUF_SIZE); | |
int n = read(newsockfd, buffer, BUF_SIZE - 1); | |
if (n < 0) error("Error reading form socket"); | |
printf("Client sent %s\n", buffer); | |
n = write(newsockfd, "I got your message", 18); | |
if (n < 0) error("Error writing to socket"); | |
//} | |
close(sockfd); | |
return 0; | |
} | |
// int main(int argc, char *argv[]) | |
// { | |
// printf("Server: starting...\n"); | |
// // Structure for handling internet addresses | |
// // We will bind this to our socket later | |
// struct sockaddr_in serv_addr; | |
// memset(&serv_addr, '0', sizeof(serv_addr)); // Zero out serv_addr | |
// // The socket address will be IPv4 | |
// // sin_family is always set to AF_INET. This is required; in Linux 2.2 | |
// // most networking functions return EINVAL when this setting is missing. | |
// serv_addr.sin_family = AF_INET; | |
// // Set the IP address of the socket | |
// // htonl() converts hostlong from host byte order to network byte order. | |
// serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
// serv_addr.sin_port = htons(5000); | |
// // These 'file descriptors' will be used for our sockets, which | |
// // like everything in Linux are treated as files. | |
// // A socket is a combination of IP address and port on one system | |
// // The call to the function ‘socket()’ creates an UN-named socket | |
// // inside the kernel and returns an integer known as socket descriptor. | |
// // AF_INET: Internet family of IPv4 addresses we use . | |
// // SOCK_STREAM: specifies that the transport layer be reliable (have | |
// // acknowledgement techniques) The protocol is generally left zero to let the | |
// // kernel decide; it will be TCP | |
// int server_socket = socket(AF_INET, SOCK_STREAM, 0); | |
// // Attempt to prevent 'bind failed. Error: Address already in use' | |
// // by setting the 'reuse address' socket option | |
// if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) | |
// printf("Server: setsockopt(SO_REUSEADDR) failed\n"); | |
// // bind() assigns the details specified in the structure ‘serv_addr’ | |
// // to the socket created in the step above: | |
// // the family/domain, the interface to listen on, and the port on which | |
// // the server will wait for the client requests to come. | |
// while (bind(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) | |
// { | |
// printf("\nServer: bind() failed: error %s, retrying in 7 seconds", strerror(errno)); | |
// for (int i = 0; i < 7; i++) | |
// { | |
// sleep(1); | |
// printf("."); | |
// fflush(stdout); | |
// } | |
// printf("\n"); | |
// } | |
// // This call finishes setup of the socket. The second argument is | |
// // the maximum number of client connections the server will accept. | |
// listen(server_socket, 10); | |
// char *msg = "Hi, I am a server!"; | |
// char buffer[1024]; | |
// while (1) | |
// { | |
// // accept() puts the server to sleep until client completes TCP handshake | |
// // At which point the server awakens and connfd is assigned the client's | |
// // socket descriptor. | |
// int client_socket = accept(server_socket, (struct sockaddr *)NULL, NULL); | |
// // Read client message | |
// read(client_socket, buffer, sizeof(buffer)); | |
// printf("Server: The client said \"%s\"\n", buffer); | |
// // Send back a message | |
// send(client_socket, msg, strlen(msg), 0); | |
// close(client_socket); | |
// sleep(0.1); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment