need sockets help, stuck on problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manontheedge
    New Member
    • Oct 2006
    • 175

    need sockets help, stuck on problem

    I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever was typed it. My problem is that this "client" code receives ONE message for the "server" and won't receive any more than that. I'm lost at this point. I've overcommented the code intentionally and took out all the error codes, because I wasn't getting any...there's no error, it's just not receiving anything more. If anyone can help me out, or tell me where I've gone wrong I would really appreciate it.

    [CODE=cpp]

    #include<stdio. h>
    #include<winsoc k2.h>
    #include<iostre am>
    using namespace std;

    #pragma comment(lib, "ws2_32.lib ")
    #define STRING_MAX 2048
    #define MAX 640000
    #define MAX_CON 16

    int nret;
    SOCKET MySock;

    char *client_send(ch ar *targetip, int port)
    {

    WSADATA wsaData;
    WORD wVersionRequest ed;
    wVersionRequest ed = MAKEWORD(2, 2);
    if(WSAStartup (wVersionReques ted, &wsaData) != 0){ }

    MySock = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in sock;

    ////////////////////////////////////////////////////////////////////////
    //hostent --> used by functions to store information about a given host

    struct hostent* target_ptr;
    if((target_ptr= gethostbyname(t argetip)) == NULL){ }

    memcpy(&sock.si n_addr, target_ptr->h_addr, target_ptr->h_length);
    sock.sin_family = AF_INET;
    sock.sin_port=h tons(port);
    ////////////////////////////////////////////////////////////////////////

    if((connect(MyS ock, (struct sockaddr *)&sock, sizeof(sock) ))){ }

    //connect --> establishes a connection to a specified socket
    //MySock --> an unconnected socket
    //(struct sockaddr *)&sock -- Name of the socket in the sockaddr structure to which
    // the connection should be established
    //sizeof(sock) -- Length of name, in bytes

    char *recvString= new char[MAX];
    nret = recv(MySock, recvString, MAX + 1, 0);


    while(nret < 1)
    {
    nret = recv(MySock, recvString, MAX + 1, 0);
    // printf("\n %d bytes received", nret);
    }

    //recv --> receives data from a connected or bound socket
    //MySock -- identifies a connected socket
    //recvString -- buffer for incoming data
    //MAX + 1 -- The length, in bytes, of the buf parameter
    //0 -- pointer flag

    char *output= new char[nret];
    strcpy(output, " ");
    strncat(output, recvString, nret);
    delete [ ] recvString;

    closesocket(MyS ock);

    WSACleanup();

    return (output);

    delete[ ] output;
    }


    int main(int argc, char *argv[])
    {
    int port = 888;
    char* targetip;
    char* output = NULL;

    targetip = argv[1];

    while(1)
    {
    printf("--> ");
    printf("%s \n", client_send(tar getip, port));
    }
    }


    [/CODE]
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    Before doing a recv call do a select and check whether there is any data available on the port for reading.
    Also check whether the server is not closing the connection after sending a message to the client

    Raghuram
    Last edited by gpraghuram; Sep 14 '07, 05:00 AM. Reason: Removing unwanted portion

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      thank you for the reply. I actually found the problem, I have some "server" and this "client" code...

      I was just plane doing some things wrong. I had the server listening for connection attempts and then sending out THE message to the client. I realised my mistake, and that the client is the one that I wanted to send the message to the server. Basically, I had enough stuff backwords to make it not work right. I thought I had pinpointed the problem to the "client" code when I posted. When I realised my mistake, it only took about 30 minutes to get it running correctly.

      anyway, not sure if that's gonna be clear, but thank you for the help, and, it's working fine now.

      Comment

      Working...