apue.h ……

书上代码都有这个
这个文件在书的附录里边

我无法理解作者为什么这么作
他自己的解释是 环保 节约纸张

还好 这个有下载

http://www.apuebook.com/

apue.h 在 include目录

http://www.apuebook.com/src.tar.gz

这里还包含了书中的代码

Read More

入门级别linux socket 通信

linux 进程间通讯 第一步

主要是参考 http://beej.us/guide/bgipc/ 写出来的
国人写的基本全是转载 不是代码残疾 就是根本看不懂的……广告

整理下 g++编译

Server.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

#include <sys/socket.h>
#include <sys/un.h>

const int MAX_RECV = 500;

int main(int argc, char ** argv){

    sockaddr_un server; // sockaddr_un 表示使用socket文件 参考 sockaddr_in

    server.sun_family = AF_UNIX;
    strcpy(server.sun_path, "test.socket");
    unlink(server.sun_path); // 绑定之前 清理掉 

    int s = socket(AF_UNIX, SOCK_STREAM, 0);

    bind(s , (sockaddr *) &server, sizeof(server));

    listen(s, 5);
   
    for(;;){
        cout<<"Wating"<<endl;

        sockaddr_un client;
        socklen_t client_len = sizeof(client);

        int c = accept(s, (sockaddr *) &client, &client_len);
        cout<<"Connected " <<endl;

        char buf[MAX_RECV];
        int len;

        while( (len = recv(c, &buf, MAX_RECV, 0)) >0 ){
            // 处理buf

            cout <<"Client Says:" << buf <<endl;
            send(c, buf, len, 0); //写入client
        }
    }

    return 0;
}

client.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

#include <sys/socket.h>
#include <sys/un.h>

const int MAX_RECV = 500;

int main(int argc, char ** argv){
   
    sockaddr_un server; // sockaddr_un 表示使用socket文件 参考 sockaddr_in

    server.sun_family = AF_UNIX;
    strcpy(server.sun_path, "test.socket");

    int s = socket(AF_UNIX, SOCK_STREAM, 0);

    connect(s, (sockaddr *) &server, sizeof(server));

    char str[MAX_RECV];
    char buf[MAX_RECV];

    while(cin>>str){
        send(s, str, sizeof(str), 0);

        if( recv(s, &buf, MAX_RECV, 0) >0 ){
            // 处理buf
            cout << "Server Say:" << buf <<endl;
        }

    }
    return 0;
}

Makefile

1
2
3
4
5
6
7
all: server client
   
server: server.cpp
    g++ -o server server.cpp

client: client.cpp
    g++ -o client client.cpp
Read More

编写python c++ 扩展 (ubuntu下)

python 的c++扩展编写 比 php简单很多

phpize 也不是很好用的一个东西

编写一个 python mod hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Python.h"

PyObject* hello(PyObject* self, PyObject* args){
    const char * str;
   
    if (! PyArg_ParseTuple(args, "s", &str))
        return NULL;

   
    return Py_BuildValue("s",str);
}



static PyMethodDef helloMethods[] =
{
    {"hello", hello, METH_VARARGS, "hello comments"},
    {NULL, NULL, 0, NULL}

};

PyMODINIT_FUNC inithello(void){
    Py_InitModule("hello", helloMethods);
}

一个Python mod 因该有几个部分
1 扩展函数 hello
PyObject* hello(PyObject* self, PyObject* args){
只能是这样的结构
PyArg_ParseTuple 处理参数

2 函数列表 helloMethods
static PyMethodDef helloMethods

3 初始化函数 inithello
init函数名

编译
在 ubuntu 下需要安装python-dev
sudo apt-get install python-dev

yum服务器上应该是
python-devel

1
2
3
4
5
6
7
8
9
10
all: hello.so

hello.so: hello.o
    g++ -shared hello.o -o hello.so

hello.o: hello.cpp
    g++ -fPIC -c -I /usr/include/python2.6/ -o hello.o hello.cpp

clean:
    rm -f hello.o hello.so

使用
写一个 test.py

1
2
3
4
5
#!/usr/bin/env python

import hello

print hello.hello('world')

python 运行时候 会加载当前目录的.so

Read More

最基础的Makefile

hello.cpp
#include <iostream>
#include “lib.h”
using namespace std;

int main(int argc, char ** argv){
Helloworld * h = new Helloworld();
h->test();

cout<<”just done”<<endl;
return 0;
}
lib.cpp

#include <iostream>
#include “lib.h”
using namespace std;

void Helloworld::test(){
cout<<”Helloworld”<<endl;
}
lib.h
class Helloworld{
public:
void test();
};

Makefile

hello: hello.cpp lib.o
g++ lib.o hello.cpp -o hello

lib.o: lib.cpp lib.h
g++ -c lib.cpp

这样就进入linux c/cc 的世界了

Read More

CString -> double

我不喜欢mfc
不过别人请我作一个计算器
就用了下

java和.net中 string -> 数 是一件很快获得事情
不过cstring 到不那么简单了

查阅 说atof 不过2008没法用……
最后_wtof 才解决问题
只能说 unicode 还是一个变态

Read More