1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. static bool stop = false;
  12. static void handle_term( int sig ) // kill pid; in another tty will triggle this signal
  13. {
  14. stop = true;
  15. printf("signal SIGTERM catched...\n");
  16. }
  17.  
  18. static void handle_int(int sig) // ctrl+c; will triggle this signal
  19. {
  20. printf("signal SIGINT catched...\n");
  21. stop = true;
  22. }
  23.  
  24. //./listen 127.0.0.1 8888 100
  25.  
  26. int main( int argc, char* argv[] )
  27. {
  28. signal( SIGTERM, handle_term );
  29. signal(SIGINT, handle_int);
  30.  
  31. if( argc <= 3 )
  32. {
  33. printf( "usage: %s ip_address port_number backlog\n", basename( argv[0] ) );
  34. return 1;
  35. }
  36. const char* ip = argv[1];
  37. int port = atoi( argv[2] );
  38. int backlog = atoi( argv[3] );
  39.  
  40. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  41. assert( sock >= 0 );
  42.  
  43. struct sockaddr_in address;
  44. bzero( &address, sizeof( address ) );
  45. address.sin_family = AF_INET;
  46. inet_pton( AF_INET, ip, &address.sin_addr );
  47. address.sin_port = htons( port );
  48.  
  49. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  50. assert( ret != -1 );
  51.  
  52. printf("after bind...\n");
  53.  
  54. //backlog is the max number of waitting connect in wait queue
  55. ret = listen( sock, backlog ); //listen is a none-block function
  56. assert( ret != -1 );
  57. printf("after listen...\n");
  58.  
  59. while ( ! stop )
  60. {
  61. sleep( 1 );
  62. }
  63.  
  64. close( sock );
  65. return 0;
  66. }

  

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. int main( int argc, char* argv[] )
  12. {
  13. if( argc <= 2 )
  14. {
  15. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  16. return 1;
  17. }
  18. const char* ip = argv[1];
  19. int port = atoi( argv[2] );
  20.  
  21. struct sockaddr_in address;
  22. bzero( &address, sizeof( address ) );
  23. address.sin_family = AF_INET;
  24. inet_pton( AF_INET, ip, &address.sin_addr );
  25. address.sin_port = htons( port );
  26.  
  27. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  28. assert( sock >= 0 );
  29.  
  30. int reuse = 1;
  31. setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) );
  32.  
  33. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  34. assert( ret != -1 );
  35. printf("AFTER bind...\n");
  36.  
  37. ret = listen( sock, 5 );
  38. assert( ret != -1 );
  39. printf("AFTER listen...\n");
  40.  
  41. //the returned client is client's address
  42. struct sockaddr_in client;
  43. socklen_t client_addrlength = sizeof( client );
  44. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); //accept is a block function
  45. printf("AFTER accept...\n");
  46.  
  47. if ( connfd < 0 )
  48. {
  49. printf( "errno is: %d\n", errno );
  50. }
  51. else
  52. {
  53. //#define INET_ADDRSTRLEN 16 , IPV4 address char array length, <netinet/in.h>
  54. char remote[INET_ADDRSTRLEN ];
  55. printf( "connected with ip: %s and port: %d\n",
  56. inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) );
  57. close( connfd );
  58. }
  59.  
  60. close( sock );
  61. return 0;
  62. }

  

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. #define BUF_SIZE 1024
  12.  
  13. int main( int argc, char* argv[] )
  14. {
  15. if( argc <= 2 )
  16. {
  17. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  18. return 1;
  19. }
  20. const char* ip = argv[1];
  21. int port = atoi( argv[2] );
  22.  
  23. struct sockaddr_in address;
  24. bzero( &address, sizeof( address ) );
  25. address.sin_family = AF_INET;
  26. inet_pton( AF_INET, ip, &address.sin_addr );
  27. address.sin_port = htons( port );
  28.  
  29. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  30. assert( sock >= 0 );
  31.  
  32. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  33. assert( ret != -1 );
  34.  
  35. ret = listen( sock, 5 );
  36. assert( ret != -1 );
  37. printf("after listen...\n");
  38.  
  39. struct sockaddr_in client;
  40. socklen_t client_addrlength = sizeof( client );
  41. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  42. printf("after accept...\n");
  43. if ( connfd < 0 )
  44. {
  45. printf( "errno is: %d\n", errno );
  46. }
  47. else
  48. {
  49. char buffer[ BUF_SIZE ];
  50.  
  51. memset( buffer, '\0', BUF_SIZE );
  52. ret = recv( connfd, buffer, BUF_SIZE-1, 0 );
  53. printf( "got %d bytes of normal data '%s'\n", ret, buffer );
  54.  
  55. memset( buffer, '\0', BUF_SIZE );
  56. //MSG_OOB: support recv out-of-band data
  57. //Only TCP support oob data
  58. //TCP only 1 byte oob data
  59. //use MSG_OOB flag when call send to send oob data
  60. ret = recv( connfd, buffer, BUF_SIZE-1, MSG_OOB );
  61. printf( "got %d bytes of oob data '%s'\n", ret, buffer );
  62.  
  63. memset( buffer, '\0', BUF_SIZE );
  64. ret = recv( connfd, buffer, BUF_SIZE-1, 0 );
  65. printf( "got %d bytes of normal data '%s'\n", ret, buffer );
  66.  
  67. close( connfd );
  68. }
  69.  
  70. close( sock );
  71. return 0;
  72. }

