Last active
December 16, 2015 18:20
-
-
Save mxcl/5477244 to your computer and use it in GitHub Desktop.
All the examples on the web of socket usage are as complicated as possible, because that is helpful. Thanks morons. Anyway, here's a simple example.
This file contains 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
#if _WIN32 | |
#include <winsock2.h> | |
#else | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <sys/socket.h> | |
#endif | |
#if __APPLE__ || _WIN32 | |
#define MSG_NOSIGNAL 0 | |
#endif | |
#if _WIN32 | |
#define close(x) closesocket(x) | |
#endif | |
void simple_socket_example(sp_session *session) { | |
struct sockaddr_in serv_addr = { | |
.sin_family = AF_INET, | |
.sin_port = htons(13581), | |
.sin_addr = { .s_addr = INADDR_ANY } | |
}; | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); | |
send(sockfd, "hello there!\n", 13, MSG_NOSIGNAL); | |
close(sockfd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment