IP Sockets Program in c++ for University Project

This is a discussion on IP Sockets Program in c++ for University Project within the C++ Programming forums, part of the General Programming Boards category; Hi There all I am developing an IP sockets program for my final year Bsc (Hons) in Computing & Networks ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    IP Sockets Program in c++ for University Project

    Hi There all

    I am developing an IP sockets program for my final year Bsc (Hons) in Computing & Networks which is going to be used for the university robot project. I have the IP sockets program working and now I am trying to add some additions functions to the program which allows me to measure network performance related tests over a (LAN).

    The tests will include sending/echoing bytes a number of bytes to the server and back. The tests will also need to show the time of how long it took to send & receive the message from the client & server respectively, the timing has to be measure milliseconds and to be able to calculate the the elasped time. The project needs to be in for the 28th August and I am not having much luck with it.

    I have enclosed some coding below to show what the program currently looks like at the moment, This also includes my attempts of measuring the time when a message is received by the server, and my attempts to show the number of bytes sent. I desperately need help, any small piece of info you may have just might make the difference or to just point out what other functions I could use. The coding is shown below for the client & server, this might take a little while to read.


    Client Coding:

    Code:
    #include "mySocket.h" 
    #include "myLog.h" 
    #include "myException.h" 
    #include "myHostInfo.h" 
    #include <string.h> 
    //#include "PerfTimer.h" 
    #include <Windows.h> 
    #include <Time.h> 
    //#include <afxwin.h> 
    
    
    myLog winLog; 
    void readServerConfig(string&); 
    void checkFileExistence(const string&); 
    
    int main() 
    { 
    // Initialize the winsock library 
    myTcpSocket::initialize(); 
    // LARGE_INTEGER *lpPerformanceCount = (LARGE_INTEGER) new (sizeof 
    (LARGE_INTEGER)); 
    LARGE_INTEGER lpPerformanceCount ; 
    // u_char cTTL 
    // CString str; 
    
    
    
    
    // get client's information (assume neither the name nor the address is given) 
    winLog << endl; 
    winLog << "Retrieve the localHost [CLIENT] name and address:" << endl; 
    myHostInfo clientInfo; 
    string clientName = clientInfo.getHostName(); 
    string clientIPAddress = clientInfo.getHostIPAddress(); 
    std::cout << "Name: " << clientName << endl; 
    std::cout << "Address: " << clientIPAddress << endl; 
    winLog << " ==> Name: " << clientName << endl; 
    winLog << " ==> Address: " << clientIPAddress << endl; 
    
    // get server's IP address and name 
    string serverIPAddress = ""; 
    readServerConfig(serverIPAddress); 
    winLog << endl; 
    winLog << "Retrieve the remoteHost [SERVER] name and address:" << endl; 
    winLog << " ==> the given address is " << serverIPAddress << endl; 
    
    myHostInfo serverInfo(serverIPAddress,ADDRESS); 
    string serverName = serverInfo.getHostName(); 
    std::cout << "Name: " << serverName << endl; 
    std::cout << "Address: " << serverIPAddress << endl; 
    winLog << " ==> Name: " << serverName << endl; 
    winLog << " ==> Address: " << serverIPAddress << endl; 
    
    // create the socket for client 
    myTcpSocket myClient(PORTNUM); 
    std::cout << myClient; 
    winLog << "client configuation: " << endl; 
    winLog << myClient; 
    
    // connect to the server. 
    std::cout << "connecting to the server [" << serverName << "] ... " << endl; 
    winLog << "connecting to the server [" << serverName << "] ... " << endl; 
    myClient.connectToServer(serverIPAddress,ADDRESS); 
    
    int recvBytes = 0; 
    
    while (1) 
    { 
    // send message to server 
    char messageToServer[MAX_MSG_LEN+1]= "hello world2"; 
    //char senddata(socketId,buf,MAX_MSG_LENGTH,0); 
    //memset(messageToServer,0,sizeof(messageToServer)); 
    std::cout << "[SEND] "; 
    std::cin.getline(messageToServer,MAX_MSG_LEN); 
    //memset(messageToServer,0,sizeof(senddata)); 
    std::cout << " &#37;s [%s]with %d bytes of data:"; 
    std::cout << strlen(messageToServer); 
    
    
    //########################################### 
    
    char tmpbuf[128], timebuf[26]; 
    time_t ltime; 
    string mystr; 
    mystr="ABC"; 
    // time( &ltime ); 
    QueryPerformanceCounter( &lpPerformanceCount); 
    std::cout<<( "Time in something:=%ldms,TTL=%d", &lpPerformanceCount); 
    //Removing getline so we that we don't need to wait for input //std::cin.getline 
    // (messageToServer,MAX_MSG_LEN); 
    
    
    
    
    //########################################### 
    
    
    winLog << "[SEND] " << mystr << endl; 
    myClient.sendMessage(mystr); 
    if ( !string(messageToServer).compare("Quit") || !string 
    (messageToServer).compare("quit") ) 
    break; 
    
    // receive message from server 
    string messageFromServer = ""; 
    recvBytes = myClient.recieveMessage(messageFromServer); 
    if ( recvBytes == -99 ) 
    break; 
    
    std::cout << "[RECV:" << serverName << "]: " << messageFromServer << endl; 
    winLog << "[RECV:" << serverName << "]: " << messageFromServer << endl; 
    
    } 
    
    return 1; 
    } 
    
    void readServerConfig(string& serverIPAddr) 
    { 
    serverIPAddr = "127.0.0.1"; //DEBUG 15.02.08 
    string serverConfigFile = ".\\serverConfig.txt"; 
    //checkFileExistence(serverConfigFile); 
    //ifstream serverConfig(serverConfigFile.c_str()); 
    
    // read server's IP address 
    //getline(serverConfig,serverIPAddr); 
    
    //serverConfig.close(); 
    } 
    
    void checkFileExistence(const string& fileName) 
    { 
    ifstream file(fileName.c_str()); 
    if (!file) 
    { 
    std::cout << "Cannot continue:" << fileName << " does NOT exist!" << endl; 
    exit(1); 
    } 
    file.close(); 
    } 
    
    
    
    Server coding: 
    
    #include "myEvent.h" 
    #include "mySocket.h" 
    #include "myLog.h" 
    #include "myException.h" 
    #include "myHostInfo.h" 
    #include "mySemaphore.h" 
    #include "myThread.h" 
    #include "myThreadArgument.h" 
    #include <iostream> 
    #include <stdio.h> 
    #include <sys/types.h> 
    #include <sys/timeb.h> 
    #include <time.h> 
    
    
    
    myLog winLog; 
    
    
    
    DWORD WINAPI clientHandleThread(LPVOID threadInfo) 
    { 
    // this structure will contain all the data this callback will work on 
    myThreadArgument* clientArgument = (myThreadArgument*)threadInfo; 
    
    // the semamphore to protect the access to the std output 
    mySemaphore* coutSemaphore = clientArgument->getCoutSemaphore(); 
    
    // get the client connection: receiving messages from client and 
    // sending messages to the client will all be done by using 
    // this client connection 
    myTcpSocket* clientConnection = clientArgument->getClientConnect(); 
    string clientName = clientArgument->getHostName(); 
    
    // the server is communicating with this client here 
    while(1) 
    { 
    string messageFromClient = ""; 
    
    // receive from the client 
    
    int numBytes = clientConnection->recieveMessage(messageFromClient); 
    if ( numBytes == -99 ) 
    break; 
    
    // write to the console and the log file, so lock the semaphore 
    coutSemaphore->lock(); 
    
    cout << "[RECV fr " << clientName << "]: " << messageFromClient << endl; 
    winLog << "[RECV fr " << clientName << "]: " << messageFromClient << endl; 
    
    // if the client wants to discount 
    if ( messageFromClient.compare("quit") == 0 || messageFromClient.compare("Quit") == 0 ) 
    { 
    coutSemaphore->unlock(); 
    break; 
    } 
    else // send to the client 
    { 
    char messageToClient[MAX_MSG_LEN+1]; 
    memset(messageToClient,0,sizeof(messageToClient)); 
    cout << "[SEND to " << clientName << "]: "; 
    //PC cin.getline(messageToClient,MAX_MSG_LEN); 
    winLog << "[SEND to " << clientName << "]: " << messageToClient << endl; 
    clientConnection->sendMessage(string(messageToClient)); 
    coutSemaphore->unlock(); 
    } 
    } 
    
    // if we reach here, this session with the client is done, 
    // so we set the event on this thread to inform the main 
    // control that this session is finished 
    clientArgument->getExitEvent()->setEvent(); 
    return 1; 
    } 
    
    DWORD WINAPI serverHandleThread(LPVOID threadInfo) 
    { 
    // this structure will contain all the data this callback will work on 
    myThreadArgument* serverArgument = (myThreadArgument*)threadInfo; 
    
    // the semamphore to protect the access to the std output 
    mySemaphore* coutSemaphore = serverArgument->getCoutSemaphore(); 
    
    // get the server 
    myTcpSocket* myServer = serverArgument->getClientConnect(); 
    string serverName = serverArgument->getHostName(); 
    
    // bind the server to the socket 
    myServer->bindSocket(); 
    cout << endl << "server finishes binding process... " << endl; 
    winLog << endl << "server finishes binding process... " << endl; 
    
    // server starts to wait for client calls 
    myServer->listenToClient(); 
    cout << "server is waiting for client calls ... " << endl; 
    winLog << "server is waiting for client calls ... " << endl; 
    
    // server starts to listen, and generates a thread to 
    // handle each client 
    
    myThreadArgument* clientArgument[MAX_NUM_CLIENTS]; 
    myThread* clientHandle[MAX_NUM_CLIENTS]; 
    for ( int i = 0; i < MAX_NUM_CLIENTS; i++ ) 
    { 
    clientArgument[i] = NULL; 
    clientHandle[i] = NULL; 
    } 
    int currNumOfClients = 0; 
    
    while ( 1 ) 
    { 
    // wait to accept a client connection. 
    // processing is suspended until the client connects 
    myTcpSocket* client; // connection dedicated for client communication 
    string clientName; // client name 
    client = myServer->acceptClient(clientName); 
    clientName = clientName + "-" + char(65+currNumOfClients); 
    
    // lock the std out so we can write to the console 
    coutSemaphore->lock(); 
    cout << endl << "==> A client from [" << clientName << "] is connected!" << endl << endl; 
    winLog << endl << "==> A client from [" << clientName << "] is connected!" << endl << endl; 
    coutSemaphore->unlock(); 
    
    // for this client, generate a thread to handle it 
    if ( currNumOfClients < MAX_NUM_CLIENTS-1 ) 
    { 
    clientArgument[currNumOfClients] = new myThreadArgument 
    (client,coutSemaphore,clientName); 
    clientHandle[currNumOfClients] = new myThread(clientHandleThread, 
    (void*)clientArgument[currNumOfClients]); 
    serverArgument->addClientArgument(clientArgument[currNumOfClients]); 
    clientHandle[currNumOfClients]->execute(); 
    currNumOfClients++; 
    } 
    } 
    
    return 1; 
    } 
    
    int main(void) 
    
    { 
    
    // build a semaphore so we can synchronize the access to std cout 
    // also includes the log file 
    mySemaphore coutSemaphore(string(""),1); 
    
    // Initialize the winsock library 
    myTcpSocket::initialize(); 
    
    // create the server: local host will be used as the server, let us 
    // first use myHostInfo class to show the name and IP address 
    // of the local host 
    winLog << endl; 
    winLog << "Retrieve the local host name and address:" << endl; 
    
    myHostInfo serverInfo; 
    string serverName = serverInfo.getHostName(); 
    string serverIPAddress = serverInfo.getHostIPAddress(); 
    std::cout << "my localhost (server) information:" << endl; 
    std::cout << " Name: " << serverName << endl; 
    std::cout << " Address: " << serverIPAddress << endl; 
    winLog << " ==> Name: " << serverName << endl; 
    winLog << " ==> Address: " << serverIPAddress << endl; 
    
    // open socket on the local host(server) and show its configuration 
    myTcpSocket myServer(PORTNUM); 
    std::cout << myServer; 
    winLog << myServer; 
    
    // create a thread to implement server process: listening to the socket, 
    // accepting client calls and communicating with clients. This will free the 
    // main control (see below) to do other process. 
    myThreadArgument* serverArgument = new myThreadArgument 
    (&myServer,&coutSemaphore,serverName); 
    myThread* serverThread = new myThread(serverHandleThread,(void*) 
    serverArgument); 
    serverThread->execute(); 
    
    // main control: since the serverThread is handling the server functions, 
    // this main control is free to do other things. 
    while ( 1 ) //MAIN CONTROL LOOP 
    { 
    // do whatever you need to do here, I am using Sleep(x) 
    // to make a little delay, pretending to be the other 
    // possible processings. 
    Sleep(1000); 
    
    } 
    return 1; 
    }

    Thank you

    Shahid

    *mod edit*
    Code tags don't do crap unless the code is indented to begin with.
    Perhaps work on indenting the code before trying to fix anything.
    Last edited by Salem; 08-05-2008 at 02:39 PM. Reason: [CODE][/CODE] go AROUND YOUR CODE, don't just add them to shut the error message up!!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Please use code tags.

    [code] code goes here [/code]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The tests will include sending/echoing bytes a number of bytes to the server and back. The tests will also need to show the time of how long it took to send & receive the message from the client & server respectively, the timing has to be measure milliseconds and to be able to calculate the the elasped time. The project needs to be in for the 28th August and I am not having much luck with it.
    Possibly use the ICMP helper lib. Specifically ping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with this last project! Credit Card Program!
    By ClearSights in forum C++ Programming
    Replies: 18
    Last Post: 10-27-2008, 10:08 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Sockets program
    By athos in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-14-2004, 02:46 PM
  5. Socket Project (getting one of multiple clients IP)
    By Cl0wn in forum C Programming
    Replies: 2
    Last Post: 04-05-2003, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21