CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    23

    Timestamp echo message

    Hey,

    I know this may not be enough info but i am trying to accept messages sent by the client, so i am working on the server code to accept requests.

    Now, i am trying to do a timestamp echo message as i am not really that good with C++. I am trying to work with somebody elses work :-(

    Here is my code:-

    if (command_str == "TIM")
    {
    string msg_sz = receive(connSocket, 4);
    int size = atoi(msg_sz.c_str());
    string msg = receive(connSocket, size);
    response = "TIMRP"+msg_sz+msg;
    }

    TIM and TIMRP are the protocols that i am following, it all works on the client side, but i need the server to accept. What is happening is that i am typing in "Test", i want the client to display the word "Test" and the time it has been received.

    Is there something wrong with the code i have?

    When i run the code i get an internet abort error?

    Apologies if i have been vague..

    Regards

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Timestamp echo message

    What do you mean by "typing "Test""? Looking at your snippit above, I would think that you should be typeing "0004Test".

  3. #3
    Join Date
    Oct 2009
    Posts
    23

    Re: Timestamp echo message

    Hey this is all the code on the page:-

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <time.h>
    #include <iomanip>
    #include <algorithm>

    using namespace std;

    bool DayTimeServer::run(TCPSocket *connSocket,
    sockaddr_in *client_addr)
    {
    time_t curTime;
    string client_ip, timeStr, command_str, msg_str;
    int client_port;
    int bytesSent, bytesRecv = SOCKET_ERROR;
    bool status = true;

    client_ip = inet_ntoa(client_addr->sin_addr);
    client_port = ntohs(client_addr->sin_port);
    cout << "Connection received from IP: "<<client_ip<<endl;

    //Read the command first.
    command_str = receive(connSocket, 3);

    string response; //declare the response

    if (command_str.size() > 0){
    cout<<"Client's message .. " << command_str <<" .. "; //Notofication
    if ( command_str == "ECH")
    {
    string msg_sz = receive(connSocket, 4);
    int size = atoi(msg_sz.c_str());
    string msg = receive(connSocket, size);
    response = "ECHRP"+msg_sz+msg;

    }
    else if (command_str == "REV")
    {
    string msg_sz = receive(connSocket, 4);
    int size = atoi(msg_sz.c_str());
    string msg = receive(connSocket, size);
    std::reverse(msg.begin(),msg.end());
    response = "REVRP"+msg_sz+msg;
    }
    else if (command_str == "TIM")
    {
    string msg_sz = receive(connSocket, 4);
    int size = atoi(msg_sz.c_str());
    string msg = receive(connSocket, size);
    response = "TIMRP"+msg_sz+msg;
    }

    else {
    timeStr = "UNKNOWN COMMAND";
    }
    send(connSocket,response);
    cout<<"Replied: -> "<<timeStr<<" --"<<endl;
    }
    return status;
    }

    string toString (unsigned int val, unsigned short len)
    {
    stringstream stream;
    stream <<setfill('0')<<setw(len)<< val ;

    return stream.str();
    }

    I want to type in "Test" or whatever and i want to be shown the TIMESTAMP of the message and the message itself.

    the code in bold is what i am working with...

    does this help make you understand what i am trying to do?

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Timestamp echo message

    Please use [CODE] [/CODE] tags around your code.

    The client code would be more helpful (assuming you are "typing" in the client and it is sending it to the server).

    It appears that whatever protocol you are using, the server expects to receive a stream composed of a three character command code (i.e. "TIM"), a 4 character "size" value, followed by "size" characters of text.

    Without seeing the client code, I assume that the client prompts the user for a string, then formats a "packet" to send to the server so that typing "Test" results in "TIM0004Test" being sent to the server.

    Now, what exactly are you trying to accomplish?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured