Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61888
  • 博文数量: 11
  • 博客积分: 76
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-17 12:06
文章分类
文章存档

2014年(1)

2013年(4)

2012年(6)

分类: LINUX

2012-11-17 17:16:21

windows date time server:

  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <sstream>

  7. using namespace std;

  8. #pragma comment(lib,"Ws2_32.lib")

  9. #define IP_ADDRESS "210.47.24.11"
  10. #define PORT 13

  11. int main()
  12. {
  13.        WSADATA Ws;
  14.        SOCKET ServerSocket, CientSocket;
  15.        struct sockaddr_in LocalAddr, ClientAddr;
  16.        int Ret = 0;
  17.        int AddrLen = 0;
  18.        HANDLE hThread = NULL;
  19.   
  20.       //Init Windows Socket
  21.       if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
  22.       {
  23.           cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
  24.           return -1;
  25.       }
  26.      
  27.        //Create Socket
  28.        ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  29.       if ( ServerSocket == INVALID_SOCKET )
  30.       {
  31.           cout<<"Create Socket Failed::"<<GetLastError()<<endl;
  32.           return -1;
  33.       }
  34.   
  35.       LocalAddr.sin_family = AF_INET;
  36.       LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  37.       LocalAddr.sin_port = htons(PORT);
  38.       memset(LocalAddr.sin_zero, 0x00, 8);
  39.  
  40.       //Bind Socket
  41.       Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
  42.       if ( Ret != 0 )
  43.       {
  44.           cout<<"Bind Socket Failed::"<<GetLastError()<<endl;
  45.           return -1;
  46.      }
  47.      
  48.      Ret = listen(ServerSocket, PORT);
  49.      if ( Ret != 0 )
  50.      {
  51.          cout<<"listen Socket Failed::"<<GetLastError()<<endl;
  52.          return -1;
  53.      }
  54.      cout<<"server startup"<<endl;
  55.      AddrLen = sizeof(ClientAddr);
  56.      CientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
  57.      if ( CientSocket == INVALID_SOCKET )
  58.      {
  59.          cout<<"Accept Failed::"<<GetLastError()<<endl;
  60.      }

  61.      cout<<"client connect::"<<inet_ntoa(ClientAddr.sin_addr)<<":"<<ClientAddr.sin_port<<endl;

  62.      stringstream ss;
  63.      string str;
  64.      SYSTEMTIME systime;
  65.      GetLocalTime(&systime);
  66.      ss<<systime.wYear<<"-"<<systime.wMonth<<"-"<<systime.wDay<<" "<<systime.wHour<<":"<<systime.wMinute<<":"<<systime.wSecond;
  67.      str=ss.str();
  68.     
  69.     // cout<<str<<endl;
  70.      const char *buf = str.c_str();
  71. //     cout<<systime.wYear<<"-"<<systime.wMonth<<"-"<<systime.wDay<<" "<<systime.wHour<<":"<<systime.wMinute<<":"<<systime.wSecond<<endl;
  72.      send(CientSocket,buf,strlen(buf)+1,0);
  73.      closesocket(ServerSocket);
  74.      closesocket(CientSocket);
  75.      WSACleanup();

  76.      system("pause");
  77.     return 1;
  78. }
linux get date time client:

  1. #include<netinet/in.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<errno.h>
  5. #include<stdarg.h>
  6. #include<sys/types.h>
  7. #include<sys/socket.h>
  8. #include<arpa/inet.h>
  9. #include<sys/un.h>
  10. #define MAXLINE 4096

  11. int main(int argc, char **argv)
  12. {
  13. int sockfd,n;
  14. char recvline[MAXLINE+1];
  15. struct sockaddr_in servaddr;

  16. if(argc!=2)
  17.     printf("usage:a.out);

  18. if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
  19.     printf("socket error");
  20. bzero(&servaddr,sizeof(servaddr));
  21. servaddr.sin_family=AF_INET;
  22. servaddr.sin_port = htons(13);
  23. if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)
  24.     printf("inet_pton error for %s",argv[1]);

  25. if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
  26.     printf("fconnect error");
  27. while((n=read(sockfd,recvline,MAXLINE))>0)
  28. {
  29.     recvline[n]=0;
  30. if(fputs(recvline,stdout)==EOF)
  31.     printf("fputs error");
  32.     printf("\n");
  33. }
  34. if(n<0) printf("read error");
  35.     exit(0);
  36. }

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

上一篇:没有了

下一篇:个人主页

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