客户端:
ui->plainTextEdit(文本编辑器)里边有中文英文还有日期,相当于日志文件。
// 不是简单的“中文”字符串,是放在QString变量里的,转换成QByteArray后就是另外一种乱码。。。。
QString str = ui->plainTextEdit->toPlainText( );。
QByteArray byData;//要发送给服务器端的数据包。
QByteArray byToken = QString( "FutureInternet" ).toAscii( );。
QByteArray byBody = "select log table";。
// 通过这个转换似乎丢了些数据。还是乱码。
QString text = QString::fromLocal8Bit( (const char *)&str, str.length() );。
qDebug() << text << endl;。
byBody.append( text );。
quint32 nLength = byToken.length( ) +。
sizeof ( quint32 ) +。
byBody.length( );。
nLength = htonl( nLength );。
byData.append( byToken );。
byData.append( ( const char* ) &nLength,。
sizeof ( quint32 ) );。
byData.append( byBody );。
clientThread->PostDataEvent( byData );。
这是因为文件路径不对造成的,解决方法如下:
1、首先pro文件配置:Qt网络功能需要在pro文件增加网络库。
2、QTcpServer服务端建立的方法代码,如下图所示。
3、QTcpServer当有新客户端连接时,会发出QTcpServer::newConnection的信号方法代码。
4、客户端为主动连接方不需要监听,直接建立QTcpSocket代码。
5、最后通过connectToHost连接指定ip和端口,将socket的连接成功的信号与对应槽连接,当连接成功可以将自定义的标记位置为true。
一般协议相同就可以通信啊,和语言关系不大。你可以尝试DatagramSocket和DatagramPacket类是基于UDP协议的。
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;。
/*其他的内容对你来说也没用了*/。
你只要保证这个Socket的生存周期足够在别的界面里面使用他就好了,主界面建立好连接,设置对了IP端口等等的,在别的地方直接调用对象调用write()函数就行了(不知道你用的是TCP还是UDP,具体的函数有点区别但大致差不多)
原文地址:http://www.qianchusai.com/qtsocket%E9%80%9A%E4%BF%A1.html