Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12401839
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C/C++

2015-11-02 16:54:34


  1. // WinServer.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. // 告知编译器,在编译后的链接阶段可以链接“ws2_32.lib”
  5. #pragma comment(lib,"ws2_32.lib")

  6. #include <stdio.h>
  7. #include <WinSock2.h>
  8. //#include <WinSock.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <process.h>
  14. #include <Windows.h>

  15. #define TEST_LISTEN_BACKLOG 0//1
  16. #define SERVER_PORT 4600 // define the defualt connect port id
  17. #define LENGTH_OF_LISTEN_QUEUE 10//1 // length of listen queue in server
  18. #define BUFFER_SIZE 255

  19. //void *thread(void *client_fd)
  20. DWORD WINAPI thread( LPVOID lpParam )
  21. {
  22.     char buf[BUFFER_SIZE] = {'\0'};
  23.     long timestamp;
  24.     
  25.     time_t rawtime;
  26.     struct tm *timeinfo;

  27.     while (1)
  28.     {
  29.         memset(buf, '\0', BUFFER_SIZE);
  30.         if (recv((*(int *)lpParam), buf, BUFFER_SIZE, 0) < 0)
  31.         {
  32.             fprintf(stderr, "get message from client error\n");
  33.             closesocket(*(int *)lpParam);
  34.             break;
  35.         }

  36.         // Transmission
  37.         Sleep(1000);
  38.         strcat(buf, " : ");
  39.         time(&rawtime);
  40.         timeinfo = localtime(&rawtime);
  41.         strcat(buf,""+timeinfo->tm_sec);

  42.         puts(buf);
  43.         send((*(int *)lpParam), buf, strlen(buf), 0);
  44.     }
  45.     return NULL;
  46. }

  47. int _tmain(int argc, _TCHAR* argv[])
  48. {
  49.     int server_fd,client_fd;
  50.     struct sockaddr_in servaddr;
  51.     struct sockaddr_in client_addr;

  52.     WSADATA wsaD;
  53.     WSAStartup(MAKEWORD(2,2),&wsaD);
  54.     server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  55.     if(server_fd == SOCKET_ERROR)
  56.     {
  57.         printf("new socket error!");
  58.         getchar();
  59.         return -1;
  60.     }

  61.     /* two way for init the char array ....
  62.      bzero(&servaddr, sizeof(servaddr)); */
  63.     memset(&servaddr,0,sizeof(servaddr));

  64.     servaddr.sin_family = AF_INET;
  65.     servaddr.sin_port = htons(SERVER_PORT);
  66.     servaddr.sin_addr.s_addr = INADDR_ANY; //htons(INADDR_ANY);

  67.     int error = 0;
  68.     error = bind(server_fd, (struct sockaddr*)&servaddr, sizeof(servaddr));
  69.     if(error<0)
  70.     {
  71.         printf("bind error");
  72.     }


  73.     listen(server_fd, LENGTH_OF_LISTEN_QUEUE);

  74. #if TEST_LISTEN_BACKLOG
  75.     /* wait the client to connect */
  76.     for (;;)
  77.     {
  78.         printf("server is listening! \n");
  79.         Sleep(1);
  80.     }
  81. #else
  82.     printf("server is listening! \n");
  83. #endif

  84.     while (1)
  85.     {
  86.         int length = sizeof(client_addr);
  87.         client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &length);
  88.         if(client_fd == -1)
  89.         {
  90.             Sleep(1000);
  91.             continue;
  92.         }

  93.         printf("a client connected: IP %s PORT %d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

  94.         Sleep(1000);
  95.         // windows C create thread ....
  96.         if(CreateThread(NULL,0,thread,(void*)&client_fd,0,NULL)==NULL)
  97.         {
  98.             fprintf(stderr, "create thread error\n");
  99.             exit(1);
  100.         }
  101.     }

  102.     closesocket(server_fd);
  103.     return 0;
  104. }

阅读(2591) | 评论(0) | 转发(0) |
0

上一篇:WinSocket的几个WSA*

下一篇:TCP数据报首部

给主人留下些什么吧!~~