本文整理汇总了C++中TcpSocket类的典型用法代码示例。如果您正苦于以下问题:C++ TcpSocket类的具体用法?C++ TcpSocket怎么用?C++ TcpSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TcpSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadData
virtual int ReadData(int fd)
{
int err = 0;
int clientFd = -1;
std::string ip;
int port = 0;
do {
err = mListenSocket->Accept(clientFd, ip, port);
if (0 != err) {
break;
}
TcpSocket *clientSocket = new TcpSocket();
err = clientSocket->Create(clientFd);
if (0 != err) {
break;
}
debug_log("accpeter accept client " << ip << ":" << port);
clientSocket->SetAddr(ip, port);
AcceptContext *ctx = new AcceptContext(clientSocket);
mNetService->Enqueue(ctx);
OperContext::DecRef(ctx);
} while(0);
return 0;
}
示例2: c_communication
void c_communication(c_serveur& sett)
{
TcpSocket svr;
svr.connect(sett.c_ip,55001);
c_infoPlayer<<c_pseudo;
svr.send(c_infoPlayer);
cout<<"Communication lancee"<<endl;
while(c_connected)
{
c_attendre.lock();
svr.send(c_sortant);
c_sortant.clear();
c_sortant<<false<<"";
c_attendre.unlock();
bool a;
string b;
if(svr.receive(c_entrant) == Socket::Done)
{
//system("cls");
c_entrant>>a>>b;
if(!a)
c_msgsRecus = b;
}
else
{
示例3: client
void client()
{
TcpSocket socket;
Socket::Status status = socket.connect(IpAddress::getLocalAddress(), DH_PORT);
if (status != Socket::Status::Done)
{
perror("connection to server failed");
return;
}
mpz_t secret;
CurveRef curve = client_receive_curve(socket);
PointRef p = client_create_key(socket, curve, secret);
client_send_key(socket, p);
PointRef remoteKey = client_receive_key(socket);
PointRef sharedSecret = PointCreateMultiple(remoteKey, secret, curve);
char *desc = PointCreateDescription(sharedSecret);
cout << "Secret: " << desc << endl;
free(desc);
mpz_clear(secret);
CurveDestroy(curve);
PointDestroy(p);
PointDestroy(remoteKey);
PointDestroy(sharedSecret);
socket.disconnect();
}
示例4: ASSERT_THROW
void TcpSocketServer::broadcast(DataToSend* data)
{
if (data == NULL) return;
if (data->getType() != DataToSend::DS_READER)
{
for (Clients::iterator itr = _clients.begin(), end = _clients.end(); itr != end; ++itr)
{
TcpSocket* client = *itr;
client->send(data);
}
}
else
{
Ref<StreamReader> reader = data->getReader();
ASSERT_THROW(reader->isSeekable(), EX_NOT_SUPPORTED);
size_t pos = reader->tell();
for (Clients::iterator itr = _clients.begin(), end = _clients.end(); itr != end; ++itr)
{
reader->seek(pos);
TcpSocket* client = *itr;
client->send(data);
}
}
}
示例5: dabInputSlipThread
void* dabInputSlipThread(void* args)
{
dabInputSlipData* data = (dabInputSlipData*)args;
TcpSocket* client;
while ((client = data->server->accept()) != NULL) {
int size = 0;
etiLog.log(info, "SLIP server got a new client.\n");
#ifdef _WIN32
WaitForSingleObject(data->semWrite, INFINITE);
WaitForSingleObject(data->semQueue, INFINITE);
#else
sem_wait(&data->semWrite);
sem_wait(&data->semQueue);
#endif
UdpPacket* packet = data->packets[data->nbPackets];
#ifdef _WIN32
ReleaseSemaphore(data->semQueue, 1, NULL);
#else
sem_post(&data->semQueue);
#endif
while ((size = client->read(packet->getData(), packet->getSize()))
> 0) {
packet->setLength(size);
#ifdef _WIN32
WaitForSingleObject(data->semQueue, INFINITE);
#else
sem_wait(&data->semQueue);
#endif
data->nbPackets++;
#ifdef _WIN32
ReleaseSemaphore(data->semQueue, 1, NULL);
#else
sem_post(&data->semQueue);
#endif
#ifdef _WIN32
WaitForSingleObject(data->semWrite, INFINITE);
WaitForSingleObject(data->semQueue, INFINITE);
#else
sem_wait(&data->semWrite);
sem_wait(&data->semQueue);
#endif
packet = data->packets[data->nbPackets];
#ifdef _WIN32
ReleaseSemaphore(data->semQueue, 1, NULL);
#else
sem_post(&data->semQueue);
#endif
}
etiLog.log(info, "SLIP server client deconnected.\n");
client->close();
}
etiLog.log(error, "SLIP thread can't accept new client (%s)\n",
inetErrDesc, inetErrMsg);
return NULL;
}
示例6: sizeof
//======================================================================================
TcpSocket* TcpSocket::Accept() {
struct sockaddr_in sa;
socklen_t len = sizeof(sa);
Log.i(SOCK_TAG, "TcpSocket::Accept m_iSocket=%u", m_iSocket);
int ret = accept(m_iSocket, (struct sockaddr*) &sa, &len);
if (-1 == ret){
Log.e(SOCK_TAG, "TcpSocket::Accept -1!");
return NULL;
}
if (0 == ret) {
Log.e(SOCK_TAG, "TcpSocket::Accept 0!");
return NULL;
}
TcpSocket *pNew = new TcpSocket(m_pSelector);
pNew->m_iSocket = ret;
//pNew->m_ip = ntohl(sa.sin_addr.s_addr);
//pNew->m_iPort = ntohs(sa.sin_port);
pNew->m_ip = sa.sin_addr.s_addr;
pNew->m_iPort = sa.sin_port;
pNew->m_bConnected = true;
pNew->m_iLastRecvTime = SelectorEPoll::getInstance().m_iHaoMiao;
pNew->setTimeout(m_iTimeout);
pNew->SetNBlock();
Log.i(SOCK_TAG, "TcpSocket accept %s:%u", ip2str(pNew->m_ip), pNew->m_iPort);
return pNew;
}
示例7: run
void run()
{
TcpSocket socket;
socket.connect("127.0.0.1", mPort);
T::Send(socket, mValue);
}
示例8: onHeadersComplete
int onHeadersComplete(http_parser *parser)
{
TcpSocket *socket = static_cast<TcpSocket*>(parser->data);
#ifndef NO_LOG
sLog(LogEndpoint::LogLevel::DEBUG) << " === parsed header ====";
const QHash<QString, QSharedPointer<QString>> &headers = socket->getHeader().getHeaderInfo();
QHash<QString, QSharedPointer<QString>>::const_iterator i = headers.constBegin();
while (i != headers.constEnd())
{
sLog(LogEndpoint::LogLevel::DEBUG) << i.key() << *(i.value().data());
++i;
}
sLog(LogEndpoint::LogLevel::DEBUG) << " === ============= ====";
sLogFlush();
#endif
QWeakPointer<QString> host = socket->getHeader().getHeaderInfo("Host");
if (!host.isNull())
{
socket->getHeader().setHost(*host.data());
}
return 0;
}
示例9: TcpSocket
/**
*函数介绍:有请求连接,新建一个socket
*输入参数:int socketId,新建socket的SocketDescriptor
*返回值:无
*/
void TcpServer::incomingConnection(int socketId)
{
TcpSocket *socket = new TcpSocket(socketId,this);
//设置socket描述符
socket->setSocketDescriptor(socketId);
//写入系统日志
//Global::systemLog->append(tr("网络通信服务端有新的连接请求"), QString(tr("服务端接收新的连接请求,socket描述符 = %1.")).arg(socketId), SystemLog::INFO);
qDebug()<<"服务端有新的连接,socketDescriptor:"<<socket->socketDescriptor();
//新建了一个socket连接
addServerSocket(socketId);
//获得客户端数据
connect(socket, SIGNAL(haveReadDataSignal(QByteArray,int)), this, SLOT(getClientMessage(QByteArray,int)));
//信号槽:跟踪服务端Socket状态
connect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(getServerSocketState(QAbstractSocket::SocketState)));
//信号槽:服务端socket出现错误
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverSocketError(QAbstractSocket::SocketError)));
//与客户端断开某socket连接,服务端状态发生改变
connect(socket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(removeServerSocket(int)));
//与客户端断开连接,输出服务器当前连接
//connect(socket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(getAllConnection()));
//与客户端连接上后,输出服务器当前连接
//connect(socket, SIGNAL(connectedForServerSignal(int)), this, SLOT(getAllConnection()));
//与客户端连接上后,输出服务器当前连接
//connect(socket, SIGNAL(connectedForServerSignal(int)), this, SLOT(addServerSocket(int)));
}
示例10: on_connect
int FileService::on_connect(Socket *socket)
{
TcpSocket * tcpsock = dynamic_cast<TcpSocket *> (socket);
tcpsock->set_recv_buffer_size(1000 * 1024);
//dlog1("FileServer new connection %s %d \n", socket->remote_addr.get_ip().c_str(), socket->remote_addr.get_port());
return FSM_END;
}
示例11: broadcast
void TcpSocketServer::broadcast(const void* data, size_t size)
{
for (Clients::iterator itr = _clients.begin(), end = _clients.end(); itr != end; ++itr)
{
TcpSocket* client = *itr;
client->send(data, size);
}
}
示例12: Accept
bool Accept(TcpSocket& clientSocket) const
{
clientSocket.Close();
InetAddr& clientAddr = clientSocket.Addr();
int connFd = accept(m_Fd, (struct sockaddr*)&clientAddr.m_Addr, &clientAddr.m_AddrLen);
clientSocket.m_Fd = connFd;
return connFd != -1;
}
示例13: on_actionCreateTcpClient_triggered
void ChooseConnectionDlg::on_actionCreateTcpClient_triggered()
{
TcpSocket * port = sConMgr2.createTcpSocket();
port->setName(tr("New TCP client"));
port->setHost("localhost");
port->setPort(80);
this->focusNewConn(port);
}
示例14: LOG
BOOL WINAPI SkylarkApp::CtrlCHandlerRoutine(__in DWORD dwCtrlType)
{
LOG("Ctrl+C handler");
TheApp().quit = true;
Cout() << "Ctrl + C\n";
TcpSocket h;
h.Connect("127.0.0.1", TheApp().port);
h.Put("quit");
return TRUE;
}
示例15: Broadcast
void SkylarkApp::Quit()
{
quit = true;
#ifdef PLATFORM_POSIX
Broadcast(SIGTERM);
#endif
TcpSocket s;
s.Timeout(100);
s.Connect("127.0.0.1", port);
}