Last active
August 29, 2015 14:03
-
-
Save oxUnd/d5e53c83b83c71954675 to your computer and use it in GitHub Desktop.
TCP socket simple sample.
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
// | |
// main.c | |
// os_base_socket | |
// | |
// Created by xiangshouding on 14-7-13. | |
// Copyright (c) 2014 xiangshouding. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
/** | |
* define `strerror` | |
*/ | |
#include <string.h> | |
/** | |
* define `ap_start` and `ap_end` | |
*/ | |
#include <stdarg.h> | |
#include <unistd.h> | |
#include <errno.h> | |
/** | |
* define `socket` `bind` | |
*/ | |
#include <sys/socket.h> | |
/** | |
* define `sockaddr` | |
*/ | |
#include <netinet/in.h> | |
/** | |
* define `htons` and `htonl` | |
*/ | |
#include <arpa/inet.h> | |
/** | |
* define `gethostbyaddr` | |
*/ | |
#include <netdb.h> | |
void catch_error(int exit_code, const char *format, ...) { | |
char *buf; | |
va_list ap; | |
va_start(ap, format); | |
vasprintf(&buf, format, ap); | |
fprintf(stderr, "%s errno: %d errmsg: %s\n", buf, errno, strerror(errno)); | |
va_end(ap); | |
exit(exit_code); | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
int socket_fd; | |
socklen_t len; | |
struct in_addr in_addr; | |
struct sockaddr_in server_addr; | |
struct sockaddr_in client_addr; | |
struct hostent *client_hostent; | |
int client_len; | |
int conn_fd; | |
//init | |
bzero((char *) & server_addr, sizeof(server_addr)); | |
in_addr.s_addr = htonl(INADDR_ANY); //listen any address. like is `0.0.0.0` | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_port = htons(4000); //port | |
server_addr.sin_addr = in_addr; | |
len = sizeof(server_addr); | |
/** | |
* AF_INET AF = address family, INET -> ip4 | |
* SOCK_STREAM -> TCP | |
*/ | |
socket_fd = socket(AF_INET, SOCK_STREAM, 0); | |
if (-1 == socket_fd) { | |
catch_error(1, "create socket fail: %d", socket_fd); | |
} | |
if (-1 == (bind(socket_fd, (const struct sockaddr *)&server_addr, len))) { | |
close(socket_fd); | |
catch_error(1, "bind address fail."); | |
} | |
if (-1 == (listen(socket_fd, 128))) { | |
close(socket_fd); | |
catch_error(1, "listen fail."); | |
} | |
for(;;) { | |
client_len = sizeof(client_addr); | |
if (-1 == (conn_fd = accept(socket_fd, (struct sockaddr *) &client_addr, (socklen_t *)&client_len))) { | |
close(socket_fd); | |
catch_error(1, "accpet fail"); | |
} | |
client_hostent = gethostbyaddr((const char *)&client_addr.sin_addr.s_addr, sizeof(client_addr.sin_addr.s_addr), AF_INET); | |
printf("conn %s %s\n", inet_ntoa(client_addr.sin_addr), client_hostent->h_name); | |
write(conn_fd, "test\n", 6); | |
close(conn_fd); | |
} | |
close(socket_fd); | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment