qtsocket編程
① QT 編程QTcpSocket 類write函數
if(m_httpAddress.isEmpty() || sendBuffer.isEmpty())
{
return 0;
}//伺服器地址或者需要發送的數據為空直接返回
QTcpSocket socket;
socket.connectToHost(m_httpAddress, m_httpPort);//建立一個TCP連接,主機地址是m_httpAddress,埠號是httpPort
socket.setSocketOption(QAbstractSocket::LowDelayOption, 1);//優化為最低延遲,後面的1代碼啟用該優化。
if (!socket.waitForConnected())
{
return 0;
}
//等待連接,如果超過3s沒有客戶端連接將退出。
socket.write(sendBuffer);//開始傳輸數據
socket.waitForBytesWritten();
while (socket.waitForReadyRead(60000))//在6s內完成數據的傳輸
{
while (socket.bytesAvailable())
{
receiveBuffer += socket.readAll();
}//如果傳輸數據不為0,那麼接受數據buffer加上該值
}
socket.close();//關閉I/O數據傳輸以及Tcp連接,並重置主機名和埠號
② 如何在qt中使用connect指令來寫socket
C/C++ code/*server.h 就是server的頭文件*/
#ifndef SERVER_H
#define SERVER_H
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <qstring.h>
#include <qapplication.h>
#include <qthread.h>
#include <qsignal.h>
#include <qobject.h>
#include <qvariant.h>
#include <qdialog.h>
#include <qwidget.h>
#include "userevent.h"
#include "VPN_usr.h"
//# define RECVBUF 140
//# define BACKLOG 10
typedef struct data
{
char command[20];
char parm[20];
char context[100];
}Data;
class server : public QThread
{
public:
int new_fd;
unsigned char from_client[140];
UserEvent *usre;
QString str;
QObject test;
void set_target(QWidget *parent);
server();
~server();
void run();
void stop();
void VPN_encrypt_send();
void VPN_certification_send();
private:
Data pData;
volatile bool stopped;
QWidget *parent_m;
int err;
unsigned char c;
int i;
int reclen;
int sockfd;
int namelen;
int portnum;
int sin_size;
int addrsize;
struct sockaddr_in server_addr,client_addr,addr;
unsigned char to_client[140];
};
#endif
/*server.cpp */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <qapplication.h>
#include <qthread.h>
#include <qsignal.h>
#include <qobject.h>
#include <qdialog.h>
#include <qvariant.h>
#include"encrypt.h"
#include"certification.h"
#include "VPN_usr.h"
#include"server.h"
#include "userevent.h"
server::server()
{
portnum=5554;
stopped= false;
err=0;
new_fd=0;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("error in socket!\n");
err=1;
}
namelen=sizeof(struct sockaddr_in);
bzero(&server_addr,namelen);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnum);
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(server_addr.sin_zero),8);
if ((bind(sockfd, (struct sockaddr *)(&server_addr), namelen))<0)
{
printf(" error in binding!\n");
err=1;
}
printf("Already bind\n");
}
server::~server()
{
stopped =true;
}
void server::run()
{
while(!stopped)
{
if ((listen(sockfd, 10))<0)
{
printf(" error in listening!\n");
err=1;
}
printf("Listening now\n");
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr), (socklen_t *)&sin_size))<0)
{
printf("error in accept!\n");
err=1;
}
if(new_fd!=0)
printf(" connected with client!\n");
printf("Starting communication with client %s .\n",inet_ntoa(client_addr.sin_addr));
addrsize=sizeof(struct sockaddr_in);
getpeername(new_fd,(struct sockaddr*)&addr,(socklen_t *)&addrsize);
bzero(to_client, 140);
bzero(from_client, 140);
if(recv(new_fd,&c,1,MSG_PEEK)==-1)
{
printf(" Error when accept data from client!\n");
close(new_fd);
}
if(c==0xff)
{
i=0;
while((reclen=recv(sockfd,&c,1,0)) > 0 && c!=0xfe)
{
from_client[i++]=c;
}
if(reclen<0)
{
printf(" Error when accept more data from client!\n");
close(new_fd);
}
else if(c==0xfe)
from_client[i]=c;
switch(from_client[1])
{
case 0x0a:
printf("receive sa data from client.\n");
//emit show_sa();
//QApplication::postEvent(parent_m, usre);
break;
default:
printf("the data is does not follow the protocal.\n");
break;
}
}
}
stopped = false;
}
void server::stop()
{
stopped =true;
}
/*其他的內容對你來說也沒用了*/
③ qt socket編程問題,不能接收數據,懂的幫忙解答一下,謝謝了。
接收函數內,一開始就readAll(),已經把數據都讀取了,所以後面就沒有可讀數據了,所以後面不用判斷bytesAvaliables()。
接收的時候,不要直接接收到QString中。應該先讀取一個長度值(quint16),再根據長度值去讀取QByteArray,再把讀取到的QByteArray放到QString中。然後再判斷bytesAvaliables是否還有剩餘。有剩餘,就再接收長度值+QByteArray。。。。