Created
February 21, 2015 20:19
-
-
Save nickmonad/f07dabe8167183949e6e to your computer and use it in GitHub Desktop.
TCP Boilerplate
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
#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> | |
#define SERVER_PORT 27070 | |
int main(void) | |
{ | |
// Configure socket address | |
struct sockaddr_in sa; | |
memset(&sa, 0, sizeof(sa)); | |
sa.sin_family = AF_INET; | |
sa.sin_port = htons(SERVER_PORT); | |
inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr); | |
// Obtain socket | |
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (-1 == sock) { | |
perror("tcp client - could not obtain socket"); | |
exit(EXIT_FAILURE); | |
} | |
if (-1 == connect(sock, (struct sockaddr *)&sa, sizeof(sa))) { | |
perror("tcp client - connect failed"); | |
close(sock); | |
exit(EXIT_FAILURE); | |
} | |
// data processing | |
// read(), write(), etc | |
// cleanup | |
shutdown(sock, SHUT_RDWR); | |
close(sock); | |
return 0; | |
} |
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
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#define SERVER_PORT 27070 | |
int main(void) | |
{ | |
// Server configuration | |
struct sockaddr_in server; | |
memset(&server, 0, sizeof(server)); | |
server.sin_family = AF_INET; | |
server.sin_port = htons(SERVER_PORT); | |
server.sin_addr.s_addr = htonl(INADDR_ANY); | |
// Start the try/check dance | |
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (-1 == sock) { | |
perror("tcp server - could not create socket"); | |
exit(EXIT_FAILURE); | |
} | |
if (-1 == bind(sock, (struct sockaddr *)&server, sizeof(server))) { | |
perror("tcp server - bind failed"); | |
close(sock); | |
exit(EXIT_FAILURE); | |
} | |
if (-1 == listen(sock, 10)) { | |
perror("tcp server - listen failed"); | |
close(sock); | |
exit(EXIT_FAILURE); | |
} | |
// Client connection information | |
struct sockaddr_in client; | |
memset(&client, 0, sizeof(client)); | |
socklen_t fromlen = sizeof(client); | |
while (1) { | |
int connection = accept(sock, (struct sockaddr *)&client, &fromlen); | |
if (connection < 0) { | |
perror("tcp server - accept failed"); | |
close(sock); | |
exit(EXIT_FAILURE); | |
} | |
// data processing | |
// read(), write(), etc using connection socket above | |
if (-1 == shutdown(connection, SHUT_RDWR)) { | |
perror("tcp server - could not shutdown socket"); | |
close(connection); | |
close(sock); | |
exit(EXIT_FAILURE); | |
} | |
close(connection); | |
} | |
close(sock); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment