[1/2OT] a buggy TCP client program

R

RoS

in windows xp
the problem is that if i have a tcp sever istalled in the port 1000 in
my computer, and "cli.exe" is the C program "client program" below and
type
cli 127.1.1.1 1000

connect of client fail with "WSAECONNREFUSED 10061"
don't understand why

there is someone expert in this there?
what is the answer?

then there is someone can point out some C program for the use api
"ReadFile" with an handle return by api "socket"?

thank you
----------------------------------------------------

#include <WINSOCK2.H>
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <wincon.h>

#define P printf
#define R return
#define F for
#define G goto
#define uns unsigned

int sockSta=0;

int openSock_m(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

if(sockSta==1) R 1;
wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{P("Tell the user that we couldn't find a usable"
"WinSock DLL.");
return 0;
}

/* Confirm that the WinSock DLL supports 1.1 */
/* Note that if the DLL supports versions greater */
/* than 1.1 in addition to 1.1, it will still return */
/* 1.1 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
P( "Tell the user that we couldn't find a usable"
"WinSock DLL., LB=%u HB=%u\n", (unsigned)LOBYTE(
wsaData.wVersion ) ,
(unsigned) HIBYTE( wsaData.wVersion ) );
WSACleanup( );
return 0;
}
sockSta=1;
return 1;
}


/* a[0] c=1, a[1] c=2, a[2] c=3*/
int main(int c, char** a)
{int i, clilen, m;
char *v;
uns porta;
SOCKET r, sc;
HANDLE hf;
char buf[4048];
uns long nu;

struct sockaddr_in cli_addr, srv_addr;
if(c!=3||a[1]==0||a[2]==0)
{l0:;
P("Questo programma si connette a un server\n");
P("Uso: > thisprogram indirizzo NumeroPorta\n");
P("Con numero porta <=%u\n", 0xFFFF);
R 0;
}
if( sscanf(a[1], "%u", &porta)!=1 ) G l0;
if(porta>0xFFFF) G l0;
if(openSock_m()==0)
{P("Errore di apertura socket\n"); R 0;}

if( (r=socket(AF_INET, SOCK_STREAM, 0))!=INVALID_SOCKET)
{F(i=0, v=(char*)&srv_addr; i<sizeof(struct sockaddr_in); ++i)
v=0;
srv_addr.sin_family = AF_INET;
if( (nu=inet_addr(a[1]))==INADDR_NONE)
{P("Errore di inet_addr\n"); G l12;}
srv_addr.sin_addr.s_addr = nu;
srv_addr.sin_port=htons(porta);
m=0;
c0:;
if(connect(r, (sockaddr*) &srv_addr, sizeof(srv_addr)))
{P("Errore di connect\n");
P("Numero errore=%d [WSAECONNREFUSED 10061]\n",
WSAGetLastError());
if(m==0){++m; G c0;}
G l12;
}
if( fgets(buf, 2048, stdin) == 0)
{P("Errore di fgets\n"); G l1;}
nu=strlen(buf);
nu=sendto(sc, buf, nu+1, 0, 0, 0);
P("Bytes mandati %u bytes\n", nu);
P("Mandato: [%s]\n", buf);
}
else {P("Errore di apertura socket\n"); G l2;}
l1:;
if( shutdown (r,SD_BOTH)) P("Errore di shutdown\n");
l12:;
if( closesocket ( r ) )
P("Errore di closesocket\n");
l2:;
if(WSACleanup( )==0) P("Tutto ok socket chiuso\n");
else P("Socket non chiuso\n");

R 0;
}
 
B

Boudewijn Dijkstra

in windows xp
the problem is that if i have a tcp sever istalled in the port 1000 in
my computer, and "cli.exe" is the C program "client program" below and
type


connect of client fail with "WSAECONNREFUSED 10061"
don't understand why

This problem has nothing to do with C, but I'll give you a hint: where is
host 127.1.1.1?
 
R

RoS

In data Tue, 18 Dec 2007 09:20:53 +0100, RoS scrisse:
in windows xp
then there is someone can point out some C program for the use api
"ReadFile" with an handle return by api "socket"?

thank you

i find some error it seems run well now
----------------------------------------------------

#include <WINSOCK2.H>
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <wincon.h>

#define P printf
#define R return
#define F for
#define G goto
#define uns unsigned

int sockSta=0;

int openSock_m(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

if(sockSta==1) R 1;
wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{P("Tell the user that we couldn't find a usable"
"WinSock DLL.");
return 0;
}

/* Confirm that the WinSock DLL supports 1.1 */
/* Note that if the DLL supports versions greater */
/* than 1.1 in addition to 1.1, it will still return */
/* 1.1 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
P( "Tell the user that we couldn't find a usable"
"WinSock DLL., LB=%u HB=%u\n", (unsigned)LOBYTE(
wsaData.wVersion ) ,
(unsigned) HIBYTE( wsaData.wVersion ) );
WSACleanup( );
return 0;
}
sockSta=1;
return 1;
}


/* a[0] c=1, a[1] c=2, a[2] c=3*/
int main(int c, char** a)
{int i, clilen, m;
char *v;
uns porta;
SOCKET r, sc;
HANDLE hf;
char buf[4048];
uns long nu;

struct sockaddr_in cli_addr, srv_addr;
if(c!=3||a[1]==0||a[2]==0)
{l0:;
P("Questo programma si connette a un server\n");
P("Uso: > thisprogram indirizzo NumeroPorta\n");
P("Con numero porta <=%u\n", 0xFFFF);
R 0;
}
if( sscanf(a[1], "%u", &porta)!=1 ) G l0;
^^^^^
this should be a[2]
if(porta>0xFFFF) G l0;
if(openSock_m()==0)
{P("Errore di apertura socket\n"); R 0;}

if( (r=socket(AF_INET, SOCK_STREAM, 0))!=INVALID_SOCKET)
{F(i=0, v=(char*)&srv_addr; i<sizeof(struct sockaddr_in); ++i)
v=0;
srv_addr.sin_family = AF_INET;
if( (nu=inet_addr(a[1]))==INADDR_NONE)
{P("Errore di inet_addr\n"); G l12;}
srv_addr.sin_addr.s_addr = nu;
srv_addr.sin_port=htons(porta);
m=0;
c0:;
if(connect(r, (sockaddr*) &srv_addr, sizeof(srv_addr)))
{P("Errore di connect\n");
P("Numero errore=%d [WSAECONNREFUSED 10061]\n",
WSAGetLastError());
if(m==0){++m; G c0;}
G l12;
}
if( fgets(buf, 2048, stdin) == 0)
{P("Errore di fgets\n"); G l1;}
nu=strlen(buf);
nu=sendto(sc, buf, nu+1, 0, 0, 0);

^^^
this should be "r"
P("Bytes mandati %u bytes\n", nu);
P("Mandato: [%s]\n", buf);
}
else {P("Errore di apertura socket\n"); G l2;}
l1:;
if( shutdown (r,SD_BOTH)) P("Errore di shutdown\n");
l12:;
if( closesocket ( r ) )
P("Errore di closesocket\n");
l2:;
if(WSACleanup( )==0) P("Tutto ok socket chiuso\n");
else P("Socket non chiuso\n");

R 0;
}
 
R

Richard Heathfield

RoS said:

#define P printf
#define R return
#define F for
#define G goto
#define uns unsigned

Before you go anywhere near networking code, take a few classes on how to
write code legibly. When you deliberately make the code harder to read
than it should be, you deter many people from wasting their time reading
it.
 
R

RoS

In data Tue, 18 Dec 2007 09:59:39 +0100, RoS scrisse:
In data Tue, 18 Dec 2007 09:20:53 +0100, RoS scrisse:

or "WriteFile" and socket like below;
why the client WriteFile write it, but the server not read it?

#include <WINSOCK2.H>
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <wincon.h>

#define P printf
#define R return
#define F for
#define G goto
#define uns unsigned

int sockSta=0;

int openSock_m(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

if(sockSta==1) R 1;
wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{P("Tell the user that we couldn't find a usable"
"WinSock DLL.");
return 0;
}

/* Confirm that the WinSock DLL supports 1.1 */
/* Note that if the DLL supports versions greater */
/* than 1.1 in addition to 1.1, it will still return */
/* 1.1 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
P( "Tell the user that we couldn't find a usable"
"WinSock DLL., LB=%u HB=%u\n", (unsigned)LOBYTE(
wsaData.wVersion ) ,
(unsigned) HIBYTE( wsaData.wVersion ) );
WSACleanup( );
return 0;
}
sockSta=1;
return 1;
}


/* a[0] c=1, a[1] c=2, a[2] c=3*/
int main(int c, char** a)
{int i, clilen, m;
char *v;
uns porta;
SOCKET r, sc;
HANDLE hf;
char buf[4048];
uns long bs, nu;

struct sockaddr_in cli_addr, srv_addr;
if(c!=3||a[1]==0||a[2]==0)
{l0:;
P("Questo programma si connette a un server\n");
P("Uso: > thisprogram indirizzo NumeroPorta\n");
P("Con numero porta <=%u\n", 0xFFFF);
R 0;
}
if( sscanf(a[2], "%u", &porta)!=1 ) G l0;
if(porta>0xFFFF) G l0;
if(openSock_m()==0)
{P("Errore di apertura socket\n"); R 0;}

if( (r=socket(AF_INET, SOCK_STREAM, 0))!=INVALID_SOCKET)
{F(i=0, v=(char*)&srv_addr; i<sizeof(struct sockaddr_in); ++i)
v=0;
srv_addr.sin_family = AF_INET;
if( (nu=inet_addr(a[1]))==INADDR_NONE)
{P("Errore di inet_addr\n"); G l12;}
srv_addr.sin_addr.s_addr = nu;
srv_addr.sin_port=htons(porta);
m=0;
c0:;
if(connect(r, (sockaddr*) &srv_addr, sizeof(srv_addr)))
{P("Errore di connect\n");
P("Numero errore=%d [WSAECONNREFUSED 10061]\n",
WSAGetLastError());
if(m==0){++m; G c0;}
G l12;
}
if( fgets(buf, 2048, stdin) == 0)
{P("Errore di fgets\n"); G l1;}
nu=strlen(buf); bs=0;
// nu=sendto(r, buf, nu+1, 0, 0, 0);
if(WriteFile( (void*)r, buf, nu+1, &bs, 0)==0)
P("Errore di write\n");
else P("Ok send %u\n", (uns) bs);
P("Bytes mandati %u bytes\n", (uns) nu);
P("Mandato: [%s]\n", buf);
}
else {P("Errore di apertura socket\n"); G l2;}
l1:;
if( shutdown (r,SD_BOTH)) P("Errore di shutdown\n");
l12:;
if( closesocket ( r ) )
P("Errore di closesocket\n");
l2:;
if(WSACleanup( )==0) P("Tutto ok socket chiuso\n");
else P("Socket non chiuso\n");

R 0;
}
 
R

RoS

In data Tue, 18 Dec 2007 10:29:35 +0100, RoS scrisse:
In data Tue, 18 Dec 2007 09:59:39 +0100, RoS scrisse:


or "WriteFile" and socket like below;
why the client WriteFile write it, but the server not read it?

error
why the client do not write with WriteFile() to the handle returned by
socket()?
(and return error code?)
 
P

pete

Richard said:
RoS said:

Before you go anywhere near networking code,
take a few classes on how to write code legibly.
When you deliberately make the code harder to read than it should be,
you deter many people from wasting their time reading it.

It's been over four years of these horrible macros here.

--
pete
"I have to tell you just how bad your style is.
I was going to respond to one of Joona's posts
and say that I thought that he was going a little overboard,
because I don't like to read posters complaining about
other posters, but that would just be me complaining
about him complaining about you, and that's even worse.

The fact is that your post literally made me grimace and cringe.
When I see your name on a post, I know that those macros are
going to be there. I hope that they're not, but then when I open
the post and there they are, time after time, it crushes all hope,
and then Joona gets upset ;) " - pete 4 June 2003
 
S

santosh

pete said:
It's been over four years of these horrible macros here.

You should consider c.l.c to have been spared from his worst
productions. Take a look at his "homemade" x86 assembler syntax that he
posts to alt.lang.asm occasionally. It makes his "code contributions"
to c.l.c look positively literate. :)
 
R

RoS

In data Tue, 18 Dec 2007 08:24:31 -0500, pete scrisse:
It's been over four years of these horrible macros here.

i'm not the kind of person that someone say "Buttati a mare"
and him "si butta"
it is better make errors with my own mind than with other ones
 
T

Tor Rustad

pete said:
It's been over four years of these horrible macros here.

Hmm.. it's time to add another entry in the kill-file then, unlike some
others that use GC, I don't.

Once in, it's no way out...
 
C

CBFalconer

santosh said:
You should consider c.l.c to have been spared from his worst
productions. Take a look at his "homemade" x86 assembler syntax
that he posts to alt.lang.asm occasionally. It makes his "code
contributions" to c.l.c look positively literate. :)

I have only seen his abortions a couple of times in about five
years (barring quoters). This may be connected with the immediate
inclusion of his revised names in my PLONK file on first detection.
 
J

jaysome

in windows xp
the problem is that if i have a tcp sever istalled in the port 1000 in
my computer, and "cli.exe" is the C program "client program" below and
type


connect of client fail with "WSAECONNREFUSED 10061"
don't understand why

there is someone expert in this there?
what is the answer?

then there is someone can point out some C program for the use api
"ReadFile" with an handle return by api "socket"?

thank you
----------------------------------------------------

#include <WINSOCK2.H>
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <wincon.h>

Only 1 out of 5 of these headers are standard C headers. The rest are
specific to Microsoft Windows. This newsgroup is about discussing
topics related to Standard C, and has nothing to do with
Windows-specific or any other OS-specific matters.
#define P printf
#define R return
#define F for
#define G goto
#define uns unsigned

These #defines are absolutely horrendous, to an exponential degree.

Think about it--anyone who reads your code and sees a use of these
#defines will immediately have to go back to these #defines to see
what they mean.

You, in your own mind, may have memorized that "P" means "printf" and
"F" means "for", but can you really expect that the readers of your
code can be trained to memorize these like you? I know I can't--and
won't.

And what was it that you think you really accomplished with these
#defines? These types of things belong in an obfuscation code contest,
not in real-world code.

[rest snipped because off-topicality]
 
S

santosh

<snip>

It's futile correcting this guy. He does what he wants, when he wants.
He is a troll.
 
R

Richard Heathfield

santosh said:
<snip>

It's futile correcting this guy. He does what he wants, when he wants.
He is a troll.

What he is learning from these corrections is that his obfuscatory
strategem is counter-productive - he is not getting the help he seeks
because people can't be bothered to decode his source.
 
K

Keith Thompson

jaysome said:
These #defines are absolutely horrendous, to an exponential degree.

Indeed.

RoS has been posting for *years*, under various names, using these
horrendous macros. All attempts to get him to stop have been futile.
I recommend killfiling him, or at least not bothering to respond.
(Expect to have to update your killfile next time he changes the name
under which he posts; he's also posted as "RoSsIaCrIiLoIA" and
"av <[email protected]>", among other aliases.)
 
J

Johannes Bauer

Keith said:
RoS has been posting for *years*, under various names, using these
horrendous macros. All attempts to get him to stop have been futile.
I recommend killfiling him, or at least not bothering to respond.
(Expect to have to update your killfile next time he changes the name
under which he posts; he's also posted as "RoSsIaCrIiLoIA" and
"av <[email protected]>", among other aliases.)

His style reminds me *so* much of Jeff Relf from c.o.l.a. He used to
post the source code of his self-written "newsreder" called X.EXE. It
was written in C++, but only because his compiler didn't understand C99
back then (// comments, variable declarations inside scope).

Still makes me feel a little sick in the stomach, thinking about that
code. It was *way* more horrible than the above one, as he also
introduced some "loop" (L) defines which would open half a scope
(another define would then close that scope back again). Terrible.

Greetings,
Johannes
 
P

pete

Richard said:
santosh said:


What he is learning from these corrections is that his obfuscatory
strategem is counter-productive - he is not getting the help he seeks
because people can't be bothered to decode his source.

I don't think that someone who has learned nothing
after four years of exposure to correction on one single point,
can be accurately described as "learning".
 
R

RoS

In data Wed, 19 Dec 2007 09:40:48 -0500, pete scrisse:
I don't think that someone who has learned nothing
after four years of exposure to correction on one single point,
can be accurately described as "learning".

i have lerned enought all you seems not
 
E

Ed Prochak

In data Wed, 19 Dec 2007 09:40:48 -0500, pete scrisse:







i have lerned enought all you seems not

Funny that if you learned so much, why are you the one asking for
help?

Ed
 
D

dj3vande

Hmm.. it's time to add another entry in the kill-file then, unlike some
others that use GC, I don't.

Once in, it's no way out...

I have a whole section for the OP in my killfile; it looks like it's
time to add another line to it.


dave
(I do occasionally clean out my killfile, but known repeat offenders stay in)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top