日期:2014-05-16  浏览次数:20912 次

如何在qt中使用connect指令来写socket?
如何在qt中使用connect指令来写socket?

我想用connect指令来设计socket
但是 qt 自动把 connect 当成传递信号了 也就是qobject里面的connect了
请问我怎样才能用 connect 链接服务器呢?

------解决方案--------------------
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 socka