程序里有bug,高手给看看,尤其要注意OnMReceive(),本人认为问题出在这个函数
#include <qsocket.h>
#include "client.h"
#include <qdatetime.h>
#include <qsocketdevice.h>
#include <qhostaddress.h>
#include <qsocketnotifier.h>
#include "init.h"
#include <malloc.h>
#include <qmessagebox.h>
void client::sendToServer()
{
FakeAddress =host->text(); //取得本机接口IP
MyAddress.setAddress(FakeAddress);
MUReceiveSocket->setBlocking(false);
int a=port1->text().toInt();
MUReceiveSocket->bind(MyAddress,a);
//绑定到指定网络接口地址(IP),指定逻辑端口
if(inputText->text()!="")
{
FakeAddress =guest->text();//取得对方接口IP
HeAddress.setAddress(FakeAddress);
MUReceiveSocket->setBlocking(false);
int b=port2->text().toInt();
QTime time=QTime::currentTime();
QString strtime=time.toString("h:m:s");
char information[10000];
strcpy(information,username->text()+":"+inputText->text());
int length=strlen(information);
MUReceiveSocket->writeBlock(information,length,HeAddress,b);
infoText->insert("("+strtime+")"+username->text()+":"+inputText->text()+"\n");
inputText->clear();
}
}
void client::closeConnection()
{
FakeAddress =guest->text();//取得对方接口IP
HeAddress.setAddress(FakeAddress);
MUReceiveSocket->setBlocking(false);
int b=port2->text().toInt();
char* information="系统提示:对方已关闭连接!";
int length=strlen(information);
MUReceiveSocket->writeBlock(information,length,HeAddress,b);
MUReceiveSocket->close ();
QMessageBox::information(this, "系统提示:", "连接已断开!");
close->setEnabled(false);
send->setEnabled(false);
inputText->setEnabled(false);
}
void client::Connect()
{
infoText->clear();
close->setEnabled(true);
send->setEnabled(true);
inputText->setEnabled(true);
host->setEnabled(false);
port1->setEnabled(false);
guest->setEnabled(false);
port2->setEnabled(false);
MUReceiveSocket->setBlocking(false);
MSocketNotifier = new QSocketNotifier(MUReceiveSocket->socket(),QSocketNotifier::Read,0,"MSocketNotifier");
//监听MUReceiveSocket套接字
connect(MSocketNotifier,SIGNAL(activated(int)),this,SLOT(OnMReceive()));
//当MSocketNotifier检测到MUReceiveSocket活跃时调用OnMReceive
connection->setEnabled(false);
}
void client::OnMReceive()
{
int ByteCount,ReadCount;
char* IncommingChar=NULL;
QTime time=QTime::currentTime();
QString strtime=time.toString("h:m:s");
FakeAddress =host->text(); //取得接口IP
MyAddress.setAddress(FakeAddress);
MUReceiveSocket->setBlocking(false);
int a=port1->text().toInt();
MUReceiveSocket->bind(MyAddress,a);
//绑定到指定网络接口地址(IP),指定逻辑端口
ByteCount=MUReceiveSocket->bytesAvailable();
IncommingChar=(char *)malloc(ByteCount);
IncommingChar[ByteCount]=NULL;
ReadCount=MUReceiveSocket->readBlock(IncommingChar,ByteCount);
IncommingChar[ReadCount]=NULL;
if(IncommingChar[0]!=NULL)
{
infoText->insert("("+strtime+")"+IncommingChar+"\n");
IncommingChar[ByteCount]='\0';
}
}
[ 此贴被XChinux在2008-07-18 12:00重新编辑 ]