connect

  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #define BUFFER_SIZE 512
  10.  
  11. int main( int argc, char* argv[] )
  12. {
  13. if( argc <= 3 )
  14. {
  15. printf( "usage: %s ip_address port_number send_bufer_size\n", basename( argv[0] ) );
  16. return 1;
  17. }
  18. const char* ip = argv[1];
  19. int port = atoi( argv[2] );
  20.  
  21. struct sockaddr_in server_address;
  22. bzero( &server_address, sizeof( server_address ) );
  23. server_address.sin_family = AF_INET;
  24. inet_pton( AF_INET, ip, &server_address.sin_addr );
  25. server_address.sin_port = htons( port );
  26.  
  27. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  28. assert( sock >= 0 );
  29.  
  30. int sendbuf = atoi( argv[3] );
  31. int len = sizeof( sendbuf );
  32. setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) );
  33. getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len );
  34. printf( "the tcp send buffer size after setting is %d\n", sendbuf );
  35.  
  36. if ( connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ) != -1 )
  37. {
  38. //
  39. printf("call getsockname ...\n");
  40. struct sockaddr_in local_address;
  41. socklen_t length;
  42. int ret = getpeername(sock, ( struct sockaddr* )&local_address, &length);
  43. assert(ret == 0);
  44. char local[INET_ADDRSTRLEN ];
  45. printf( "local with ip: %s and port: %d\n",
  46. inet_ntop( AF_INET, &local_address.sin_addr, local, INET_ADDRSTRLEN ), ntohs( local_address.sin_port ) );
  47. //
  48.  
  49. char buffer[ BUFFER_SIZE ];
  50. memset( buffer, 'a', BUFFER_SIZE );
  51. send( sock, buffer, BUFFER_SIZE, 0 );
  52. }
  53.  
  54. close( sock );
  55. return 0;
  56. }

  

accept:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. int main( int argc, char* argv[] )
  12. {
  13. if( argc <= 2 )
  14. {
  15. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  16. return 1;
  17. }
  18. const char* ip = argv[1];
  19. int port = atoi( argv[2] );
  20.  
  21. struct sockaddr_in address;
  22. bzero( &address, sizeof( address ) );
  23. address.sin_family = AF_INET;
  24. inet_pton( AF_INET, ip, &address.sin_addr );
  25. address.sin_port = htons( port );
  26.  
  27. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  28. assert( sock >= 0 );
  29.  
  30. int reuse = 1;
  31. setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) );
  32.  
  33. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  34. assert( ret != -1 );
  35. printf("AFTER bind...\n");
  36.  
  37. ret = listen( sock, 5 );
  38. assert( ret != -1 );
  39. printf("AFTER listen...\n");
  40.  
  41. //the returned client is client's address
  42. struct sockaddr_in client;
  43. socklen_t client_addrlength = sizeof( client );
  44. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); //accept is a block function
  45. printf("AFTER accept...\n");
  46.  
  47. if ( connfd < 0 )
  48. {
  49. printf( "errno is: %d\n", errno );
  50. }
  51. else
  52. {
  53. //#define INET_ADDRSTRLEN 16 , IPV4 address char array length, <netinet/in.h>
  54. char remote[INET_ADDRSTRLEN ];
  55. printf( "connected with ip: %s and port: %d\n",
  56. inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) );
  57.  
  58. //
  59. printf("call getsockname ...\n");
  60. struct sockaddr_in local_address;
  61. socklen_t length;
  62. int ret = getsockname(connfd, ( struct sockaddr* )&local_address, &length);
  63. if (ret == 0)
  64. {
  65. char local[INET_ADDRSTRLEN ];
  66. printf( "local connfd ip: %s and port: %d\n", inet_ntop( AF_INET, &local_address.sin_addr, local, INET_ADDRSTRLEN ), ntohs( local_address.sin_port ) );
  67. }
  68. else
  69. printf("getsockname on connfd fail...\n");
  70.  
  71. bzero( &local_address, sizeof( local_address ) );
  72. ret = getpeername(connfd, ( struct sockaddr* )&local_address, &length);
  73. if (ret == 0)
  74. {
  75. char local1[INET_ADDRSTRLEN ];
  76. printf( "remote ip: %s and port: %d\n", inet_ntop( AF_INET, &local_address.sin_addr, local1, INET_ADDRSTRLEN ), ntohs( local_address.sin_port ) );
  77.  
  78. }
  79. else
  80. printf("getpeername on connfd fail...\n");
  81.  
  82. close( connfd );
  83. }
  84.  
  85. close( sock );
  86. return 0;
  87. }

  

