tahamajs's picture
download
raw
2.83 kB
#include <arpa/inet.h> // htons(), ntohs()
#include <netdb.h> // gethostbyname(), struct hostent
#include <netinet/in.h> // struct sockaddr_in
#include <stdio.h> // perror(), fprintf()
#include <string.h> // memcpy()
#include <sys/socket.h> // getsockname()
#include <unistd.h> // stderr
/**
* Make a server sockaddr given a port.
* Parameters:
* addr: The sockaddr to modify (this is a C-style function).
* port: The port on which to listen for incoming connections.
* Returns:
* 0 on success, -1 on failure.
* Example:
* struct sockaddr_in server;
* int err = make_server_sockaddr(&server, 8888);
*/
int make_server_sockaddr(struct sockaddr_in *addr, int port) {
// Step (1): specify socket family.
// This is an internet socket.
addr->sin_family = AF_INET;
// Step (2): specify socket address (hostname).
// The socket will be a server, so it will only be listening.
// Let the OS map it to the correct address.
addr->sin_addr.s_addr = INADDR_ANY;
// Step (3): Set the port value.
// If port is 0, the OS will choose the port for us.
// Use htons to convert from local byte order to network byte order.
addr->sin_port = htons(port);
return 0;
}
/**
* Make a client sockaddr given a remote hostname and port.
* Parameters:
* addr: The sockaddr to modify (this is a C-style function).
* hostname: The hostname of the remote host to connect to.
* port: The port to use to connect to the remote hostname.
* Returns:
* 0 on success, -1 on failure.
* Example:
* struct sockaddr_in client;
* int err = make_client_sockaddr(&client, "141.88.27.42", 8888);
*/
int make_client_sockaddr(struct sockaddr_in *addr, const char *hostname, int port) {
// Step (1): specify socket family.
// This is an internet socket.
addr->sin_family = AF_INET;
// Step (2): specify socket address (hostname).
// The socket will be a client, so call this unix helper function
// to convert a hostname string to a useable `hostent` struct.
struct hostent *host = gethostbyname(hostname);
if (host == nullptr) {
fprintf(stderr, "%s: unknown host\n", hostname);
return -1;
}
memcpy(&(addr->sin_addr), host->h_addr, host->h_length);
// Step (3): Set the port value.
// Use htons to convert from local byte order to network byte order.
addr->sin_port = htons(port);
return 0;
}
/**
* Return the port number assigned to a socket.
*
* Parameters:
* sockfd: File descriptor of a socket
*
* Returns:
* The port number of the socket, or -1 on failure.
*/
int get_port_number(int sockfd) {
struct sockaddr_in addr;
socklen_t length = sizeof(addr);
if (getsockname(sockfd, (sockaddr *) &addr, &length) == -1) {
perror("Error getting port of socket");
return -1;
}
// Use ntohs to convert from network byte order to host byte order.
return ntohs(addr.sin_port);
}

Xet Storage Details

Size:
2.83 kB
·
Xet hash:
41c56189a12d706e6185692ef9123b3f695d5ce496f95380393640a18b7a5d4f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.