bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

TCPSession.isPending() behavior.


From: Cui Qiang
Subject: TCPSession.isPending() behavior.
Date: Thu, 27 Dec 2001 13:40:09 +0900

Dear all,
 
I founded the serverside TCPSession.isPending(SOCKET_PENDING_INPUT) is goes on when the clientside of socket is closed abnormally.
How can I close the serverside TCPSession after the client-socket is closed abnormally?
 
Windows 2000, CommonC++1.93.
 
Any suggestions?
 
regards.
 
Server.cpp
 
// Copyright (C) 1999-2001 Open Source Telecom Corporation.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception to the GNU General Public License, permission is
// granted for additional uses of the text contained in its release
// of APE.
//
// The exception is that, if you link the APE library with other files
// to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the APE library code into it.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the
// name APE.  If you copy code from other releases into a copy of
// APE, as the General Public License permits, the exception does
// not apply to the code that you add in this way.  To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for APE, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice. 
 

#include <config.h>
#include <strchar.h>
#include <exception.h>
#include <thread.h>
#include <socket.h>
#include <cstdlib>
 
#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif
 
class myTCPSocket : public TCPSocket
{
protected:
 bool OnAccept(const InetHostAddress &ia, tpport_t port);
 
public:
 myTCPSocket(InetAddress &ia);
};
 
class myTCPSession : public TCPSession
{
private:
 static Mutex mutex;
 void Run(void);
 file://void Final(void);
 
public:
 myTCPSession(TCPSocket &server);
 static volatile int count;
};
 
myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {};
 
bool myTCPSocket::OnAccept(const InetHostAddress &ia, tpport_t port)
{
 cout << "accepting from: " << ia.getHostname() << ":" << port << endl;;
 return true;
}
 
volatile int myTCPSession::count = 0;
 
Mutex myTCPSession::mutex;
 
myTCPSession::myTCPSession(TCPSocket &server) :
TCPSession(server)
{
 cout << "creating session client object" << endl;
 mutex.EnterMutex();
 ++count;
 mutex.LeaveMutex();
 unsetf(ios::binary);
}
 
void myTCPSession::Run(void)
{
 try{
  char buf[1000];
  InetAddress addr = getLocal();
  while(isPending(SOCKET_PENDING_INPUT) )
  {
   *tcp() >> buf;
   *tcp() << buf << endl;
   flush();
  }
 
 }
 catch(Socket *socket)
 {
  tpport_t port;
  int err = socket->getErrorNumber();
  InetAddress saddr = (InetAddress)socket->getPeer(&port);
  cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl;
  if(err == SOCKET_BINDING_FAILED)
  {
   cerr << "bind failed; port busy" << endl;
   ::exit(-1);
  }
  else
   cerr << "client socket failed" << endl;
  Final();
 }
}
 
 
int main()
{
 myTCPSession *tcp;
 InetAddress addr;
 addr = "255.255.255.255";
 cout << "testing addr: " << addr << ":" << 4096 << endl;
 addr = "192.168.1.1";
 cout << "binding for: " << addr << ":" << 4096 << endl;
 
 try
 {
  myTCPSocket server(addr);
 
  while(true)
  {
   cout << "before create" << endl;
   tcp = new myTCPSession(server);
   cout << "after create" << endl;
   tcp->Start();
  }
 }
 catch(Socket *socket)
 {
  tpport_t port;
  int err = socket->getErrorNumber();
  InetAddress saddr = (InetAddress)socket->getPeer(&port);
  cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl;
  if(err == SOCKET_BINDING_FAILED)
  {
   cerr << "bind failed; port busy" << endl;
   ::exit(-1);
  }
  else
   cerr << "client socket failed" << endl;
 }
 cout << "timeout after 30 seconds inactivity, exiting" << endl;
}
 
 
 
 
Client.cpp
 
#include <config.h>
#include <strchar.h>
#include <exception.h>
#include <thread.h>
#include <socket.h>
#include <iostream>
#include <stdio.h>
 
#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif
 
int main(int argc, char *argv[])
{
  char buf[1024];
 
  InetHostAddress addr = "192.168.1.1";
  cout << "binding for: " << addr << ":" << 4096 << endl;
  TCPStream client(addr,4096,2048,false);
 
  try
    {
 client << "abc" << endl;
      // the last parameter indicates not to throw exceptions
  client >> buf;
  cout << buf << endl;
 
    }
 
  catch (Socket *s)
    {
      cout << "exception thrown: " << s->getErrorString() << endl;
    }
 
  return 0;
}
 
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]