setsendbuffer:

  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #define BUFFER_SIZE 512
  10.  
  11. int main( int argc, char* argv[] )
  12. {
  13. if( argc <= 3 )
  14. {
  15. printf( "usage: %s ip_address port_number send_bufer_size\n", basename( argv[0] ) );
  16. return 1;
  17. }
  18. const char* ip = argv[1];
  19. int port = atoi( argv[2] );
  20.  
  21. struct sockaddr_in server_address;
  22. bzero( &server_address, sizeof( server_address ) );
  23. server_address.sin_family = AF_INET;
  24. inet_pton( AF_INET, ip, &server_address.sin_addr );
  25. server_address.sin_port = htons( port );
  26.  
  27. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  28. assert( sock >= 0 );
  29.  
  30. int sendbuf = atoi( argv[3] );
  31. int len = sizeof( sendbuf );
  32. setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) );
  33. getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len );
  34. printf( "the tcp send buffer size after setting is %d\n", sendbuf );
  35.  
  36. if ( connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ) != -1 )
  37. {
  38. char buffer[ BUFFER_SIZE ];
  39. memset( buffer, 'a', BUFFER_SIZE );
  40. send( sock, buffer, BUFFER_SIZE, 0 );
  41. }
  42.  
  43. close( sock );
  44. return 0;
  45. }

  

setrecvbuffer:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. #define BUFFER_SIZE 1024
  12.  
  13. int main( int argc, char* argv[] )
  14. {
  15. if( argc <= 3 )
  16. {
  17. printf( "usage: %s ip_address port_number receive_buffer_size\n", basename( argv[0] ) );
  18. return 1;
  19. }
  20. const char* ip = argv[1];
  21. int port = atoi( argv[2] );
  22.  
  23. struct sockaddr_in address;
  24. bzero( &address, sizeof( address ) );
  25. address.sin_family = AF_INET;
  26. inet_pton( AF_INET, ip, &address.sin_addr );
  27. address.sin_port = htons( port );
  28.  
  29. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  30. assert( sock >= 0 );
  31. int recvbuf = atoi( argv[3] );
  32. printf("recvbuf is %d\n", recvbuf); /////!!!
  33. int len = sizeof( recvbuf );
  34. printf("len is %d\n", len); /////!!!
  35. setsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, sizeof( recvbuf ) );
  36. getsockopt( sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, ( socklen_t* )&len );
  37. printf( "the receive buffer size after settting is %d\n", recvbuf );
  38.  
  39. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  40. assert( ret != -1 );
  41.  
  42. ret = listen( sock, 5 );
  43. assert( ret != -1 );
  44.  
  45. struct sockaddr_in client;
  46. socklen_t client_addrlength = sizeof( client );
  47. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  48. if ( connfd < 0 )
  49. {
  50. printf( "errno is: %d\n", errno );
  51. }
  52. else
  53. {
  54. char buffer[ BUFFER_SIZE ];
  55. memset( buffer, '\0', BUFFER_SIZE );
  56. while( recv( connfd, buffer, BUFFER_SIZE-1, 0 ) > 0 )
  57. {
  58. printf("%s\n", buffer);
  59. }
  60. close( connfd );
  61. }
  62.  
  63. close( sock );
  64. return 0;
  65. }

  

