tutorial里面已经有了现成的服务器代码,tutorial/py/PythonServer.py。但是在Thrift 0.8.0里面,这个代码是有问题的,需要修改
第84行,
transport = TSocket.TServerSocket(9090)
修改为transport = TSocket.TServerSocket(port = 9090)
否则,运行的时候会报错,TypeError: getaddrinfo() argument 1 must be string or None。而且我们在运行的时候,最好要先进入目录tutorial/py,然后以./PythonServer.py方式运行,否则由于PYTHONPATH的问题,还会有错误ImportError: No module named tutorial。当然也有其他解决办法,解决方法看这里。
在运行PHP客户端之前,我们还有一些其他工作要做,建议这样配置:
cd tutorial/php
cp -r ../../lib/php/src/ ./
cp -r ../gen-php ./src/packages/
四、使用C++服务器
使用C++做服务器需要安装C++ lib,修改Makefile文件,参照wiki
http://wiki.apache.org/thrift/ThriftUsageC%2B%2B
安装C++ lib的方法是:
cd thrift-0.8.0/lib/cpp/
make
sudo make install
sudo /sbin/ldconfig -v
上面的第4步是更新系统lib库的,让系统库包含/usr/local/lib,否则运行C++ Server的时候,找不到libthrift-0.8.0.so。参考链接:http://blog.csdn.net/shendl/article/details/5746062
下一步是更新Makefile文件, 可以参考我的Makefile(Wiki上也有更详细的说明),
我的Makefile文件如下:
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
BOOST_DIR := /usr/include
THRIFT_DIR := /usr/local/include/thrift
LIB_DIR := /usr/local/lib
GEN_SRC := ./gen-cpp/SharedService.cpp ./gen-cpp/shared_types.cpp ./gen-cpp/tutorial_types.cpp ./gen-cpp/Calculator.cpp
GEN_OBJ := $(patsubst %.cpp, %.o, $(GEN_SRC))
INC = -I$(THRIFT_DIR) -I$(BOOST_DIR)
.PHONY: all clean
all: server client
%.o: %.cpp
$(CXX) -Wall -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $< -o $@
server: CppServer.o $(GEN_OBJ)
$(CXX) $^ -o $@ -L$(LIB_DIR) -lthrift
client: CppClient.o $(GEN_OBJ)
$(CXX) $^ -o $@ -L$(LIB_DIR) -lthrift
clean:
$(RM) *o server client