Skip to content

Instantly share code, notes, and snippets.

@mxcl
Last active December 16, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxcl/5477244 to your computer and use it in GitHub Desktop.
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.
#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