daytime:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <assert.h>
  8. #include <errno.h>
  9.  
  10. int main( int argc, char *argv[] )
  11. {
  12. //assert( argc == 2 );
  13. //char *host = argv[1];
  14.  
  15. struct hostent *host; //存放主机信息
  16. //char addr_p[NET_ADDR_STR_LEN]; //用于存放点分十进制IP地址的字符串
  17. if((host = gethostent()) == NULL)
  18. {
  19. perror("fail to get host's information\n");
  20. return -1;
  21. }
  22. printf("hostName: %s\n" , host->h_name);
  23.  
  24. //用域名或主机名获取IP地址
  25. struct hostent* hostinfo = gethostbyname( host->h_name );
  26. assert( hostinfo );
  27. struct servent* servinfo = getservbyname( "daytime", "tcp" );
  28. assert( servinfo );
  29. printf( "daytime port is %d\n", ntohs( servinfo->s_port ) );
  30.  
  31. struct sockaddr_in address;
  32. address.sin_family = AF_INET;
  33. address.sin_port = servinfo->s_port;
  34. address.sin_addr = *( struct in_addr* )*hostinfo->h_addr_list;
  35.  
  36. char remote[INET_ADDRSTRLEN ];
  37. printf( "connected with ip: %s and port: %d\n",
  38. inet_ntop( AF_INET, &address.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( address.sin_port ) );
  39.  
  40. int sockfd = socket( AF_INET, SOCK_STREAM, 0 );
  41. //int reuse = 1;
  42. //setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) );
  43. int result = connect( sockfd, (struct sockaddr* )&address, sizeof( address ) );
  44. printf( "errno is: %d\n", errno );
  45. perror("connect error:");
  46. assert( result != -1 );
  47.  
  48. char buffer[128];
  49. result = read( sockfd, buffer, sizeof( buffer ) );
  50. assert( result > 0 );
  51. buffer[ result ] = '\0';
  52. printf( "the day tiem is: %s", buffer );
  53. close( sockfd );
  54. return 0;
  55. }

  

testdup:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. int main( int argc, char* argv[] )
  12. {
  13. if( argc <= 2 )
  14. {
  15. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  16. return 1;
  17. }
  18. const char* ip = argv[1];
  19. int port = atoi( argv[2] );
  20.  
  21. struct sockaddr_in address;
  22. bzero( &address, sizeof( address ) );
  23. address.sin_family = AF_INET;
  24. inet_pton( AF_INET, ip, &address.sin_addr );
  25. address.sin_port = htons( port );
  26.  
  27. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  28. assert( sock >= 0 );
  29.  
  30. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  31. assert( ret != -1 );
  32.  
  33. ret = listen( sock, 5 );
  34. assert( ret != -1 );
  35.  
  36. struct sockaddr_in client;
  37. socklen_t client_addrlength = sizeof( client );
  38. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  39. if ( connfd < 0 )
  40. {
  41. printf( "errno is: %d\n", errno );
  42. }
  43. else
  44. {
  45. close( STDOUT_FILENO );
  46. dup( connfd );
  47. printf( "abcd\n" ); //the same as write/send data in connfd
  48. close( connfd );
  49. }
  50.  
  51. close( sock );
  52. return 0;
  53. }

  

writev:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <fcntl.h>
  13.  
  14. #define BUFFER_SIZE 1024
  15. static const char* status_line[2] = { "200 OK", "500 Internal server error" };
  16.  
  17. int main( int argc, char* argv[] )
  18. {
  19. if( argc <= 3 )
  20. {
  21. printf( "usage: %s ip_address port_number filename\n", basename( argv[0] ) );
  22. return 1;
  23. }
  24. const char* ip = argv[1];
  25. int port = atoi( argv[2] );
  26. const char* file_name = argv[3];
  27.  
  28. struct sockaddr_in address;
  29. bzero( &address, sizeof( address ) );
  30. address.sin_family = AF_INET;
  31. inet_pton( AF_INET, ip, &address.sin_addr );
  32. address.sin_port = htons( port );
  33.  
  34. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  35. assert( sock >= 0 );
  36.  
  37. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  38. assert( ret != -1 );
  39.  
  40. ret = listen( sock, 5 );
  41. assert( ret != -1 );
  42.  
  43. struct sockaddr_in client;
  44. socklen_t client_addrlength = sizeof( client );
  45. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  46. if ( connfd < 0 )
  47. {
  48. printf( "errno is: %d\n", errno );
  49. }
  50. else
  51. {
  52. char header_buf[ BUFFER_SIZE ];
  53. memset( header_buf, '\0', BUFFER_SIZE );
  54. char* file_buf;
  55. struct stat file_stat;
  56. bool valid = true;
  57. int len = 0;
  58. if( stat( file_name, &file_stat ) < 0 )
  59. {
  60. valid = false;
  61. }
  62. else
  63. {
  64. if( S_ISDIR( file_stat.st_mode ) )
  65. {
  66. valid = false;
  67. }
  68. else if( file_stat.st_mode & S_IROTH )
  69. {
  70. int fd = open( file_name, O_RDONLY );
  71. file_buf = new char[ file_stat.st_size + 1 ];
  72. memset( file_buf, '\0', file_stat.st_size + 1 );
  73. if ( read( fd, file_buf, file_stat.st_size ) < 0 )
  74. {
  75. valid = false;
  76. }
  77. }
  78. else
  79. {
  80. valid = false;
  81. }
  82. }
  83.  
  84. if( valid )
  85. {
  86. ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[0] );
  87. len += ret;
  88. ret = snprintf( header_buf + len, BUFFER_SIZE-1-len,
  89. "Content-Length: %d\r\n", (int)file_stat.st_size );
  90. len += ret;
  91. ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" );
  92. struct iovec iv[2];
  93. iv[ 0 ].iov_base = header_buf;
  94. iv[ 0 ].iov_len = strlen( header_buf );
  95. iv[ 1 ].iov_base = file_buf;
  96. iv[ 1 ].iov_len = file_stat.st_size;
  97. ret = writev( connfd, iv, 2 );
  98. }
  99. else
  100. {
  101. ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[1] );
  102. len += ret;
  103. ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" );
  104. send( connfd, header_buf, strlen( header_buf ), 0 );
  105. }
  106. close( connfd );
  107. delete [] file_buf;
  108. }
  109.  
  110. close( sock );
  111. return 0;
  112. }

  

sendfile:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <sys/sendfile.h>
  14.  
  15. int main( int argc, char* argv[] )
  16. {
  17. if( argc <= 3 )
  18. {
  19. printf( "usage: %s ip_address port_number filename\n", basename( argv[0] ) );
  20. return 1;
  21. }
  22. const char* ip = argv[1];
  23. int port = atoi( argv[2] );
  24. const char* file_name = argv[3];
  25.  
  26. int filefd = open( file_name, O_RDONLY );
  27. assert( filefd > 0 );
  28. struct stat stat_buf;
  29. fstat( filefd, &stat_buf );
  30.  
  31. struct sockaddr_in address;
  32. bzero( &address, sizeof( address ) );
  33. address.sin_family = AF_INET;
  34. inet_pton( AF_INET, ip, &address.sin_addr );
  35. address.sin_port = htons( port );
  36.  
  37. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  38. assert( sock >= 0 );
  39.  
  40. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  41. assert( ret != -1 );
  42.  
  43. ret = listen( sock, 5 );
  44. assert( ret != -1 );
  45.  
  46. struct sockaddr_in client;
  47. socklen_t client_addrlength = sizeof( client );
  48. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  49. if ( connfd < 0 )
  50. {
  51. printf( "errno is: %d\n", errno );
  52. }
  53. else
  54. {
  55. //linux, not GNU. high performance send a file, 'zero copy'
  56. sendfile( connfd, filefd, NULL, stat_buf.st_size );
  57. close( connfd );
  58. }
  59.  
  60. close( sock );
  61. return 0;
  62. }

  

splice:

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11.  
  12. int main( int argc, char* argv[] )
  13. {
  14. if( argc <= 2 )
  15. {
  16. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  17. return 1;
  18. }
  19. const char* ip = argv[1];
  20. int port = atoi( argv[2] );
  21.  
  22. struct sockaddr_in address;
  23. bzero( &address, sizeof( address ) );
  24. address.sin_family = AF_INET;
  25. inet_pton( AF_INET, ip, &address.sin_addr );
  26. address.sin_port = htons( port );
  27.  
  28. int sock = socket( PF_INET, SOCK_STREAM, 0 );
  29. assert( sock >= 0 );
  30.  
  31. int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
  32. assert( ret != -1 );
  33.  
  34. ret = listen( sock, 5 );
  35. assert( ret != -1 );
  36.  
  37. struct sockaddr_in client;
  38. socklen_t client_addrlength = sizeof( client );
  39. int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
  40. if ( connfd < 0 )
  41. {
  42. printf( "errno is: %d\n", errno );
  43. }
  44. else
  45. {
  46. int pipefd[2];
  47. assert( ret != -1 );
  48. ret = pipe( pipefd ); //pipe, pipefd[0] is opened for read, pipefd[1] is opened for write.
  49. //splice用于在两个文件描述符之间移动数据, 也是零拷贝。使用splice时, fd_in和fd_out中必须至少有一个是管道文件描述符。
  50. //sendfile只适用于将数据从文件拷贝到套接字上,限定了它的使用范围。Linux在2.6.17版本引入splice系统调用,
  51. //用于在两个文件描述符中移动数据.
  52.  
  53. //an ECHO implement use splice.
  54. //copy data from connfd(recv from client) to pipefd[1]
  55. ret = splice( connfd, NULL, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
  56. assert( ret != -1 );
  57. //copy data from pipefd[1] -->pipefd[0] --> connfd, send to client.
  58. ret = splice( pipefd[0], NULL, connfd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
  59. assert( ret != -1 );
  60. close( connfd );
  61. }
  62.  
  63. close( sock );
  64. return 0;
  65. }

  

tee:

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7.  
  8. int main( int argc, char* argv[] )
  9. {
  10. if ( argc != 2 )
  11. {
  12. printf( "usage: %s <file>\n", argv[0] );
  13. return 1;
  14. }
  15. int filefd = open( argv[1], O_CREAT | O_WRONLY | O_TRUNC, 0666 );
  16. assert( filefd > 0 );
  17.  
  18. int pipefd_stdout[2];
  19. int ret = pipe( pipefd_stdout );
  20. assert( ret != -1 );
  21.  
  22. int pipefd_file[2];
  23. ret = pipe( pipefd_file );
  24. assert( ret != -1 );
  25.  
  26. //close( STDIN_FILENO );
  27. // dup2( pipefd_stdout[1], STDIN_FILENO );
  28. //write( pipefd_stdout[1], "abc\n", 4 );
  29.  
  30. //copy data: stdin --> pipefd_stdout[1]
  31. ret = splice( STDIN_FILENO, NULL, pipefd_stdout[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
  32. assert( ret != -1 );
  33.  
  34. //tee在两个管道文件描述符之间复制数据,同是零拷贝。但它不消耗数据,数据被操作之后,仍然可以用于后续操作。
  35. //copy data: pipefd_stdout[0] --> pipefd_file[1]
  36. ret = tee( pipefd_stdout[0], pipefd_file[1], 32768, SPLICE_F_NONBLOCK );
  37. assert( ret != -1 );
  38.  
  39. //copy data: pipefd_file[0] --> filefd
  40. ret = splice( pipefd_file[0], NULL, filefd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
  41. assert( ret != -1 );
  42.  
  43. //copy data: pipefd_stdout[0] --> stdout
  44. ret = splice( pipefd_stdout[0], NULL, STDOUT_FILENO, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
  45. assert( ret != -1 );
  46.  
  47. close( filefd );
  48. close( pipefd_stdout[0] );
  49. close( pipefd_stdout[1] );
  50. close( pipefd_file[0] );
  51. close( pipefd_file[1] );
  52. return 0;
  53. }

  

select:

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12.  
  13. int main( int argc, char* argv[] )
  14. {
  15. if( argc <= 2 )
  16. {
  17. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  18. return 1;
  19. }
  20. const char* ip = argv[1];
  21. int port = atoi( argv[2] );
  22. printf( "ip is %s and port is %d\n", ip, port );
  23.  
  24. int ret = 0;
  25. struct sockaddr_in address;
  26. bzero( &address, sizeof( address ) );
  27. address.sin_family = AF_INET;
  28. inet_pton( AF_INET, ip, &address.sin_addr );
  29. address.sin_port = htons( port );
  30.  
  31. int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
  32. assert( listenfd >= 0 );
  33.  
  34. ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
  35. assert( ret != -1 );
  36.  
  37. ret = listen( listenfd, 5 );
  38. assert( ret != -1 );
  39.  
  40. struct sockaddr_in client_address;
  41. socklen_t client_addrlength = sizeof( client_address );
  42. int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
  43. if ( connfd < 0 )
  44. {
  45. printf( "errno is: %d\n", errno );
  46. close( listenfd );
  47. }
  48.  
  49. char remote_addr[INET_ADDRSTRLEN];
  50. printf( "connected with ip: %s and port: %d\n", inet_ntop( AF_INET, &client_address.sin_addr, remote_addr, INET_ADDRSTRLEN ), ntohs( client_address.sin_port ) );
  51.  
  52. char buf[1024];
  53. fd_set read_fds;
  54. fd_set exception_fds;
  55.  
  56. FD_ZERO( &read_fds );
  57. FD_ZERO( &exception_fds );
  58.  
  59. // option:SO_OOBINLINE, value:1 --- put out of band data into normal data.
  60. int nReuseAddr = 1;
  61. setsockopt( connfd, SOL_SOCKET, SO_OOBINLINE, &nReuseAddr, sizeof( nReuseAddr ) );
  62. while( 1 )
  63. {
  64. memset( buf, '\0', sizeof( buf ) );
  65. FD_SET( connfd, &read_fds );
  66. FD_SET( connfd, &exception_fds );
  67.  
  68. ret = select( connfd + 1, &read_fds, NULL, &exception_fds, NULL );
  69. printf( "select one\n" );
  70. if ( ret < 0 )
  71. {
  72. printf( "selection failure\n" );
  73. break;
  74. }
  75.  
  76. if ( FD_ISSET( connfd, &read_fds ) )
  77. {
  78. ret = recv( connfd, buf, sizeof( buf )-1, 0 );
  79. if( ret <= 0 )
  80. {
  81. break;
  82. }
  83. printf( "get %d bytes of normal data: %s\n", ret, buf );
  84. }
  85. else if( FD_ISSET( connfd, &exception_fds ) )
  86. {
  87. ret = recv( connfd, buf, sizeof( buf )-1, MSG_OOB );
  88. if( ret <= 0 )
  89. {
  90. break;
  91. }
  92. printf( "get %d bytes of oob data: %s\n", ret, buf );
  93. }
  94.  
  95. }
  96.  
  97. close( connfd );
  98. close( listenfd );
  99. return 0;
  100. }

  

epoll:

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include <sys/epoll.h>
  13. #include <pthread.h>
  14.  
  15. #define MAX_EVENT_NUMBER 1024
  16. #define BUFFER_SIZE 10
  17.  
  18. int setnonblocking( int fd )
  19. {
  20. int old_option = fcntl( fd, F_GETFL );
  21. int new_option = old_option | O_NONBLOCK;
  22. fcntl( fd, F_SETFL, new_option );
  23. return old_option;
  24. }
  25.  
  26. void addfd( int epollfd, int fd, bool enable_et )
  27. {
  28. epoll_event event;
  29. event.data.fd = fd;
  30. event.events = EPOLLIN; //表示对应的文件描述符可以读(包括对端SOCKET正常关闭)
  31. if( enable_et )
  32. {
  33. //EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。
  34. event.events |= EPOLLET;
  35. }
  36. epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event ); //register to epoll
  37. setnonblocking( fd );
  38. }
  39.  
  40. void lt( epoll_event* events, int number, int epollfd, int listenfd )
  41. {
  42. char buf[ BUFFER_SIZE ];
  43. for ( int i = 0; i < number; i++ )
  44. {
  45. int sockfd = events[i].data.fd;
  46. if ( sockfd == listenfd )
  47. {
  48. struct sockaddr_in client_address;
  49. socklen_t client_addrlength = sizeof( client_address );
  50. int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
  51. addfd( epollfd, connfd, false );
  52. }
  53. else if ( events[i].events & EPOLLIN )
  54. {
  55. printf( "event trigger once\n" );
  56. memset( buf, '\0', BUFFER_SIZE );
  57. int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 );
  58. if( ret <= 0 )
  59. {
  60. close( sockfd );
  61. continue;
  62. }
  63. printf( "get %d bytes of content: %s\n", ret, buf );
  64. }
  65. else
  66. {
  67. printf( "something else happened \n" );
  68. }
  69. }
  70. }
  71.  
  72. void et( epoll_event* events, int number, int epollfd, int listenfd )
  73. {
  74. char buf[ BUFFER_SIZE ];
  75. for ( int i = 0; i < number; i++ )
  76. {
  77. int sockfd = events[i].data.fd;
  78. if ( sockfd == listenfd )
  79. {
  80. struct sockaddr_in client_address;
  81. socklen_t client_addrlength = sizeof( client_address );
  82. int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
  83. addfd( epollfd, connfd, true );
  84. }
  85. else if ( events[i].events & EPOLLIN )
  86. {
  87. printf( "event trigger once\n" );
  88. while( 1 )
  89. {
  90. memset( buf, '\0', BUFFER_SIZE );
  91. int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 );
  92. if( ret < 0 )
  93. {
  94. if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) )
  95. {
  96. printf( "read later\n" );
  97. break;
  98. }
  99. close( sockfd );
  100. break;
  101. }
  102. else if( ret == 0 )
  103. {
  104. close( sockfd );
  105. }
  106. else
  107. {
  108. printf( "get %d bytes of content: %s\n", ret, buf );
  109. }
  110. }
  111. }
  112. else
  113. {
  114. printf( "something else happened \n" );
  115. }
  116. }
  117. }
  118.  
  119. int main( int argc, char* argv[] )
  120. {
  121. if( argc <= 2 )
  122. {
  123. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  124. return 1;
  125. }
  126. const char* ip = argv[1];
  127. int port = atoi( argv[2] );
  128.  
  129. int ret = 0;
  130. struct sockaddr_in address;
  131. bzero( &address, sizeof( address ) );
  132. address.sin_family = AF_INET;
  133. inet_pton( AF_INET, ip, &address.sin_addr );
  134. address.sin_port = htons( port );
  135.  
  136. int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
  137. assert( listenfd >= 0 );
  138.  
  139. ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
  140. assert( ret != -1 );
  141.  
  142. ret = listen( listenfd, 5 );
  143. assert( ret != -1 );
  144.  
  145. epoll_event events[ MAX_EVENT_NUMBER ];
  146. int epollfd = epoll_create( 5 ); //tell epoll the listen number is 5
  147. assert( epollfd != -1 );
  148. addfd( epollfd, listenfd, true );
  149.  
  150. while( 1 )
  151. {
  152. //参数events用来从内核得到事件的集合,maxevents告之内核这个events有多大,
  153. //这个maxevents的值不能大于创建epoll_create()时的size,参数timeout是超时时间
  154. //该函数返回需要处理的事件数目,如返回0表示已超时。
  155. int ret = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 );
  156. if ( ret < 0 )
  157. {
  158. printf( "epoll failure\n" );
  159. break;
  160. }
  161.  
  162. lt( events, ret, epollfd, listenfd );
  163. //et( events, ret, epollfd, listenfd );
  164. }
  165.  
  166. close( listenfd );
  167. return 0;
  168. }

  

