/* ============================================================================ Name : TestServer.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/in.h> #include <string.h> int main(void) { int sfp, nfp; int sin_size; struct sockaddr_in s_add, c_add; int port = 8888; sfp = socket(AF_INET, SOCK_STREAM, 0); if (sfp == -1) { printf("socket build fail\r\n"); return -1; } printf("socket ok!\r\n"); bzero(&s_add, sizeof(struct sockaddr_in)); s_add.sin_family = AF_INET; s_add.sin_addr.s_addr = htonl(INADDR_ANY); s_add.sin_port = htons(port); if (-1 == bind(sfp, (struct sockaddr *) (&s_add), sizeof(struct sockaddr))) { printf("bind error\r\n"); return -1; } printf("bind ok\r\n"); if (-1 == listen(sfp, 5)) { printf("listen fail ! \r\n"); return -1; } while (1) { sin_size = sizeof(struct sockaddr_in); nfp = accept(sfp, (struct sockaddr *) (&c_add), &sin_size); if (-1 == nfp) { printf("accept error \r\n"); return -1; } printf("accept ok \r\n"); write(nfp,"hello,welcome to my server \r\n", 32); } return EXIT_SUCCESS; }
Socket Server By C
猜你喜欢
转载自myten.iteye.com/blog/2336105
今日推荐
周排行