Skip to content

Instantly share code, notes, and snippets.

@obiltschnig
Created December 7, 2019 09:38
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 obiltschnig/13e73d98d386d88f772c0064a9ba0143 to your computer and use it in GitHub Desktop.
Save obiltschnig/13e73d98d386d88f772c0064a9ba0143 to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
struct sockaddr_in sa;
int ssock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ssock == -1) {
perror("cannot create socket");
exit(EXIT_FAILURE);
}
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_port = htons(1092);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(ssock, (struct sockaddr*) &sa, sizeof sa) == -1)
{
perror("bind failed");
close(ssock);
exit(EXIT_FAILURE);
}
if (listen(ssock, 10) == -1)
{
perror("listen failed");
close(ssock);
exit(EXIT_FAILURE);
}
for (;;)
{
int csock = accept(ssock, NULL, NULL);
if (csock < 0)
{
perror("accept failed");
close(ssock);
exit(EXIT_FAILURE);
}
send(csock, "Hello, world!\r\n", 15, 0);
close(csock);
}
close(ssock);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment