Re: [1/2OT] a buggy TCP client program



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[i]=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;
}



.



Relevant Pages

  • [1/2OT] a buggy TCP client program
    ... int openSock_m ... WSADATA wsaData; ... int main(int c, char** a) ... G l12; ...
    (comp.lang.c)
  • TCP client program
    ... int openSock_m ... WSADATA wsaData; ... int main(int c, char** a) ... G l12; ...
    (microsoft.public.win32.programmer.networks)
  • Re: TCP client program
    ... int openSock_m ... WSADATA wsaData; ... int main(int c, char** a) ... G l12; ...
    (microsoft.public.win32.programmer.networks)
  • Re: TCP client program
    ... int openSock_m ... WSADATA wsaData; ... int main(int c, char** a) ... G l12; ...
    (microsoft.public.win32.programmer.networks)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)