服务端
scoket.h
#ifndef SOCKET_H
#define SOCKET_H
#pragma execution_character_set("utf-8")
#include <QtGui/QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include<QTextCodec>
#include "ui_socket.h"
class Socket : public QWidget
{
Q_OBJECT
public:
Socket(QWidget *parent = 0, Qt::WFlags flags = 0);
~Socket();
private:
Ui::SocketClass ui;
QTcpServer *tcpSever;//监听套接字
QTcpSocket *tcpSocket; //通信套接字
public slots:
void fun();
void get_message() ;
void send_message();
void close_clicked();
};
#endif // SOCKET_H
scoket.cpp
#include "socket.h"
Socket::Socket(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
setWindowTitle(QString::fromUtf8("服务端 ,端口8888"));
tcpSocket = NULL;
tcpSever = NULL;
tcpSever = new QTcpServer(this) ;
//监听地址,和8888端口
tcpSever->listen(QHostAddress::Any, 8888) ;
//如果有客户端连接成功,进入fun函数
connect(tcpSever, SIGNAL(newConnection()), this, SLOT(fun()));
}
Socket::~Socket()
{
}
void Socket::fun()
{
//取出建立好的套接字
tcpSocket = tcpSever->nextPendingConnection() ;
//获取对方的IP和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 prot = tcpSocket->peerPort();
QString temp = QString(QString::fromUtf8("[%1:%2]:成功连接")).arg(ip).arg(prot);
ui.textEditRead->setText(temp) ;
//有消息发过来,就会触发readyRead信号
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(get_message())) ;
}
void Socket::get_message()
{
//将信号显示到read文本框
QByteArray array = tcpSocket->readAll();
ui.textEditRead->append(array);
}
void Socket::send_message()
{
//向socket写数据
if(tcpSocket == NULL)
return ;
QString str = ui.textEditWrite->toPlainText();
tcpSocket->write(str.toUtf8().data());
}
void Socket::close_clicked()
{
if(tcpSocket == NULL)
return ;
//断开连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = NULL;
}
客户端
client.h
#ifndef CLIENT_H
#define CLIENT_H
#pragma execution_character_set("utf-8")
#include <QtGui/QWidget>
#include <QTcpSocket>
#include "ui_client.h"
class Client : public QWidget
{
Q_OBJECT
public:
Client(QWidget *parent = 0, Qt::WFlags flags = 0);
~Client();
private:
Ui::ClientClass ui;
QTcpSocket *tcpSocket;
public slots:
void set_message();
void connect_to_sever();
void send_message();
void get_message();
void close_clicked();
};
#endif // CLIENT_H
client.cpp
#include "client.h"
Client::Client(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
setWindowTitle(QString::fromUtf8("客户端")) ;
tcpSocket = NULL;
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(set_message())) ;
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(get_message()));
}
Client::~Client()
{
}
void Client::set_message()
{
ui.textEditReader->setText("succed connect");
}
void Client::connect_to_sever()
{
//获取服务器ip和端口
QString ip = ui.lineEditIp->text();
qint16 port = ui.lineEditprot->text().toInt();
tcpSocket->connectToHost(ip, port);
}
void Client::send_message()
{
// 向socket写数据
QString str = ui.textEditWrite->toPlainText();
tcpSocket->write(str.toUtf8().data());
}
void Client::get_message()
{
QByteArray array = tcpSocket->readAll();
ui.textEditReader->append(array);
}
void Client::close_clicked()
{
tcpSocket->disconnectFromHost();
tcpSocket->close();
}