linux进程通信 获取远程系统时间的例子

server.c

--------------------

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define LOCAL_IP "192.168.59.194"

#define LOCAL_PORT 8888

struct sockaddr_in local_sk_addr, client_sk_addr;

int fd_sock,full_sock;

char buf[27];

struct timeval tv;

int length = 0;

int main(void)

{

int lsn_id;

fd_set fdset;

fd_sock = socket(AF_INET, SOCK_STREAM, 0);

bzero(&local_sk_addr, sizeof(local_sk_addr));

local_sk_addr.sin_port = htons(LOCAL_PORT);

local_sk_addr.sin_family = AF_INET;

local_sk_addr.sin_addr.s_addr = inet_addr(LOCAL_IP);

bind(fd_sock, (struct sockaddr*)&local_sk_addr, sizeof(local_sk_addr));

lsn_id = listen(fd_sock, 5);

if (lsn_id != -1)

printf("listen on port %d\n:", LOCAL_PORT);

FILE * file = NULL;

file = fopen("out.txt", "w+");

if (NULL == file){

printf("open out.txt failed.\n");}

while (1)

{

FD_ZERO(&fdset);

if ((full_sock = accept(fd_sock, NULL, NULL))>0 )

{

printf("remote connected!\n");

break;

}

}

while(2)

{

// printf("full_sock:%d\n", full_sock);

//FD_SET(full_sock, &fdset);

//tv.tv_sec = tv.tv_usec = 0;

bzero(buf, 27);

if(0 < recv(full_sock, buf, 26 , 0))

{

fputs(buf, file);

// printf("%s\n", "in!");

printf("remote says:%s\n", buf);

// printf("\n");

}

}

//sleep(1);

/*

if (select(full_sock + 1, &fdset, NULL, NULL, &tv) > 0)

{

printf("data recving!\n");

if(FD_ISSET(full_sock, &fdset))

{

bzero(buf, 27);

if(recv(full_sock, buf, 27 , 0))

{

printf("%s\n", "in!");

printf("remote says:%s\n", buf);

printf("\n");

}

}

}

*/

//close(full_sock);

//close(fd_sock);

// fclose(file);

}

client.c

_____________________________________

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define REMOTE_IP "192.168.59.194"

#define REMOTE_PORT 8888

struct sockaddr_in remote_sk_addr;

int fd_sock;

char buf[27];

struct tm l_time;

void local_time(char* buf)

{

struct timeval tv;

time_t time;

struct tm* st_tm;

gettimeofday(&tv, NULL);

time = tv.tv_sec;

st_tm = localtime((const time_t*)&time);

sprintf(buf,"%d-%d-%d %d:%d:%d.%ld\n",

st_tm->tm_year + 1900,

st_tm->tm_mon + 1,

st_tm->tm_mday,

st_tm->tm_hour,

st_tm->tm_min,

st_tm->tm_sec,

tv.tv_usec);

}

int main(void)

{

fd_set fdset;

fd_sock = socket(AF_INET, SOCK_STREAM, 0);

bzero(&remote_sk_addr, sizeof(remote_sk_addr));

remote_sk_addr.sin_port = htons(REMOTE_PORT);

remote_sk_addr.sin_family = AF_INET;

remote_sk_addr.sin_addr.s_addr = inet_addr(REMOTE_IP);

connect(fd_sock, (struct sockaddr*)&remote_sk_addr, sizeof(remote_sk_addr));

//printf("fd_sock:%d\n", fd_sock);

if(fd_sock != -1)

{

printf("connected to remote!");

}

printf("\n");

while (1)

{

// FD_ZERO(&fdset);

// FD_SET(fd_sock, &fdset);

bzero(buf, 27);

local_time(buf);

printf("%s\n", buf);

// send(fd_sock, buf, 27, 0);

sendto(fd_sock, buf, 27, 0, (struct sockaddr*) &remote_sk_addr,sizeof(remote_sk_addr));

sleep(2);

}

// close(fd_sock);

}

