Re: TCP client program



In data Tue, 18 Dec 2007 09:59:45 +0100, RoS scrisse:

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"?

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[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); 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;
}



.



Relevant Pages

  • Re: [1/2OT] a buggy TCP client program
    ... or "WriteFile" and socket like below; ... why the client WriteFile write it, but the server not read it? ... int openSock_m ... uns long bs, nu; ...
    (comp.lang.c)
  • Re: TCP client program
    ... why the client WriteFile write it, but the server not read it? ... or "WriteFile" and socket like below; ... int openSock_m ... uns long bs, nu; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Linux 2.6.29.4
    ... static int is_efer_nx ... if (ret) ... struct ftdi_private { ... the specific security attributes of the socket ...
    (Linux-Kernel)
  • [PATCH] paccept, socket, socketpair w/flags
    ... there is a helper function to convert the new socket flags into ... socket() and socketpair. ... static int status; ... int fd1, fd2, err; ...
    (Linux-Kernel)
  • Re: reconnect tcp
    ... instance throws a fatal exception, ... to close the socket and reconnect. ... public ClientHandler(Socket client, int timeout) ... while (rxDataCounter < receiveBytes) ...
    (microsoft.public.dotnet.languages.csharp)

Loading