Re: TCP client program
- From: "Volodymyr Shcherbyna" <v_scherbina@xxxxxxxxxxxxxxx>
- Date: Thu, 20 Dec 2007 11:56:33 +0100
The problem is that you improperly parse IP and port.
Simply changing the following lines in the code fixes it:
// COMMENT this: srv_addr.sin_addr.s_addr = nu;
srv_addr.sin_addr.s_addr = inet_addr("207.46.197.32");
// COMMENT this: srv_addr.sin_port=htons(porta);
srv_addr.sin_port=htons(80);
Connection to www.microsoft.com will succed
--
Volodymyr
"RoS" <Ros@xxxxxxxxx> wrote in message
news:tv2fm319pgdrghd8ked74ddrbc189ks0d2@xxxxxxxxxx
In data Tue, 18 Dec 2007 09:20:58 +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"?
if this is not the right NG can you point out some NG more on topic on
that argument?
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;
}
.
- Follow-Ups:
- Re: TCP client program
- From: RoS
- Re: TCP client program
- References:
- TCP client program
- From: RoS
- Re: TCP client program
- From: RoS
- TCP client program
- Prev by Date: Error code : 12019
- Next by Date: Re: TCP client program
- Previous by thread: Re: TCP client program
- Next by thread: Re: TCP client program
- Index(es):
Relevant Pages
|