设为首页 加入收藏

TOP

socket 开发 - 那些年用过的基础 API(一)
2018-10-21 20:08:44 】 浏览:357
Tags:socket 开发 那些 基础 API

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

前言 - 思考还是

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

  socket 写过一点点,  总感觉很别扭. 例如 read, recv, recvfrom 这些为啥这么奇葩. 这是 linux 的设计吗.

这种强糅合的 read 代码, '带坏'了多少人. 想起很久以前看过的 <<UNIX痛恨者手册>>, 外加上常写点跨平台

库. 不得不思考设计, 发现 

  1) winds 对于 socket 设计比 linux POSIX 设计理解更加友好一丢丢

  2) linux 性能比 winds 好. (开源哲学 对冲 精英文化)

  3) 应用层是个不完备的域, 不要一条胡同走不到头

(备注 : 有一段日子特别讨厌 winds, 及其喜欢羡慕 unix, 但是随着成长认识有了很大变化, 痛恨没钱没时间)

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

正文 - 来点证明

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

1. 如果可以不妨多写点跨平台, 线程安全的代码

  不妨举个烂大街的例子, 我们经常在处理时间的时候直接用  gettimeofday

#include <sys/time.h>

int gettimeofday(struct timeva l * tv, struct timezone * tz);

The  functions  gettimeofday() can get and set the time as well as a timezone. 
The tv argument is a struct timeva l (as specified in <sys/time.h>):

    struct timeva l {
        time_t      tv_sec;     /* seconds */
        suseconds_t tv_usec;    /* microseconds */
    };

and gives the number of seconds and microseconds since the Epoch (see time(2)).  
The tz argument is a struct timezone:

    struct timezone {
        int tz_minuteswest;     /* minutes west of Greenwich */
        int tz_dsttime;         /* type of DST correction */
    };

If either tv or tz is NULL, the corresponding structure is not set or returned.  
(However, compilation warnings will result if tv is NULL.)

The use of the timezone structure is obsolete; 
the tz argument should normally be specified  as  NULL.

只是简单的得到当前时间秒数和微秒, 附赠一个时区消息. 这个函数一眼看过去, 设计的不优美.

如果希望你的代码能够在 winds 上面也奔跑, 可能需要一个移植版本 

#ifdef _MSC_VER

#include <winsock2.h>
// // gettimeofday - Linux sys/time.h 中得到微秒的一种实现 // tv : 返回结果包含秒数和微秒数 // tz : 包含的时区,在winds上这个变量没有用不返回 // return : 默认返回0 // inline int gettimeofday(struct timeva l * tv, void * tz) { struct tm st; SYSTEMTIME wtm; GetLocalTime(&wtm); st.tm_year = wtm.wYear - 1900; st.tm_mon = wtm.wMonth - 1; // winds的计数更好些 st.tm_mday = wtm.wDay; st.tm_hour = wtm.wHour; st.tm_min = wtm.wMinute; st.tm_sec = wtm.wSecond; st.tm_isdst = -1; // 不考虑夏令时 tv->tv_sec = (long)mktime(&st); // 32位使用数据强转 tv->tv_usec = wtm.wMilliseconds * 1000; // 毫秒转成微秒 return 0; } #endif

同样你的工作量已经起来了. 不管高不高效. 总是个下策. 这里有个更好的主意, 利用  timespec_get 

#include <time.h>


/* Set TS to calendar time based in time base BASE.  */
int
timespec_get (struct timespec *ts, int base)
{
  switch (base)
    {
    case TIME_UTC:
      if (__clock_gettime (CLOCK_REALTIME, ts) < 0)
        return 0;
      break;

    default:
      return 0;
    }

  return base;
}

C11 标准提供的获取秒和纳秒的时间函数, CL 和 GCC clang 都提供了支持. 上面是glibc中一个实现, 是不是很 low.

扯一点

  1.1 写代码应该有很强的目的, 非特殊领域应该弱化针对性

  1.2 上层应用, 应该首要向着标准靠拢, 其次是操作系统, 再到编译器

对于CL 实现了 timespec_get, 应该最主要目的是为了 C++11基础特性支持, 还有 clang 的实现.

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

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇建立简单的Hash table(哈希表)b.. 下一篇高精度小数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目