oneshot:

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include <sys/epoll.h>
  13. #include <pthread.h>
  14.  
  15. #define MAX_EVENT_NUMBER 1024
  16. #define BUFFER_SIZE 1024
  17.  
  18. struct fds
  19. {
  20. int epollfd;
  21. int sockfd;
  22. };
  23.  
  24. int setnonblocking( int fd )
  25. {
  26. int old_option = fcntl( fd, F_GETFL );
  27. int new_option = old_option | O_NONBLOCK;
  28. fcntl( fd, F_SETFL, new_option );
  29. return old_option;
  30. }
  31.  
  32. void addfd( int epollfd, int fd, bool oneshot )
  33. {
  34. epoll_event event;
  35. event.data.fd = fd;
  36. //EPOLLIN表示对应的文件描述符可以读(包括对端SOCKET正常关闭);
  37. //EPOLLET将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。
  38. event.events = EPOLLIN | EPOLLET; //
  39. if( oneshot )
  40. {
  41. //EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,
  42. //需要再次把这个socket加入到EPOLL队列里
  43. event.events |= EPOLLONESHOT;
  44. }
  45. //EPOLL_CTL_ADD:注册新的fd到epfd中;
  46. epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event );
  47. setnonblocking( fd );
  48. }
  49.  
  50. void reset_oneshot( int epollfd, int fd )
  51. {
  52. epoll_event event;
  53. event.data.fd = fd;
  54. event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
  55. //EPOLL_CTL_MOD:修改已经注册的fd的监听事件;
  56. epoll_ctl( epollfd, EPOLL_CTL_MOD, fd, &event );
  57. }
  58.  
  59. void* worker( void* arg )
  60. {
  61. int sockfd = ( (fds*)arg )->sockfd;
  62. int epollfd = ( (fds*)arg )->epollfd;
  63. printf( "start new thread to receive data on fd: %d\n", sockfd );
  64. char buf[ BUFFER_SIZE ];
  65. memset( buf, '\0', BUFFER_SIZE );
  66. while( 1 )
  67. {
  68. int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 );
  69. //ret=0 -- 这里表示对端的socket已正常关闭.
  70. if( ret == 0 )
  71. {
  72. close( sockfd );
  73. printf( "foreiner closed the connection\n" );
  74. break;
  75. }
  76. else if( ret < 0 )
  77. {
  78. //sockfd is NONBLOCK, EAGAIN -- no more data to read, so reset sockfd again
  79. if( errno == EAGAIN )
  80. {
  81. reset_oneshot( epollfd, sockfd );
  82. printf( "read later\n" );
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. printf( "get content: %s\n", buf );
  89. sleep( 5 );
  90. }
  91. }
  92. printf( "end thread receiving data on fd: %d\n", sockfd );
  93. }
  94.  
  95. int main( int argc, char* argv[] )
  96. {
  97. if( argc <= 2 )
  98. {
  99. printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
  100. return 1;
  101. }
  102. const char* ip = argv[1];
  103. int port = atoi( argv[2] );
  104.  
  105. int ret = 0;
  106. struct sockaddr_in address;
  107. bzero( &address, sizeof( address ) );
  108. address.sin_family = AF_INET;
  109. inet_pton( AF_INET, ip, &address.sin_addr );
  110. address.sin_port = htons( port );
  111.  
  112. int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
  113. assert( listenfd >= 0 );
  114.  
  115. ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
  116. assert( ret != -1 );
  117.  
  118. ret = listen( listenfd, 5 );
  119. assert( ret != -1 );
  120.  
  121. epoll_event events[ MAX_EVENT_NUMBER ];
  122. int epollfd = epoll_create( 5 );
  123. assert( epollfd != -1 );
  124. addfd( epollfd, listenfd, false );
  125.  
  126. while( 1 )
  127. {
  128. int ret = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 );
  129. if ( ret < 0 )
  130. {
  131. printf( "epoll failure\n" );
  132. break;
  133. }
  134.  
  135. for ( int i = 0; i < ret; i++ )
  136. {
  137. int sockfd = events[i].data.fd;
  138. if ( sockfd == listenfd )
  139. {
  140. struct sockaddr_in client_address;
  141. socklen_t client_addrlength = sizeof( client_address );
  142. int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
  143. addfd( epollfd, connfd, true );
  144. }
  145. else if ( events[i].events & EPOLLIN )
  146. {
  147. pthread_t thread;
  148. fds fds_for_new_worker;
  149. fds_for_new_worker.epollfd = epollfd;
  150. fds_for_new_worker.sockfd = sockfd;
  151. pthread_create( &thread, NULL, worker, ( void* )&fds_for_new_worker );
  152. }
  153. else
  154. {
  155. printf( "something else happened \n" );
  156. }
  157. }
  158. }
  159.  
  160. close( listenfd );
  161. return 0;
  162. }

  

