socket/port error

We have two processes on our HP Unix server – ONSB and IMSB.  
ONSB runs our overnight batch processes, posting, reports, etc.
IMSB run our posting, some of the reports and some batch processes.

Something has changed on our HP Unix server, but it wasn’t done by us.
When I try to run either process they are timing out when trying to create socket.
The socket/port to be used is determined by the dispatcher that it is trying to connect to.

The dispatcher using the same socket/port has no problem starting but the ONSB and IMSB that need to use the same socket/port as the dispatcher doesn’t work.

The way I was able to determine the error was by placing statements in the program that were written out to a file.

The things that have been tried up to this point are as follows:

Putting in displays to determine the error
Have tried recompiling the original version of the ONSB program (which is the same as the current version, there was no changes made to it)
Rebouncing the server
Tried changing the socket/port numbers
Tried comparing the values on  the Sun server and the HP server – when I changed the value it didn’t even get to the portion of the program where it creates the socket/port

I am at a loss for what to do or try next.  I have tried Google to see if there is any information on freeing up the port but not much info is to be found.
I am submitting this to Experts Exchange.  

Any thoughts or help would be greatly appreciated.  

Here is the code that is being executed:

// System Include Files:
#ifdef WIN32
#include "windows.h"
#include <winsock.h>
#include <fstream>
#include <iostream>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <fstream.h>
#include <iostream.h>

#ifdef KP_SOLARIS
#include <sysent.h>
#endif

#ifdef KP_AIXUX
#include <unistd.h>
#else
#include <sys/unistd.h>
#endif
#endif                     // WIN32

extern "C" {
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <memory.h>
}

//   KPMG Include Files:
#include "kptype20.hpp"
#include "kptrace.hpp"

//===========================================================

short g_nDispatchPort;

#ifdef WIN32
SOCKET     g_sSocket;
#else
int  g_sSocket;
extern     int errno;
#endif

struct sockaddr_in SockAddr;
struct hostent *g_pHostEnt;

extern KP_Trace g_TraceLog;

//===========================================================
KP_SINT2 RunBatch(void)
//===========================================================
{         
int   iRetCd, iRecvLen;
char  szHostName[30];
KP_SCHAR szAcknowledge[10];

       cerr << "Checkpoint 1" << endl;

     gethostname( szHostName, 30 );

       cerr << "Checkpoint 2" << endl;

     if((g_pHostEnt = gethostbyname(szHostName)) == NULL)
     {
           return 1;
     }
     
       cerr << "Checkpoint 3" << endl;

     memcpy((char *) &SockAddr.sin_addr.s_addr, g_pHostEnt->h_addr, g_pHostEnt->h_length);

       cerr << "Checkpoint 4" << endl;

   /* create socket */
#ifdef WIN32
     if((g_sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
#else
     if((g_sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
#endif
   {
              cerr << "Checkpoint 5a" << endl;
           return 1;
   }
   else
   {
           /*  Fill in address to which we will connect */
             cerr << "Checkpoint 5b" << endl;

           SockAddr.sin_family = PF_INET;
           SockAddr.sin_port = htons(g_nDispatchPort);

          cerr << "SockAddr.sin_family is: " << SockAddr.sin_family << endl;
          cerr << "SockAddr.sin_port is: " << SockAddr.sin_port << endl;
             cerr << "Checkpoint 6" << endl;
     
           /*  try to connect */
           if((iRetCd=connect(g_sSocket, (struct sockaddr *) &SockAddr, sizeof(struct sockaddr_in))) != 0)
           {
#ifdef WIN32
                closesocket(g_sSocket);
                g_sSocket = INVALID_SOCKET;
                    cerr << "Checkpoint 7a" << endl;
#else
                cerr << "g_sSocket is: " << g_sSocket << endl;
                close(g_sSocket);
                g_sSocket = -1;
                    cerr << "Checkpoint 7b" << endl;

#endif
                cerr << "iRetCd is: " << iRetCd << endl;
                cerr << "errno is: " << errno << endl;     
                return(1);
           }
     }
    
     // Send "Connect" message to dispatcher
     if( send(g_sSocket, "1#OvernightBatch", 16, 0) < 16 )
     {     
             cerr << "Checkpoint 8" << endl;
           return 1;
     }

     cerr << "================================================" << "\n"  
            << "ONSB:\t\tOvernight Batch Has Been Executed. " << "\n"  
            << "================================================" << endl;

     //begin added by YinC 8/20/98
     #ifdef WIN32
           Sleep(2000);
     #endif 
       cerr << "Checkpoint 9" << endl;

     // wait for ack from dispatcher
     if(( iRecvLen = recv( g_sSocket, szAcknowledge, 10, 0)) == -1)
     {

           cerr << "Checkpoint 10a" << endl;
           g_TraceLog.Trace( "recv() error 1" );
           return 1;
             
     }

       cerr << "Checkpoint 10b" << endl;

     if( send(g_sSocket, "103\r\n", 5, 0) < 5 )
     {
                cerr << "Checkpoint 11a" << endl;
           g_TraceLog.Trace( "send() error 1" );
           return 1;
     }

       cerr << "Checkpoint 11b" << endl;

     // wait for ack from dispatcher
     if(( iRecvLen = recv( g_sSocket, szAcknowledge, 10, 0)) == -1)
     {
             cerr << "Checkpoint 12a" << endl;
           g_TraceLog.Trace( "recv() error 2" );
           return 1;
     }

       cerr << "Checkpoint 12b" << endl;

     // Send logoff message
     // 980925 JRK: Corrected lenght from 2 to 5.
     if(send(g_sSocket, "2#0\r\n", 5, 0) < 5)
     {
             cerr << "Checkpoint 13a" << endl;
          g_TraceLog.Trace( "send() error 2" );
           return 1;
     }

       cerr << "Checkpoint 13b" << endl;

     cerr << "================================================" << "\n"
           << "ONSB:\t\tOvernight Batch Complete " << "\n"
           << "================================================" << endl; 

     // wait for ack from dispatcher
     if(( iRecvLen = recv( g_sSocket, szAcknowledge, 10, 0)) == -1)
     {
           g_TraceLog.Trace( "recv() error 3" );
           return 1;
     }

#ifdef WIN32
     closesocket(g_sSocket);
     g_sSocket = INVALID_SOCKET;
     WSACleanup();
#else
     close( g_sSocket );
#endif

     return 0;
}


//===========================================================
// Connect to the dispatcher on this machine, send a message to
// process overnight batch, and exit
//===========================================================

//===========================================================
int main(int argc, char **argv)
//===========================================================
{
     g_TraceLog.SetFile( "onsb.log" );
        

     if( argc != 2 )
     {
           cerr << "ONSB:\t\tUsage:  overnight <dispatch port#>";
           return -1;
     }



#ifdef WIN32
WSADATA               wskData;
     if(WSAStartup(0x0101, &wskData))
     {
           // Socket Library Initialization Failed
           cerr << "ONSB:\t\tError initializing socket library";
           return 1;
     }
#endif
        cerr << "Checkpoint A" << endl;
        
     g_nDispatchPort = atoi( argv[1] );
        cerr << "Dispatcher Port is: " << g_nDispatchPort << endl;

     RunBatch();

     return 0;
}

Open in new window

ams42870Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carlmdCommented:
Only one process at a time may use a port. If this used to work and now does not, then I suspect the first program does not release the port when it is done. When it fails type:

netstat -Aan | grep LISTEN | grep port#          (where port# is the port in question)

then take the entry in the first column of the result (typically a hex result starting in f) and type

 rmsock ffirstcolumnnumber tcpcb (this won't actually remove anything)

this will return the pid of the process using this port. You can then

ps -ef | grep thatpid

to get the program

This will tell you what has the port, then look at that program to determine why it is not being released. Probably an error now being generated as a result of something that has changed.

When a socket program listens for something in particular to be sent, and the sender is not under your control, it is not unusual for a problem to occur when the sender changes something.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.