服務端
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();
}