linux socket编程示例的更多相关文章

  1. Linux socket编程示例(最简单的TCP和UDP两个例子)

    一.socket编程 网络功能是Uinux/Linux的一个重要特点,有着悠久的历史,因此有一个非常固定的编程套路. 基于TCP的网络编程: 基于连接, 在交互过程中, 服务器和客户端要保持连接, 不 ...

  2. Linux Socket 编程简介

    在 TCP/IP 协议中,"IP地址 + TCP或UDP端口号" 可以唯一标识网络通讯中的一个进程,"IP地址+端口号" 就称为 socket.本文以一个简单的 ...

  3. 多线程Java Socket编程示例

    package org.merit.test.socket; import java.io.BufferedReader; import java.io.IOException; import jav ...

  4. Linux socket 编程中存在的五个隐患

    前言:         Socket API 是网络应用程序开发中实际应用的标准 API.尽管该 API 简单,但是   开发新手可能会经历一些常见的问题.本文识别一些最常见的隐患并向您显示如何避免它 ...

  5. Linux Socket编程

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  6. Linux Socket编程(不限Linux)【转】

    转自:http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几 ...

  7. Linux Socket编程(不限Linux)

    "一切皆Socket!" 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. --有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信 ...

  8. Socket详解-Linux Socket编程(不限Linux)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  9. Windows Socket和Linux Socket编程的区别 ZZ

    socket相关程序从Windows移植到Linux下需要注意的: 1)头文件 Windows下winsock.h/winsock2.h Linux下sys/socket.h 错误处理:errno.h ...

