#include "tcpclient.h"
#include "ui_tcpclient.h"
#include <QHostInfo>
#include <QMessageBox>
#include<QDebug>
#include<QByteArray>
#include<QTextCodec>
TcpClient::TcpClient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TcpClient)
{
ui->setupUi(this);
setWindowTitle(tr("TCP Client"));
serverIP = new QHostAddress();
connect(ui->enterBtn,SIGNAL(clicked()),this,SLOT(slotEnter()));
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(slotSend()));
ui->sendButton->setEnabled(false);
this->status=false;
}
TcpClient::~TcpClient()
{
delete ui;
}
void TcpClient::slotEnter()
{
if(!status)
{
QString ip = ui->serverIPLineEdit->text();
if(!serverIP->setAddress(ip))
{
QMessageBox::information(this,tr("error"),tr("server ip address error!"));
return;
}
if(ui->userNameLineEdit->text()==" ")
{
QMessageBox::information(this,tr("error"),tr("User name error!"));
return;
}
userName = ui->userNameLineEdit->text();
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotError(QAbstractSocket::SocketError)));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
port=ui->portLineEdit->text().toInt();
tcpSocket->connectToHost(*serverIP,port);
status=true;
}
else
{
int length = 0;
QString msg = userName+tr(":leave chat Room");
if((length=tcpSocket->write(msg.toLatin1(),msg.length()))!=msg.length())
{
return;
}
tcpSocket->disconnectFromHost();
status=false;
}
}
void TcpClient::slotConnected()
{
ui->sendButton->setEnabled(true);
ui->enterBtn->setText(tr("leave"));
int length = 0;
QString msg = userName + tr(":Enter Chat room");
if((length=tcpSocket->write(msg.toLatin1(),msg.length()))!=msg.length())
{
return;
}
}
void TcpClient::slotError(QAbstractSocket::SocketError)
{
QMessageBox::information(this,tr("Error"),tr("Error"));
}
void TcpClient::slotSend()
{
if(ui->sendLineEdit->text()==" ")
{
return;
}
QString msg = userName+":"+ui->sendLineEdit->text();
tcpSocket->write(msg.toLatin1(),msg.length());
ui->sendLineEdit->clear();
}
void TcpClient::dataReceived()
{
while(tcpSocket->bytesAvailable()>0)
{
QByteArray datagram;
datagram.resize(tcpSocket->bytesAvailable());
QHostAddress sender;
tcpSocket->read(datagram.data(),datagram.size());
QString msg = datagram.data();
ui->contentListWidget->addItem(msg.left(datagram.size()));
}
}
void TcpClient::slotDisconnected()
{
QMessageBox::information(this,tr("Disconnected!"),tr("Disconnected!"));
ui->sendButton->setEnabled(false);
ui->enterBtn->setText(tr("Enter"));
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Server&Client.rar (17个子文件)
Server&Client
TcpServer
tcpserver.ui 3KB
main.cpp 191B
tcpserver.h 473B
TcpClientSocket.cpp 595B
TcpServer.pro.user 28KB
Server.h 514B
TcpServer.pro 242B
TcpClientSocket.h 418B
tcpserver.cpp 1KB
Server.cpp 1KB
TcpClient
main.cpp 198B
TcpClient.pro 169B
Setup.SED 1KB
tcpclient.h 634B
TcpClient.pro.user 28KB
tcpclient.ui 3KB
tcpclient.cpp 3KB
共 17 条
- 1
资源评论
Victor_YXL
- 粉丝: 4
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功