#include "qqserver.h"
#include "ui_qqserver.h"
#include <stdio.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <QTextCodec>
QVector<Client> c(50);
int sockfd;
int size=0;
char* IP="127.0.0.1";
short PORT=10000;
QQServer::QQServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::QQServer)
{
ui->setupUi(this);
init();
}
QQServer::~QQServer()
{
delete ui;
}
void QQServer::init()
{
ui->show_text->setText(tr("服务器开始启动。。。"));
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1){
ui->show_text->append(tr("创建socket失败"));
ui->show_text->append(tr("服务器启动失败"));
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=inet_addr(IP);
//绑定socket和通信地址
if(bind(sockfd,(struct sockaddr*)&addr,sizeof(addr))==-1){
ui->show_text->append(tr("绑定失败"));
ui->show_text->append(tr("服务器启动失败"));
exit(-1);
}
ui->show_text->append(tr("设置监听成功"));
ui->show_text->append(tr("初始化服务器成功"));
}
void QQServer::sendMsgToAll(char* msg)
{
int i=0;
for(;i <size ;i++)//这行报错
{
if(c[i].fds!=0){
send(c[i].fds,msg,strlen(msg),0);
}
}
}