GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2007, 14:26
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Need Help using socket


Im trying to use socket to do a telnet program, the socket works fine the first run of the program, but if I kill the program without doing a close on the socket. I can't get the program to work again untill whenever the socket finally goes away by itself.

It would create the socket but will not connect to it, and im sure its because the socket is already held up, I try doing a setsockopt with SO_REUSEADDR, but still don't work.

So guess the question is how can I get the socket to work again if the program exited and I need to start it up again.

Thanks.
  #2  
Old 21-Nov-2007, 16:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Help using socket


Quote:
Originally Posted by Spidy08
...I guess the question is...

Show some code. I can't see much point in our trying to guess what you actually tried.

You might also mention what operating system and what compiler you are using.

Although most of the sockets functions are supposedly specified (more-or-less) by the POSIX standard there are many variations that exist in the wild, and none of them is a part of the C standard library.

Details vary with different implementations (different compilers; different operating systems, different etc.).

Regards,

Dave
  #3  
Old 26-Nov-2007, 07:48
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Re: Need Help using socket


This is for c++ Greenhill INTEGRITY environment.
Just your basic create and connect command... This program is downloaded from somewhere else.

CPP / C++ / C Code:
CSocketDx::CSocketDx(char *strIP,int nPort)
{
  unsigned long ip;

  if((*strIP <= '9') && (*strIP >= '0'))
  {
     if((ip = inet_addr(strIP)) == INADDR_NONE)
       printf("invalid host ip given");
  }
  else
  {
    hostent* ent = gethostbyname(strIP);
    if(!ent) printf("invalid host name\n");
    ip = *(unsigned long*)(ent->h_addr);
  }

  m_sockaddr_in.sin_family = AF_INET;
  m_sockaddr_in.sin_port = htons(nPort);
  m_sockaddr_in.sin_addr = *(in_addr*)&ip;
}

int CSocketDx::Create()
{
  m_hSocket = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  if ( m_hSocket == -1/*== INVALID_SOCKET*/) return -1;

  /// Added this but didn't help.
  int yes=1;
  if (setsockopt(m_hSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    perror("setsockopt");
    exit(1);
  } 
  return 0;
}

int CSocketDx::Connect()
{
  int nRet;

  nRet = connect(m_hSocket,(sockaddr*)&m_sockaddr_in,sizeof(sockaddr));
  if ( nRet == -1 ) return -1;
  return 0;
}

SOCKET CSocketDx::TelnetConnect()
{
  int nRet;
	
  nRet = Create();
  if ( nRet < 0 ) return NULL;

  nRet = Connect();
  if ( nRet < 0 )
  {
    close(m_hSocket);
    return NULL;
  }
  return m_hSocket;
}

The problem is when I get the telnet session running and if I decided to just close the gui without going through the proper log off, the next time I try to run the program again it just hang in the connect command, im guessing its because the socket is not close. I guess I just need a exception handling to that it close the socket before it exit, but how would you handle that if the user just close the gui application?
  #4  
Old 26-Nov-2007, 09:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Help using socket


Quote:
Originally Posted by Spidy08
This is for c++ Greenhill INTEGRITY environment
I have no knowledge or access to this. Is there a tech support group or forum for this?

Quote:
Originally Posted by Spidy08
Just your basic create and connect command...
That's how I would use setsockopt in a telnet (or other) server before trying to bind a socket to a particular IP address and port. (Except for some of my compilers, the variable "yes" must be a char.) On my systems (Linux, Windows XP) it is not necessary for a client.
Quote:
Originally Posted by Spidy08
The problem is...

I assume you are implementing a telnet client with the class you showed.

Have you tried a very simple main() (a console application)?

CPP / C++ / C Code:
// minimal set of #include files for your class

int main()
{
    int port = 9034; // whatever port your server is listening to
    int i;
    SOCKET s;

    // If you are using Windows, then put the WSA stuff here

    CSocketDx mySocket("127.0.0.1", port); // Assuming the server is on the same machine
    s = mySocket.TelnetConnect();
    cout << "TelnetConnect returns s = " << s  << endl;

    cout << "Press 'Enter' to proceed . . .";
    cin.get();

    // If you are using Windows, put WSACleanup() here

    return 0;
}


Start the server.

Start the client. Hit 'Enter' and start the client again. Repeat.

Start the client. Hit ctrl-C instead of Enter. Repeat.


Do various sequences of ctrl-C, Enter from the client.

You might also tell us what operating system and what telnet server you are using.

If any of my assumptions are incorrect, then maybe you can give us a few more clues.

Regards,

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Socket send() maximum size Mofix C Programming Language 4 26-Mar-2007 12:41
[PROGRAM] Winsock Programming Max Payne MS Visual C++ / MFC Forum 1 08-Mar-2007 23:38
Reading socket input streams. AJSHOPE C++ Forum 2 21-May-2006 16:42
windows sockets confusion ubergeek C++ Forum 1 18-Jul-2005 20:44
Child Thread fails to update Shared Memory Correctly j_kenpo C++ Forum 1 12-Jul-2005 17:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.