⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renjusocket.cpp

📁 linux下的网络五指棋。用QT做的界面。
💻 CPP
字号:
#include "renjusocket.h"
RenjuSocket::RenjuSocket( int sock, QObject *parent=0, const char *name=0 ) : QSocket(parent, name)
{
	connect( this, SIGNAL(readyRead()), SLOT(read()) );
    connect( this, SIGNAL(connectionClosed()),SLOT(connectionClosed())); 
	setSocket( sock );
	ts = new QTextStream(this);
}

RenjuSocket::RenjuSocket( QString &host, QObject *parent=0, const char *name=0 ) : QSocket(parent, name)
{
	connect( this, SIGNAL(readyRead()), SLOT(read()) );
	this->connectToHost( host, 4242 );
	ts = new QTextStream(this);
}

void RenjuSocket::connectionClosed()
{
    //qDebug("hgfhffgg");
    emit clientconnectionClosed();
}

void RenjuSocket::read()
{
	while ( canReadLine() ) 
	{
	    QString str = readLine();
		QString s = str.section('/',0,0);
		//qDebug(s);
		if (s == "msg")
			{
			s = str.section('/',1,2);
			
			
			parser(s);
			}
		
		if (s =="position")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			zhouqi(a,b);
			}
		if (s == "done")
			{
            sendclinetdone();
				    
			}
		if (s == "agreetohuiqi")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			sendagreetohuiqi(a,b);
			}
        if (s == "donotagreetohuiqi")
        	{
        	senddonotagreetohuiqi();
        	}
		if (s == "clienthuiqi")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			qDebug("%d,%d",a,b);
			emit clienthuiqi(a,b);
			}
		if (s == "agreetorestart")
			{
			emit clientrestart();
			}
		if (s == "clientrestart")
			{
			emit clientnewrequest();
			}
		if (s == "donotagreetorestart")
			{
			emit clientdonotagreetonew();
			}
	}
}

void RenjuSocket::parser(QString &str)
{
      emit recvMsg(str);	
}

void RenjuSocket::sendclinetdone()
{

    emit clinetdone();
}

void RenjuSocket::sendMsg(QString &msg)
{
	*ts<<"msg"<<"/"<<msg<< "\n";	
}
void RenjuSocket::zhouqi(int x, int y)//, int color)
{
        
	  emit parserzhouqi(x,y);
}
void RenjuSocket::serverzhouqi(int x,int y)
{
       *ts <<"position"<<"/"<<x<<"/"<<y<<"\n";
}
void RenjuSocket::sendDone(int x,int y)
{
     *ts<<"done"<<"/"<<x<<"/"<<y<<"\n";
}
void RenjuSocket::sendhuiqi(int x,int y)
{
      *ts<<"huiqi"<<"/"<<x<<"/"<<y<<"\n";
}
void RenjuSocket::sendagreetohuiqi(int x,int y)
{
      emit clientagreetohuiqi(x,y);
}
void RenjuSocket::senddonotagreetohuiqi()
{
      emit clientdonotagreetohuiqi();
}
void RenjuSocket::reqHuiqiRenju(int x,int y)
{
      *ts<<"serveragreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
}
void RenjuSocket::reqHuiqiRenjuno(int x,int y)
{
      *ts<<"serverdonotagreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
}
void RenjuSocket::reqNewRenju()
{
      *ts<<"new"<<"/"<<"\n";
}
void RenjuSocket::serveragreetorestart()
{
      *ts<<"newdone"<<"/"<<"\n";
}
void RenjuSocket::serverdonotagreetorestart()
{
      *ts<<"newdenied"<<"/"<<"\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -