本文整理汇总了C++中TCPSocket类的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket类的具体用法?C++ TCPSocket怎么用?C++ TCPSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TCPSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendEmail
bool Email::sendEmail( const std::string &smtpServer, u16 port, const std::string &from, const std::string &to, const std::string &subject, const std::string &body, const std::string &attachedFile /*= ""*/, bool onlyCheck /*= false */ )
{
bool ok = false;
TCPSocket sock;
Address ad(smtpServer, port);
sock.connect(ad);
if (sock.connected())
{
// we must skip the first line
std::string formatedBody = "\r\n";
if(!sendEMailCommand (sock, "", 220)) goto end;
if(onlyCheck)
{
if(!sendEMailCommand (sock, "HELO localhost")) goto end;
if(!sendEMailCommand (sock, "MAIL FROM: " + from)) goto end;
if(!sendEMailCommand (sock, "RCPT TO: " + to)) goto end;
if(!sendEMailCommand (sock, "QUIT", 221)) goto end;
ok = true;
}
else
{
if(!sendEMailCommand (sock, "HELO localhost")) goto end;
//if(!sendEMailCommand (sock, "STARTTLS", 220)) goto end;
//if(!sendEMailCommand (sock, "AUTH PLAIN AHVzZXIAc2VjcmV0")) goto end;
if(!sendEMailCommand (sock, "AUTH LOGIN", 334)) goto end;
if(!sendEMailCommand (sock, "bccr001")) goto end;
if(!sendEMailCommand (sock, "MAIL FROM: " + from)) goto end;
if(!sendEMailCommand (sock, "RCPT TO: " + to)) goto end;
if(!sendEMailCommand (sock, "DATA", 354)) goto end;
std::string buffer =
"From: " + from + "\r\n"
"To: " + to + "\r\n"
"Subject: " + subject + "\r\n"
+ formatedBody + "\r\n.";
if(!sendEMailCommand (sock, buffer)) goto end;
if(!sendEMailCommand (sock, "QUIT", 221)) goto end;
ok = true;
}
end:
sock.close();
}
else
{
MessageBox(NULL, smtpServer.c_str(), "woops", MB_OK);
}
return ok;
}
示例2: main
int main(int argc, char** argv){
char* hname;
char* sname;
debug_lib::init(argv[0]);
if ( argc == 2 ){
hname = NULL;
sname = argv[1];
}else if ( argc == 3 ){
hname = argv[1];
sname = argv[2];
}else{
debug_lib::log( "exit: wrong arguments passed %d\n", argc );
print_help();
exit(1);
}
try{
TCPServerSocket* sock = new TCPServerSocket();
sock->bind(hname, sname);
sock->listen();
do{
while( true ){
TCPSocket* acceptsock = sock->accept();
MessageFramer* framer = new FixedLenFramer(sizeof(nowait_struct));
TCPAcceptSocketNoWait* acceptsocknw =
new TCPAcceptSocketNoWait( acceptsock->getFd(), framer);
Message msg;
msg.setFramer(framer);
struct nowait_struct stmsg;
strcpy( stmsg.str, "hello world in a structure" );
msg.putDataRaw( (char*)&stmsg, sizeof(nowait_struct) );
acceptsocknw->send( msg );
//delete only closes the socket from the server side. The socket is still open on the client side.
delete acceptsock;
delete framer;
delete acceptsocknw;
}
} while(1);
}catch(debug_lib::Exception& e){
debug_lib::log( "exit due to error in server: %s", e.what());
exit(1);
}catch(...){
debug_lib::log( "exit due to error in server: %s", "unknown error" );
exit(1);
}
return 0;
}
示例3: GetSessionTask
Task* CRTConnListener::GetSessionTask(int osSocket, struct sockaddr_in* addr)
{
TCPSocket* theSocket = NULL;
Assert(osSocket != EventContext::kInvalidFileDesc);
// when the server is behing a round robin DNS, the client needs to knwo the IP address ot the server
// so that it can direct the "POST" half of the connection to the same machine when tunnelling RTSP thru HTTP
CRTConnection* theTask = new CRTConnection();
if(NULL == theTask)
return NULL;
theSocket = theTask->GetSocket(); // out socket is not attached to a unix socket yet.
//set options on the socket
int sndBufSize = 96L * 1024L;
theSocket->Set(osSocket, addr);
theSocket->InitNonBlocking(osSocket);
//we are a server, always disable nagle algorithm
theSocket->NoDelay();
theSocket->KeepAlive();
theSocket->SetSocketBufSize(sndBufSize);
//setup the socket. When there is data on the socket,
//theTask will get an kReadEvent event
theSocket->RequestEvent(EV_RE);
theTask->SetTimer(30*1000);
StrPtrLen* remoteStr = theSocket->GetRemoteAddrStr();
LI("CRTConnListener Get a connection,ip:%.*s port:%d \n",remoteStr->Len, remoteStr->Ptr, ntohs(addr->sin_port));
this->RunNormal();
return theTask;
}
示例4: Init
void LRTGroupSession::Init()
{
TCPSocket* socket = this->GetSocket();
socket->Open();
socket->InitNonBlocking(socket->GetSocketFD());
socket->NoDelay();
socket->KeepAlive();
socket->SetSocketBufSize(96L * 1024L);
socket->SetTask(this);
this->SetTimer(120*1000);
for(int i=0;i<MSG_PACKED_ONCE_NUM;++i)
{
m_packedNewMsg.add_msgs();
}
for(int i=0;i<MSG_PACKED_ONCE_NUM;++i)
{
m_packedSeqnMsg.add_msgs();
}
for(int i=0;i<MSG_PACKED_ONCE_NUM;++i)
{
m_packedDataMsg.add_msgs();
}
}
示例5: TCPSOCKET_OPEN_CLOSE_REPEAT
void TCPSOCKET_OPEN_CLOSE_REPEAT()
{
SKIP_IF_TCP_UNSUPPORTED();
TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
}
for (int i = 0; i < 2; i++) {
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
}
delete sock;
}
示例6: _tcpsocket_connect_to_chargen_srv
static nsapi_error_t _tcpsocket_connect_to_chargen_srv(TCPSocket &sock)
{
SocketAddress tcp_addr;
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(19);
nsapi_error_t err = sock.open(get_interface());
if (err != NSAPI_ERROR_OK) {
return err;
}
return sock.connect(tcp_addr);
}
示例7: TCPSOCKET_RECV_100K
void TCPSOCKET_RECV_100K()
{
TCPSocket sock;
_tcpsocket_connect_to_chargen_srv(sock);
Timer timer;
timer.start();
rcv_n_chk_against_rfc864_pattern(sock);
timer.stop();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
printf("MBED: Time taken: %fs\n", timer.read());
}
示例8: PeersRequestsDispatcher
void TCPMessengerServer::run()
{
m_isRunning = true;
m_dispatcher = new PeersRequestsDispatcher(this);
m_dispatcher->start();
while (m_isRunning)
{
TCPSocket* peerSocket = m_tcpServerSocket->listenAndAccept();
if (peerSocket != NULL)
{
cout << "new peer connected: " << peerSocket->GetDestIpAndPort() << endl;
m_openedPeers[peerSocket->GetDestIpAndPort()] = peerSocket;
}
}
}
示例9: while
void MsaTcpServer::run()
{
_dispatcher->start();
while(true)
{
TCPSocket* newPeer = _serverSock->listenAndAccept();
if (newPeer!= NULL)
{
cout<<"Add Peer"<<newPeer->destIpAndPort()<<endl;
_dispatcher->addPeer(newPeer);
}
}
}
示例10: FD_ZERO
TCPSocket* MultipleTCPSocketsListener::listenToSocket()
{
if (sockets.empty())
{
return NULL;
}
// create local set for the select function (fd_set)
fd_set readFd;
FD_ZERO(&readFd);
// fill the set with file descriptors from the socket list using (FD_SET macro)
int biggestSocket = 0;
for (unsigned int i = 0; i < this->sockets.size(); i++)
{
int currentFd = this->sockets[i]->getSocketFd();
FD_SET(currentFd, &readFd);
if (currentFd > biggestSocket)
{
biggestSocket = currentFd;
}
}
struct timeval timeout;
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
int numOfActiveFds = select(biggestSocket + 1, &readFd, NULL, NULL, &timeout);
TCPSocket* activeClient = NULL;
if (numOfActiveFds > 0)
{
for (unsigned int i = 0; i < this->sockets.size(); i++)
{
TCPSocket* currentSocket = this->sockets[i];
if (FD_ISSET(currentSocket->getSocketFd(), &readFd))
{
cout << "Incoming from : " << currentSocket->getClientAsString() << endl;
activeClient = currentSocket;
break;
}
}
}
// if select return a valid socket return the matching TCPSocket object otherwise return NULL
return activeClient;
}
示例11: CreateEvent
SocketPort::SocketPort(SocketService *svc, TCPSocket &tcp) :
Socket(accept(tcp.getSocket(), NULL, NULL))
{
detect_pending = true;
detect_output = false;
detect_disconnect = true;
#ifdef WIN32
// FIXME: error handling
event = CreateEvent(NULL,TRUE,FALSE,NULL);
#endif
next = prev = NULL;
service = NULL;
// FIXME: use macro here and in other files...
#ifndef WIN32
if(so > -1)
#else
if(so != INVALID_SOCKET)
#endif
{
setError(false);
if( svc )
svc->attach(this);
}
}
示例12: main
int main() {
TCPSocket socket;
socket.connect("169.254.0.10", 10001);
boost::thread requisicoes1(fazRequisicoes, &socket, "8", .2);
boost::thread requisicoes2(fazRequisicoes, &socket, "5", .2);
boost::thread requisicoes3(fazRequisicoes, &socket, "I", 1);
boost::thread leitura(recebeRequisicoes, &socket);
while(1);
printf("finalizou a thread principal\n");
return 0;
}
示例13: rcv_n_chk_against_rfc864_pattern_nonblock
void rcv_n_chk_against_rfc864_pattern_nonblock(TCPSocket &sock)
{
static const size_t total_size = 1024 * 100;
static const size_t buff_size = 1220;
uint8_t buff[buff_size];
size_t recvd_size = 0;
int time_allotted = split2half_rmng_tcp_test_time(); // [s]
Timer timer;
timer.start();
// Verify received data
while (recvd_size < total_size) {
int rd = sock.recv(buff, buff_size);
TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK);
if (rd > 0) {
check_RFC_864_pattern(buff, rd, recvd_size);
recvd_size += rd;
} else if (rd == NSAPI_ERROR_WOULD_BLOCK) {
if (timer.read() >= time_allotted) {
TEST_FAIL();
break;
}
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
} else {
TEST_FAIL();
break;
}
}
timer.stop();
printf("MBED: Time taken: %fs\n", timer.read());
}
示例14: RecvConnection
/*-----TCPSocket::ReceiveConnection--------------------------------------------
* Called to set up a given TCPSocket from an incoming connection on the
* current TCPSocket
*---------------------------------------------------------------------------*/
bool TCPSocket::RecvConnection(TCPSocket &newSocket)
{
socklen_t sizeofSockaddr = sizeof(myRemoteAddrStorage);
bool success = false;
// Make sure we stay under FD_SETSIZE
// See:
// * http://www.securityfocus.com/archive/1/490711
// * http://securityvulns.com/docs7669.html
// for more details
// This probably has no affect, since we are using multiple threads, but keep it here
// to be used as a sanity check.
int newDesc = accept(myDescriptor, (struct sockaddr*)&newSocket.myRemoteAddr, &sizeofSockaddr);
if (newDesc < 0)
{
// Something went wrong, probably indicates an error somewhere else
gLog.warning(tr("Cannot accept new connection:\n%s"), strerror(errno));
return false;
}
if (newDesc < static_cast<int>(FD_SETSIZE))
{
newSocket.myDescriptor = newDesc;
newSocket.SetLocalAddress();
success = true;
}
else
{
gLog.error(tr("Cannot accept new connection, too many descriptors in use."));
close(newDesc);
}
return success;
}
示例15: TCPSOCKET_RECV_100K_NONBLOCK
void TCPSOCKET_RECV_100K_NONBLOCK()
{
TCPSocket sock;
_tcpsocket_connect_to_chargen_srv(sock);
sock.set_blocking(false);
sock.sigio(callback(_sigio_handler, Thread::gettid()));
Timer timer;
timer.start();
rcv_n_chk_against_rfc864_pattern_nonblock(sock);
timer.stop();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
printf("MBED: Time taken: %fs\n", timer.read());
}