3.4 Client
和 Server – 发送和接受数据
C++
的高级特性之一就是运算符重载,或者简单的说,就是让某个运算符执行特定的功能。在ClientSocket 和 ServerSocket 类中,我重载了 << 和 >> 运算符,所以当使用的时候,他们就从socket里面写入或者读出数据。这里,是进一步更新过的server版本:
列表 4 : 简单server的实现 ( simple_server_main.cpp )
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
int main ( int argc, int argv[] )
{
try
{
// Create the socket
ServerSocket server ( 30000 );
while ( true )
{
ServerSocket new_sock;
server.accept ( new_sock );
try
{
while ( true )
{
std::string data;
new_sock >> data;
new_sock << data;
}
}
catch ( SocketException& ) {}
}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
}
#include "SocketException.h"
#include <string>
int main ( int argc, int argv[] )
{
try
{
// Create the socket
ServerSocket server ( 30000 );
while ( true )
{
ServerSocket new_sock;
server.accept ( new_sock );
try
{
while ( true )
{
std::string data;
new_sock >> data;
new_sock << data;
}
}
catch ( SocketException& ) {}
}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
}
new_sock
变量包含所有socket的信息,所以我们可以使用它来于客户端交换数据。"new_sock >> data;" 一行应该被解读为 "从 new_sock读数据,并且把他们放到我们的变量'data'中"。类似的,下一行就是把'data'中的数据通过连接发送回client。
如果你注意,你会发现我们已经创建了一个可以相应的服务器,但是没有任何数据从客户端发送过来。这里我们就来写一个client,它向server发数据,并且打印出server的响应。
列表 5 : 简单客户端的实现 ( simple_client_main.cpp )
#include "ClientSocket.h"
#include "SocketException.h"
#include <iostream>
#include <string>
int main ( int argc, int argv[] )
{
try
{
ClientSocket client_socket ( "localhost", 30000 );
std::string reply;
try
{
client_socket << "Test message.";
client_socket >> reply;
}
catch ( SocketException& e) {}
std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\n";
}
return 0;
}
#include "SocketException.h"
#include <iostream>
#include <string>
int main ( int argc, int argv[] )
{
try
{
ClientSocket client_socket ( "localhost", 30000 );
std::string reply;
try
{
client_socket << "Test message.";
client_socket >> reply;
}
catch ( SocketException& e) {}
std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\n";
}
return 0;
}
我们发送字符串是"Test Message." 到服务器,然后从服务器那里读取响应,并且通过标准输出输出。
4.
编译和测试我们的cs
4.1
文件列表
如下文件是我们的例子:
多方面的:
Makefile –
项目的makefile文件
Socket.h, Socket.cpp – Socket
类,实现的原生的socket API调用。
SocketException.h - SocketException
类
Server:
simple_server_main.cpp –
主文件
ServerSocket.h, ServerSocket.cpp - ServerSocket
类
Client:
simple_client_main.cpp –
主文件
ClientSocket.h, ClientSocket.cpp - ClientSocket
4.2
编译和测试
编译简单。首先吧所有的项目文件保存到一个目录下,之后再你的命令提示符下键入:
prompt$ cd directory_you_just_created
prompt$ make
prompt$ make
这样就编译了所有的文件,并且创建了simple_server 和 simple_client 输出文件。要测试着两个输入文件,运行如下命令:
first prompt:
prompt$ ./simple_server
running....
second prompt:
prompt$ ./simple_client
We received this response from the server:
"Test message."
prompt$
prompt$ ./simple_server
running....
second prompt:
prompt$ ./simple_client
We received this response from the server:
"Test message."
prompt$
之后客户端回发送文件到服务器,读取并且相应,之后把相应输出到标准输出上去。你可以运行任意多次客户端,服务器会相应每个请求。
5.
总结
Rob Tougher
Rob
是一位再纽约市中心区域工作的C++软件工程师。当不能在他最喜爱的平台上工作的时候,你能发现Rob正在沙滩上散步,和他的女友Nicole和他们的宠物狗Halley在一起。
转载于:https://blog.51cto.com/thinkinginmind/174878