TCP长链接keep-alive_connection

摘引文章: https://www.gomcu.com/setsockop/

  • 熟悉函数

socket设置选项

1
2
3
#include<sys/types.h>
#include<sys/socket.h>
int setsockopt(int sock,int level,int optanme,const void* optval,socklen_t optlen);

返回說明:
成功執行時,返回0。失敗返回-1,errno被設為以下的某個值
EBADF:sock不是有效的檔描述詞
EFAULT:optval指向的記憶體並非有效的進程空間
EINVAL:在調用setsockopt()時,optlen無效
ENOPROTOOPT:指定的協定層不能識別選項
ENOTSOCK:sock描述的不是通訊端
Nagle演算法設置。


  • vs报错:
    1
    https://jingyan.baidu.com/article/1709ad8097e5904634c4f03e.html

  1. #ifdef 编译时候带参数编译
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifdef TAKE
#define THAT 0

#endif


#ifdef TAKE2
#define THAT 2
#endif


#include<iostream>
using std::cout;
using std::endl;

int main()
{


cout<<THAT<<endl;



}
1
g++ -DTAKE2 ndef.cpp

选择编译TAKE2的宏定义

[vs创建DLL/使用][1]
[1]:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29121609&id=3851844

知识:

  1. Linux 动态库剖析
    https://www.ibm.com/developerworks/cn/linux/l-dynamic-libraries/index.html

  2. linux下动态链接库(.so)的显式调用和隐式调用

https://blog.csdn.net/lc_910927/article/details/42393121

  1. DLL中类的显式链接
    http://www.moon-soft.com/doc/14639.htm

  2. Windows关键字

1
BOOL APIENTRY DllMain(HANDLE hModule,DOWRD dwReason,void * lpReserved)
  • APIENTRY:表明此函数是应用程序的入口点,相当于c的main()函数
  1. visualstudio2015加载DLL

将编译好的DLL_AGM.libDLL_AGM.dll还有头文件DLL_AGM.h文件放入vs工程源码目录里,以及子目录Debug目录里;

每次更改代码都要替换.dll以及.lib文件,如修改.h文件也要替换.h文件

Linux下编写so

参考文章:

[linux与windows调用动态库so dll文件] [2]
[2]: https://blog.csdn.net/cbbbc/article/details/45102861

  1. add.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include"add.h"

    int add(int a,int b)
    {
    int sum;
    sum=a+b;
    //std::cout<<sum<<std::endl;
    return sum;
    }
  2. add.h

    1
    2
    3
    #include<iostream>

    int add(int a,int b);
  3. 封装成.so

    1
    g++   add.cpp  -fPIC  -shared -o libADD.so

    生成libADD.so

调用代码
4. main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include"add.h"
using namespace std;
int main()
{

int sum = add(1,2);

std::cout<<sum<<endl;

return 0;
}

g++链接.so
5.

1
g++ main.cpp -L. -lADD -Wl,-rpath,./ -o b.out

VS创建DLL

  1. 用Visualstudio新建一个工程Templates—–>Visual c++——–>Win32 Project

Overview Application Settings 选择Application type:下的DLL 勾选Empty project

通过编译会生成^&*.dll以及*&^.lib

  1. VS使用dll
  • 在工程里新建一个include文件,将编译dll工程里的.h文件放入include里面

  • 在工程目录新建一个lib文件夹,将编译dll工程生成的.lib文件放入该目录

  • 在使用dll工程中设置加载lib,

工程添加lib


2018-06-22

隐式加载

  • add_header.h

    1
    2
    3
    4
    /*      add_header.h  */
    #include<iostream>

    const int add(cosnt int a, const int b);
  • libadd_source.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     int add(const int a, const int b)
    {
    return a + b;
    }
    ````





    编译指令:
    ```linux
    g++ -shared -fPIC libadd_source.cpp -o libadd.so
  • main_test.cpp

    隐式加载

1
2
3
4
5
6
7
8
9
10
#include"add_header.h"

int main()
{
using namespace std;
int a = 3;
int b = 7;
cout<<a<<"+"<<b<<"="<<add(a,b)<<endl;
return 0;
}

编译指令:

1
g++ -o a.out main_test.cpp  -L. -ladd -Wl,-rpath,./

Fork me on GitHub