{"id":20162253,"url":"https://github.com/ralucas/basic_c_socket_server","last_synced_at":"2025-06-21T20:05:00.775Z","repository":{"id":71020194,"uuid":"65479551","full_name":"ralucas/basic_c_socket_server","owner":"ralucas","description":null,"archived":false,"fork":false,"pushed_at":"2016-08-12T05:45:41.000Z","size":3,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T10:45:04.848Z","etag":null,"topics":["c","socket-server"],"latest_commit_sha":null,"homepage":"http://blog.richardalucas.com/2016/08/11/Writing-a-small-socket-server-in-C/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ralucas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-11T15:16:01.000Z","updated_at":"2020-09-30T08:08:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"20c28a8b-5254-40e3-81d9-6fed999da8af","html_url":"https://github.com/ralucas/basic_c_socket_server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ralucas/basic_c_socket_server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralucas%2Fbasic_c_socket_server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralucas%2Fbasic_c_socket_server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralucas%2Fbasic_c_socket_server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralucas%2Fbasic_c_socket_server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ralucas","download_url":"https://codeload.github.com/ralucas/basic_c_socket_server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralucas%2Fbasic_c_socket_server/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261186769,"owners_count":23121943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","socket-server"],"created_at":"2024-11-14T00:24:03.955Z","updated_at":"2025-06-21T20:04:55.763Z","avatar_url":"https://github.com/ralucas.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Building a socket server in C is a great introduction to the language as well as IPC mechanisms such as sockets and it can show you how simple it is to set up a server.  For this installment, we're going to do a single-threaded version.\n\nSo, let's get started...\n\n### The Server\n\nFirst I want to define some constants that we'll use throughout. For some of these you may want to pass these in dynamically, but for this case I'm just hardcoding them here as constants:\n\n```\n#define SOCKET_ERROR  -1\n\n#define MAX_PENDING_CONNECTIONS   10\n#define LOCALHOST   \"127.0.0.1\"\n#define PORT        8080\n#define BUFSIZE     1024\n```\n\nWe'll put the server in a void function that we'll call serve: `void serve()`, so\n```\nvoid serve() {\n  // Put all subsequent code here\n```\n\nCreate the server socket file descriptor, usually created with with `PF_INET`, `SOCK_STREAM`, and with tcp `IPPROTO_TCP`.\n\n```\nint server_socket_fd;\nif ( (server_socket_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) \u003c= SOCKET_ERROR ) {\n  perror(\"Error creating the server socket\");\n}\n```\n\nSet the socket options, passing in the newly created socket file descriptor.  Additionally,\n\n```\nint sockopt;\nint socket_opts;\nif ( (socket_opts = setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, \u0026sockopt, sizeof(int))) \u003c= SOCKET_ERROR ) {\n  perror(\"Error setting the socket options\");\n}\n```\n\nNow we need to set up the socket address:\n\n```\n// Create ip address\nin_addr_t ip_address;\nip_address = inet_addr();\n\nstruct sockaddr_in socket_address;\n// Allocate some memory for the socket_address\nmemset(\u0026socket_address, 0, sizeof(socket_address));\nsocket_address.sin_family = AF_INET;\nsocket_address.sin_addr.s_addr = ip_address;\nsocket_address.sin_port = htons(PORT);\n```\n\nBind the server\n\n```\nint server;\nif ( (server = bind(server_socket_fd, (struct sockaddr *) \u0026socket_address, sizeof(socket_address))) \u003c= SOCKET_ERROR ) {\n  perror(\"Error binding the server\");\n}\n```\n\nListen for incoming connections\n\n```\nint connection_listener;\nif ( (connection_listener = listen(server_socket_fd, MAX_PENDING_CONNECTIONS)) \u003c= SOCKET_ERROR ) {\n  perror(\"Error connecting the listener\");\n}\n```\n\nNow let's run the server by running an infinite loop\n\n```\nfor (;;) {\n  int client;\n  struct sockaddr_in client_address;\n  socklen_t client_len;\n\n  client_len = sizeof(client_address);\n\n  // Accept incoming connections from the client\n  if ( (client = accept(server_socket_fd, (struct sockaddr *) \u0026client_address, \u0026client_len)) \u003c= SOCKET_ERROR ) {\n    perror(\"Error accepting on the server socket\");\n  }\n\n  // Receive the message\n  int receiver;\n  if ( (receiver = recv(client, buffer, BUFSIZE - 1, 0)) \u003c= SOCKET_ERROR ) {\n    perror(\"Error receiving message from the client);\n  }\n\n  // Respond\n  ssize_t sender;\n  char response[BUFSIZE];\n  size_t len;\n\n  strcpy(response, \"This is a response string!\");\n  len = strlen(response);\n\n  if ( (sender = send(client, response, len, 0)) \u003c= SOCKET_ERROR ) {\n    perror(\"Error sending the response\");\n  }\n  fprintf(stdout, \"Successfully sent (%i) message\", sender);\n}\n```\n\nGo ahead, build it, and fire it up and send a curl request.  In a future post, I'll build an HTTP server.\n\nThe source code for this can be found here: \u003chttps://github.com/ralucas/basic_c_socket_server\u003e\n\nI also suggest taking a look at [Beej's Guide to Networking](http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralucas%2Fbasic_c_socket_server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fralucas%2Fbasic_c_socket_server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralucas%2Fbasic_c_socket_server/lists"}