January 30th, 2003, 05:24 PM
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.
#define RCVBUFSIZE 32
void DieWithError(char *errorMessage);
int main(int argc, char *argv)
struct sockaddr_in echoServAddr;
unsigned short echoServPort;
int bytesRcvd, totalBytesRcvd;
if ((argc<3) || (argc>4))
fprintf(stdout,"Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",argv);
echoServPort = atoi(argv);
echoServPort = 7;
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
fprintf(stderr,"WSAStartup () failed");
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)
echoStringLen = strlen(echoString);
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
totalBytesRcvd = 0;
while (totalBytesRcvd < echoStringLen)
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE -1, 0)) <=0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd;
echoBuffer[bytesRcvd] = '\0';
void DieWithError(char *errorMessage)
fprintf(stderr,"%s: %d\n",errorMessage, WSAGetLastError());
January 30th, 2003, 05:34 PM
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?!
January 30th, 2003, 08:23 PM
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.
February 1st, 2003, 12:39 PM
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.