#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <time.h># Include <arpa/inet.h> #define buffer_size 255#define server_port 13579#define ligen_of_listen_queue 5int main (INT&NBSP;ARGS,&NBSP;CHAR**&NBSP;ARGV) {//init socketint servfd,clifd;struct sockaddr_in server_addr,client_addr;if (Servfd = socket (AF_INET, sock_stream,0) < 0) {printf ("socket create error!\n"); exit (1);} Server_addr.sin_family = af_inet;server_addr.sin_port = htons (SERVER_PORT); server_addr.sin _addr.s_addr = htons (Inaddr_any); if (Bind (servfd, (struct sockaddr*) &server_addr,sizeof ( SERVER_ADDR) < 0) {printf ("bind to port %d\n failure!", SERVER_ PORT); exit (1);} if (Listen (Servfd, ligen_of_listen_queue) < 0) {printf ("create listen failure!\n"); exit (1);} while (1) {char buf[buffer_size];long timestamp;socklen_t length = sizeof (client_ addr); Clifd = accept (servfd, (struct sockaddr*) &client_addr,&length); if (clifd < 0) {printf ("accept error !\n"); close and print messageprintf ("from client ip:%s,port:%d\n", inet_ntoa (Client_ ADDR.SIN_ADDR), Ntohs (Client_addr.sin_port)); Timestamp = time (NULL);//strcpy (buf, "hello! i Am shaoyongyang! "); Send (CLIFD, "Hello world", buffer_size,0); close (CLIFD);} printf ("hello world\n");}
Compiling the environment
[Email protected]:~/public/xtranfer$ gcc--versiongcc (Ubuntu/linaro 4.6.3-1ubuntu5) 4.6.3Copyright (C) free Software Foundation, inc.this is free software; See the source for copying conditions. There is nowarranty; Not even to merchantability or FITNESS for A particular PURPOSE.
Re-build Wheels-linux-socket