#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    2
    Rep Power
    0

    sockets, echoclient, error 10061


    hi, I am trying to write a simple echoclient program in C (windows) yet when I try to run it with my own computer's IP address as the server, I get error 10061, or, connection has been denied. Would there be any way to fix this? Thanks. And, the code is below.
    Jen :confused:

    #include <stdio.h>
    #include <winsock.h>
    #include <stdlib.h>


    #define RCVBUFSIZE 32
    void DieWithError(char *errorMessage);

    int main(int argc, char *argv[])
    {
    int sock;
    struct sockaddr_in echoServAddr;
    unsigned short echoServPort;
    char *servIP;
    char *echoString;
    char echoBuffer[RCVBUFSIZE];
    int echoStringLen;
    int bytesRcvd, totalBytesRcvd;
    WSADATA wsaData;



    if ((argc<3) || (argc>4))
    {
    fprintf(stdout,"Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",argv[0]);
    exit(1);
    }

    servIP=argv[1];
    echoString=argv[2];

    if (argc==4)
    echoServPort = atoi(argv[3]);
    else
    echoServPort = 7;

    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
    {
    fprintf(stderr,"WSAStartup () failed");
    exit(1);
    }

    if ((sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
    DieWithError("socket() failed");

    memset(&echoServAddr,0,sizeof(echoServAddr));
    echoServAddr.sin_family = AF_INET;
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);
    echoServAddr.sin_port = htons(echoServPort);


    if (connect(sock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr))<0)
    DieWithError("connect() failed");

    echoStringLen = strlen(echoString);

    if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
    DieWithError("send() sent a different number of bytes than expected");

    totalBytesRcvd = 0;
    printf("Received: ");
    while (totalBytesRcvd < echoStringLen)
    {
    if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE -1, 0)) <=0)
    DieWithError("recv() failed or connection closed prematurely");
    totalBytesRcvd += bytesRcvd;
    echoBuffer[bytesRcvd] = '\0';
    printf(echoBuffer);
    }

    printf("\n");
    closesocket(sock);
    WSACleanup();
    exit(0);

    }

    void DieWithError(char *errorMessage)
    {
    fprintf(stderr,"%s: %d\n",errorMessage, WSAGetLastError());
    exit(1);

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    do you have an "echo" service running?
    where do you connect to? 127.0.0.1 (aka localhost) or your real IP?
    for the latter, XP´s built-in firewall is blocking the access?!
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    2
    Rep Power
    0
    sorry, i was kind of confused.
    yes, i do have a server running. and i'm using port 1087, but i still get error 10061. the server is running on windows ME and the client is running on windows 2000. I connect to the server's IP.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Jen:

    Your program works on my local network. Like your test case, I ran the echo server on Windows ME and the client on Windows 2000. I even copied your source code directly from your post and compiled it.

    Now the stupid question -- well, not so stupid, since it bit me too. Your client uses TCP. Is the server a TCP server or a UDP server? When I first ran the test, I also got a 10061 error, but that was because I was running a UDP echo server. Then when I ran a TCP echo server, it worked fine.


    BTW, I recognize the code as coming from Donahoo and Calvert's "Pocket Guide to TCP/IP Sockets: C Version" (second edition now titled "TCP/IP Sockets in C"). Good introductory book. It got me out of my "analysis paralysis" from researching the theory and got me coding and experimenting right away with their complete examples.

IMN logo majestic logo threadwatch logo seochat tools logo