linux 远程修改时间,linux 获取远程系统时间的例子相关推荐

  1. js如何获取计算机当前时间,js获取当前系统时间

    搜索热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. var myDate = new Date(); myDate.getYear( ...

  2. java 当前时间string_Java 获取当前系统时间的三种方法

    准备工作: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 方式一: /** ...

  3. web java获取当前时间_Java 获取当前系统时间的三种方法

    准备工作: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 方式一: /** ...

  4. java从数据库中获取当前时间_JAVA获取当前系统时间(包含时分秒)插入数据库...

    网上找到好多方法,但是貌似都是不准确,当我看到这个我内牛满面 Timestamp d= new Timestamp(System.currentTimeMillis()); //获取系统时间直接转换成 ...

  5. sap获取系统时间_C++获取当前系统时间的方法总结

    C++并发编程(C++11到C++17) 为什么要并发编程 大型的软件项目常常包含非常多的任务需要处理.例如:对于大量数据的数据流处理,或者是包含复杂GUI界面的应用程序.如果将所有的任务都以串行的方 ...

  6. html中如何写系统时间,在HTML页面获取当前系统时间

    function getCurDate() { var d = new Date(); var week; switch (d.getDay()){ case 1: week="星期一&qu ...

  7. C++获取当前系统时间

    在Linux环境下,C++获取当前系统时间的相关代码: #include <stdio.h> #include <stdlib.h> #include <time.h&g ...

  8. linux 如何在命令行下改系统时间

    我们一般使用"date -s"命令来修改系统时间.比如将系统时间设定成2009年6月1日的命令如下.       #date -s 06/01/2009       或 #date ...

  9. Linux —— 时间(date设置系统时间)

    Date命令的作用:以给定的格式显示当前的时间,或者设置系统时间 一.了解date命令的参数 date命令使用方式: 用法:date [选项]... [+格式] 或:date [-u|--utc|-- ...

  10. DataTime获取当前系统时间大全

    在c# / ASP.net中我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间 ...

最新文章

  1. 十四、H.264的变换编码(一)——矩阵运算与正交变换基本概念
  2. python从入门到精通书-Python从入门到精通
  3. 不要轻易修改ESX主机的主机名
  4. xxxx is not found in PojoType<Order, fields = [amount: Integer, product: String, user: Long]>
  5. sklearn 决策树(分类树、回归树)的 重要参数、属性、方法理解
  6. 前端学习(1337):mongoDB文档查询
  7. c语言结构体指针初始化
  8. Java抽象类、接口、类的特殊成员
  9. 组织需要什么样的我_为什么开放组织对我说话
  10. android drawable 对象,Android Drawable开发简介
  11. Node的textContent属性
  12. 自动点击器如何设置最快_微视APP如何设置自动播放视频-微视APP设置自动播放视频的方法...
  13. 【java笔记】继承与多态
  14. 【转】cs231n学习笔记-CNN-目标检测、定位、分割
  15. vb难还是c语言难java_c语言难还是vb难?
  16. 智能门锁主要有哪些优势,具体是怎么分类的?
  17. Navicat 注册机破解问题
  18. 目前IT很火,很多人转行,简单培训后进入IT行业,那么这样“速成”的IT从业人员发展前景如何?
  19. Python初学者:输入若干个正整数,升序后输出
  20. os.system和os.popen函数的区别

热门文章

  1. 由_exit()引起的对于缓冲区的理解
  2. 在Windows编译libssh
  3. DPDK - flow Haripin
  4. 使用SSE指令集优化memcpy
  5. 解决Ubuntu ssh 登录慢问题
  6. linux+mmap父子通信_Linux 系统开发5 进程间通信 pipe() fifo() mmap()
  7. yagmail和keyring的安装与注册
  8. mysql导出_mysql数据库导入导出
  9. 学生管理系统(源码)(C语言版)
  10. android adb驱动win7,adbwin7下载地址_win7安装adb驱动的方法