Layla_2401 0 Newbie Poster

Hello Everyone,

I'm trying to exchange messages between three different programs, I was able to establish a connection between the Server and Client A programs, but when I added code to include client B program I got an infinite loop. I've enclosed segments of my code. I would really appreciate any help I can get.

Thanks in advance.

Server(Passive connection):
=====================
             WSADATA WsaDat;
    SOCKET Socket;
    SOCKET TempSock = SOCKET_ERROR;
    int portnum = 50;
    char String[50];
    int retval = SOCKET_ERROR;
             SOCKADDR_IN SockAddr;
    HOSTENT *hp;
    char ServerName[255];





        if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    {
        cout <<"\n WSA Initialization Failed";
        cout.flush();
    }

       //  Clear the Address 
    memset(&SockAddr, 0, sizeof(SOCKADDR_IN));

    // Get Local Computer Name 
    gethostname(ServerName, sizeof(ServerName));

    // Get Local Computer Address Information 
    hp = gethostbyname(ServerName);
    if ( hp == NULL)
    {
        cout <<"\n Attempt to retreive computer infromation failed";
        cout.flush();
        return INVALID_SOCKET;
    }

    // This is the host address
    SockAddr.sin_family = hp->h_addrtype;

    // This is the port number
    SockAddr.sin_port = htons(portnum); 

    // Create the Socket
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == INVALID_SOCKET)
    {
        cout <<"\n Socket Creation Failed";
        cout.flush();
        return INVALID_SOCKET;
    }


    // Binding the Address
    if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) ==   SOCKET_ERROR)
    {
        cout <<"\n Attempt to bind failed";
        cout.flush();
        return INVALID_SOCKET;
    }

    // Listen to Incoming Requests and Return a Valid Socket

    listen(Socket, 40);

          // Accepting Incoming Requests 

    while (TempSock == SOCKET_ERROR)
    {
        TempSock =accept(Socket, NULL, NULL);
    }
    Socket = TempSock;

          // Message exchange with Client A comes here

      closesocket(TempSock);
      closesocket(Socket);
==========================================================
Client A (Active connection):
======================
// -------  Initializing WSA  --------
    InitializingWSA(WsaDat);


    // ------- Store Information About the Server -----
    hostEntry = gethostbyname("station-1");

    if(!hostEntry)
    {
        cout <<"\nError getting the host name";
        cout.flush();
        WSACleanup();
    }


    // -------  Create the Socket --------

    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == INVALID_SOCKET)
    {
        cout <<"\n Socket Creation Failed";
        cout.flush();
    }


    // ------- Binding the Socket --------
    // I want an internet type connection (TCP/IP)
    ServerInfo.sin_family = AF_INET;
    ServerInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);    
    // I want to use port 50
    ServerInfo.sin_port = htons(50); 


    // Connect to the server
if (connect(Socket, (SOCKADDR *)(&ServerInfo), sizeof(ServerInfo)) != 0)
    {
        cout <<"\n Failed to Establish Connection with the Server";
        cout.flush();
    }

// Message exchange with the SERVER takes place here

// Close the Connection on the Client side

    closesocket(Socket);

// Attempt to connect to client B
    // -------  Initializing WSA  ------
    InitializingWSA(WsaDat);


    // ------- Store Information About the Server -----
    hostEntry2 = gethostbyname("station-1");

    if(!hostEntry2)
    {
        cout <<"\nError getting the host name";
        cout.flush();
        WSACleanup();
    }


    // -------  Create the Socket --------

    Socket2 = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket2 == INVALID_SOCKET)
    {
        cout <<"\n Socket 2 Creation Failed";
        cout.flush();
    }


    // ------- Binding the Socket --------
    // I want an internet type connection (TCP/IP)
    ClientBInfo.sin_family = AF_INET;
    ClientBInfo.sin_addr = *((LPIN_ADDR)*hostEntry2->h_addr_list);  
    // I want to use port 60
    ClientBInfo.sin_port = htons(60); 
if (connect(Socket, (SOCKADDR *)(&ClientBInfo), sizeof(ClientBInfo)) != 0)
    {
        cout <<"\n Failed to Establish Connection with the Client B";
        cout.flush();
    }

retval= SOCKET_ERROR;
    char String22[50] = "Hello";
    cout <<"\n String is:"<< String22 << endl;
    cout.flush();

    while (retval == SOCKET_ERROR)
    {
        retval = send(Socket2, String22, strlen(String22) +1,0);
        if ( retval == 0)
        {
            cout <<"\n Connection closed at other end\n";
            cout.flush();
            break;
        }
    }


    closesocket(Socket2);
==========================================================
Client B(Passive Connection):
======================
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    {
        cout <<"\n WSA Initialization Failed";
        cout.flush();
    }
//  Clear the Address 
    memset(&SockAddr, 0, sizeof(SOCKADDR_IN));

    // Get Local Computer Name 
    gethostname(ClientBname, sizeof(ClientBname));

    // Get Local Computer Address Information 
    hp = gethostbyname(ClientBname);
    if ( hp == NULL)
    {
        cout <<"\n Attempt to retreive computer infromation failed";
        cout.flush();
        return INVALID_SOCKET;
    }

    // This is the host address
    SockAddr.sin_family = hp->h_addrtype;

    // This is the port number
    SockAddr.sin_port = htons(60);  

    // Create the Socket
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket== INVALID_SOCKET)
    {
        cout <<"\n Socket Creation Failed";
        cout.flush();
        return INVALID_SOCKET;
    }


    // Binding the Address
    if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
    {
        cout <<"\n Attempt to bind failed";
        cout.flush();
        return INVALID_SOCKET;
    }

    // Listen to Incoming Requests and Return a Valid Socket

    listen(Socket 40);

// Accepting Incoming Requests 

    while (TempSock == SOCKET_ERROR)
    {
        TempSock =accept(Socket, NULL, NULL);
    }
    Socket = TempSock;


    retval= SOCKET_ERROR;

    while (retval == SOCKET_ERROR)
        {
            retval = recv(Socket, String, 50,0);
            if (retval == 0) 
            {
                cout <<"\n Connection closed at other end";
                cout.flush();
                break;
            }

        }

    cout << "\n Contents of the message:" << String << endl;
    cout.flush();
// Close the Connection on the Server side
    closesocket(TempSock);
    closesocket(Socket);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.