Last active
April 19, 2017 06:17
-
-
Save vvavrychuk/25a4300726da05e7be5c8ae8f584b8f7 to your computer and use it in GitHub Desktop.
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 <winsock2.h> | |
#include <ws2tcpip.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
WSADATA wsaData; | |
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { | |
fprintf(stderr, "WSAStartup failed.\n"); | |
exit(1); | |
} | |
int socketfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (socketfd == -1) { | |
fprintf(stderr, "socket failed!\n"); | |
exit(1); | |
} | |
struct sockaddr_in sa; | |
sa.sin_family = AF_INET; | |
inet_pton(AF_INET, "0.0.0.0", &(sa.sin_addr)); | |
sa.sin_port = htons(12345); | |
int ret = bind(socketfd, (const struct sockaddr*)&sa, sizeof(sa)); | |
if (ret == -1) { | |
fprintf(stderr, "bind failed!\n"); | |
exit(1); | |
} | |
ret = listen(socketfd, 0); | |
if (ret == -1) { | |
fprintf(stderr, "listen failed!\n"); | |
exit(1); | |
} | |
struct sockaddr_in their_addr; | |
socklen_t sin_size = sizeof(their_addr); | |
int client_sock = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size); | |
if (client_sock == -1) { | |
fprintf(stderr, "accept failed!\n"); | |
exit(1); | |
} | |
char buff[256]; | |
int len; | |
while ((len = recv(client_sock, buff, sizeof(buff), 0)) != -1) { | |
send(client_sock, "I received: ", strlen("I received: "), 0); | |
send(client_sock, buff, len, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment