Link to home
Start Free TrialLog in
Avatar of ams42870
ams42870

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Carl Dula
Carl Dula
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial