请教在局域网通信问题
之前做了个基于C—S的数据包传输测试,结果在单机上表现良好,基本上10M以内的文件均无丢包的情况发生,但将程序移到局域网上,多台机器之间进行测试,却出现严重的丢包。究其原因,还请各位大侠不吝才赐教。代码如下:服务器:
#include <stdio.h>
#include <tchar.h>
#include <ace/Acceptor.h>
#include <ace/SOCK_Acceptor.h>
#include <ace/SOCK_Stream.h>
#include <ace/Svc_Handler.h>
static const char * filename = "bigfile.dat";
class ACEServer : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> super;
public:
ACEServer() {
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Create New ACEServer\n"));
};
public:
virtual ~ACEServer() {
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Destory ACEServer\n"));
};
virtual int open(void *p){
if((file = fopen(filename, "rb")) == NULL) {
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Failed to open file\n"));
return -1;
}
total = 0;
ACE_DEBUG ((LM_DEBUG,"(%P|%t) New Client Connection\n"));
ACE_Reactor::instance()->register_handler(this,
ACE_Event_Handler::WRITE_MASK);
return super::open(p);
}
protected:
virtual int handle_output(ACE_HANDLE) {
size_t count = fread(block, sizeof(char), sizeof(block), file);
if(count <= 0) {
ACE_DEBUG ((LM_DEBUG,"\n(%P|%t) Send Finished!\n"));
fclose(file);
file = NULL;
return -1;
}
this->peer().send(block, count);
total += count;
ACE_DEBUG((LM_DEBUG, ".", total));
return 1;
}
private:
char block;//以4K为单位
FILE *file;
size_t total;
};
int _tmain(int argc, _TCHAR* argv[])
{
typedefACE_Acceptor<ACEServer, ACE_SOCK_ACCEPTOR> MyServer;
ACE_INET_Addr address(12345);
MyServer server;
if(server.open(address) == -1) {
ACE_DEBUG((LM_DEBUG, "(%P|%t) Failed to Create Server\n"));
return -1;
}
ACE_Reactor::instance()->run_reactor_event_loop();
return 0;
}
客户端:
#include <stdio.h>
#include <tchar.h>
#include <ace/Connector.h>
#include <ace/SOCK_Connector.h>
#include <ace/SOCK_Stream.h>
#include <ace/Svc_Handler.h>
class ACEClient : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> super;
public:
ACEClient() {
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Create New ACEClient\n"));
};
public:
virtual ~ACEClient() {
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Destory ACEClient\n"));
};
virtual int open(void *p){
total = 0;
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Connected to Server\n"));
return super::open(p);
}
protected:
virtual int handle_input(ACE_HANDLE) {
int count = this->peer().recv(block, sizeof(block));
if(count > 0) {
total += count;
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Received %d bytes\n", total));
return 0;
}
if(count == 0 && ACE_OS::last_error() != EWOULDBLOCK) {
ACE_Reactor::instance()->end_reactor_event_loop();
return -1;
}
ACE_DEBUG ((LM_DEBUG,"(%P|%t) Received %d bytes\n", total));
return 0;
}
private:
char block;
size_t total;
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef ACE_Connector<ACEClient, ACE_SOCK_CONNECTOR> MyClient;
ACE_INET_Addr address(12345, "192.35.168.1");
MyClient client;
ACEClient c;
ACEClient *pc = &c;
if(client.connect(pc, address) == -1) {
ACE_DEBUG((LM_DEBUG, "(%P|%t) Failed to connect server!\n"));
return -1;
}
ACE_Reactor::instance()->run_reactor_event_loop();
return 0;
}
页:
[1]