12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <fcntl.h>
- #include <poll.h>
- #include <sys/time.h>
- #define socketerrno errno
- #define SOCKET_EAGAIN_EINPROGRESS EAGAIN
- #define SOCKET_EWOULDBLOCK EWOULDBLOCK
- #define INVALID_SOCKET -1
- #define SOCKET_ERROR -1
-
- typedef int socket_t;
-
- #include "../common/iobuffer.h"
- #include "protocol.pb.h"
- int main(int, char**){
- int _fd = socket(AF_INET, SOCK_DGRAM, 0);
- sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(23816);
- sin.sin_addr.s_addr = inet_addr("192.168.3.131");
- MessageHead Head;
- Head.Command=RemoNet::CS_SyncTime;
- Head.Length=0;
- CIOBuffer buffer;
- Head.Serialize(buffer.Buffer);
- buffer.Length=sizeof(MessageHead);
- int32_t ret=sendto(_fd,buffer.Buffer,buffer.Length,0,(sockaddr * )&sin,sizeof(sin));
- sockaddr_in from;
- socklen_t fromlen=sizeof(sockaddr_in);
- ret=recvfrom(_fd,buffer.Buffer,CIOBuffer::IO_BUFFER_SIZE,0,(sockaddr *)&from,&fromlen);
- if(ret>0)
- {
- MessageHead Head;
- Head.Deserialize(buffer.Buffer);
- if(ret<MessageHead::Size()+Head.Length) return -1;
- auto ptr=buffer.Buffer+MessageHead::Size();
- RemoNet::SyncTime Req;
- Req.ParseFromArray(ptr,Head.Length);
-
- {
- struct tm _tm;
- _tm.tm_sec = Req.tm_sec();
- _tm.tm_min = Req.tm_min();
- _tm.tm_hour = Req.tm_hour();
- _tm.tm_mday = Req.tm_mday();
- _tm.tm_mon = Req.tm_mon();
- _tm.tm_year = Req.tm_year();
- _tm.tm_wday=Req.tm_wday();
- _tm.tm_yday=Req.tm_yday();
- _tm.tm_isdst=Req.tm_isdst();
- auto timep = mktime(&_tm);
- struct timeval tv;
- tv.tv_sec = timep;
- tv.tv_usec = 0;
- if(settimeofday (&tv, (struct timezone *) 0) < 0)
- {
- printf("Set system datatime error %s!/n",strerror(errno ));
-
- }
- }
- }
-
-
- }
|