随机推荐

  1. 洛谷 P3320: bzoj 3991: LOJ 2182: [SDOI2015]寻宝游戏

    题目传送门:LOJ #2182. 题意简述: 一棵 \(n\) 个节点的树,边有边权. 每个点可能是关键点,每次操作改变一个点是否是关键点. 求所有关键点形成的极小联通子树的边权和的两倍. 题解: 有 ...

  2. 【干货】Windows系统信息收集篇

    市场分析:计算机取证,就是应急响应.而应急响应的市场在于黑产的攻击频率.在当今的社会里,更多的人为了钱铤而走险的比比皆是,这个市场随着比特币,大数据,物联网的来临,规模将更加的庞大与有组织性.这将导致 ...

  3. linux系统时间不同步解决办法(同步本地时间)

    改变/etc/目录下的localtime文件,既可以改变当前的时区 1.方法是到/usr/share/zoneinfo目录下找到你要相对应的时区文件,例如上海在/usr/share/zoneinfo/ ...

  4. Java中获取包含变量的配置文件config.properties内容

    应用场景 有些时候项目中会用到很多路径,并且很可能多个路径在同一个根目录下,那为了方便配置的修改,达到只修改根目录即可达到一改全改的效果,此时就会想到要是有变量就好了: 另外有时候路径中的文件名是不确 ...

  5. asp.net 文件下载显示中文名称

    protected void Page_Load(object sender, EventArgs e)    {        string guid = Request.QueryString[& ...

  6. Java基础87 MySQL数据约束

    1.默认值 -- 创建表student1,设置address字段有默认值 create table student1 ( id int, name ), address ) default '广东省深 ...

  7. java实现xml格式与javabean之间的转换XmlUtil类

    XmlUtil类:不多说,直接撸代码: /** * java 转换成xml * @Title: toXml * @Description: TODO * @param obj 对象实例 * @retu ...

  8. shell加密

    如何保护自己编写的shell程序要保护自己编写的shell脚本程序,方法有很多,最简单的方法有两种:1.加密 2.设定过期时间,下面以shc工具为例说明: 一.下载安装shc工具shc是一个加密she ...

  9. IntelliJ IDEA JRebel Maven Tomcat 实现热部署

    一,JRebel 插件 获取与安装 直接在 IDEA 中操作获取 JRebel 插件 Paste_Image.png Paste_Image.png 安装完成,记得重启 IDEA 使刚才安装的插件生效 ...

  10. eaccelerator 完全手册:配置、控制、API接口

    安装官方有很详细的文档 转自 http://www.enjoyphp.com/2010/eaccelerator-manual/ 配置选项 eaccelerator.shm_size指定 eAccel ...