Re-build Wheels-linux-socket

Source: Internet
Author: User
Tags htons

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.