Red de conocimiento informático - Consumibles informáticos - ¡Recompensa por puntuación alta! Estoy usando VC para diseñar programación de Socket con estructura C_S.

¡Recompensa por puntuación alta! Estoy usando VC para diseñar programación de Socket con estructura C_S.

Tu solicitud es demasiado complicada. Solo puedo dar algunos ejemplos simples para amigos que tengan tiempo de combinarlos según tu solicitud.

1. Servidor simple

/*

#include lt; winsock2.hgt

#pragma comment(lib, "WS2_32) .lib")

*/

WSADATA wsd;

puerto UINT estático=1;

escucha UINT (LPVOID pParam)

{

SOCKET sServer, sClient;

char buf[1024]

int retVal

if; (WSAStartup(MAKEWORD(2, 2), amp; wsd)!=0)

{

return -1; //Error

}

sServer=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (INVALID_SOCKET==sServer)

{

WSACleanup();

return -1; //Error al crear el socket

}

SOCKADDR_IN addrServ;

addrServ.sin_family=AF_INET;

p>

addrServ.sin_port=htons((short)pParam);

addrServ.sin_addr.s_addr=INADDR_ANY;

retVal=bind(sServer, ( LPSOCKADDR)amp; addrServ, tamaño de(SOCKADDR_IN));

if(SOCKET_ERROR==retVal)

{

closesocket(sServer);

WSACleanup ();

return -1; //No se pudo vincular el socket

}

retVal=listen(sServer, 1);

if(SOCKET_ERROR==retVal)

{

closesocket(sServer

WSACleanup(); return - 1; //No se pudo iniciar la escucha

}

sockaddr_in addrClient;

int addrClientlen=sizeof(addrClient);

sClient=aceptar (sServer, (sockaddr FAR*) & addrClient, & addrClientlen);

if (INVALID_SOCKET==sClient)

{

closesocket(sServer ) ;

WSACleanup();

return -1; //No se pudo comenzar a aceptar conexiones de cliente

}

ZeroMemory(buf , sizeof(buf));

retVal=recv(sClient,buf,sizeo

f(buf), 0);

if(SOCKET_ERROR==retVal)

{

closesocket(sServer);

closesocket (sClient);

WSACleanup();

return -1; // Error al recibir datos

}

CString 2( buf );

closesocket(sServidor);

closesocket(sCliente);

WSACleanup();

devuelve 0; p >

}

CWinThread *pThread=AfxBeginThread(Escuchar, amp; puerto);

2. Cliente simple

/*

#include lt;winsock2.hgt;

#pragma comment(lib, "WS2_32.lib")

*/

WSADATA wsd;

SOCKET sHost;

SOCKADDR_IN servAddr;

char buf[1024];

int retVal;

if (WSAStartup(MAKEWORD(2, 2),amp;wsd)!=0)

{

return -1; //Error

}

sHost=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(INVALID_SOCKET==sHost)

{

WSACleanup();

return -1; //Error al crear el socket

}

servAddr.sin_family=AF_INET;

servAddr.sin_addr.s_addr = inet_addr(3);

servAddr.sin_port=htons((short)2);

int nServAddlen=sizeof(servAddr);

retVal=connect ( sHost, (LPSOCKADDR) & servAddr, tamaño de (servAddr));

if (SOCKET_ERROR==retVal) {

closesocket(sHost);

WSACleanup( );

return -1; //Error al conectarse al servidor

}

ZeroMemory(buf, sizeof(buf));

strcpy(buf, 3);

retVal=send(sHost, buf, sizeof(buf), 0

if (SOCKET_ERROR==retVal)

{

closesocket(sHost);

WSACleanup();

return -1 //No se pudieron enviar datos al servidor

p>

}

closesocket(sHost);<

/p>

WSACleanup();

3 Obtener la IP local

/*

#include lt; >

#pragma comment(lib, "WS2_32.lib")

*/

WSADATA wsd;

if(WSAStartup(MAKEWORD(2, 2), amp; wsd)!=0)

{

return -1; //Error

}

char szHostname [100], szHostaddress[200];

if(gethostname(szHostname, sizeof(szHostname))!=SOCKET_ERROR)

{

HOSTENT *pHostEnt= gethostbyname(szHostname);

if(pHostEnt!=NULL){

sprintf(szHostaddress, "d.d.d.d",

( pHostEnt-gt; h_addr_list[0 ][0]amp; 0x00ff ),

( pHostEnt-gt; h_addr_list[0][1]amp; 0x00ff ),

( pHostEnt-gt; h_addr_list[0][ 2]amp; 0x00ff ),

( pHostEnt-gt; h_addr_list[0][3]amp; 0x00ff ));

}

}

else

return;

CString 1(szHostaddress);

4. Comunicación de extremo a extremo

/*

#include lt;winsock2.hgt;

#pragma comment(lib, "WS2_32.lib")

*/

WSADATA wsd;

SOCKET s;

char buf[1024];

if(WSAStartup(MAKEWORD(2,2),&wsd)!= 0)

{

return -1; //Error

}

s=socket(AF_INET, SOCK_DGRAM, 0);

if(s==INVALID_SOCKET)

{

WSACleanup();

return -1 // Error al crear socket

}

SOCKADDR_IN servAddr;

servAddr.sin_family=AF_INET;

servAddr.sin_addr.s_addr=inet_addr(1); p>

servAddr.sin_port=htons(INADDR_ANY);

if(bind(s, (SOCKADDR*)amp; servAddr, sizeof(SOCKADDR_IN))==SOCKET_ERROR)

{

cierre(s);

<

p> WSACleanup();

return -1; //No se pudo vincular el socket

}

int nServAddrlen=sizeof(servAddr);

p>

ZeroMemory(buf, sizeof(buf));

if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)amp;servAddr,amp;nServAddrlen)= =SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1; datos

}

CString 2(buf)

ZeroMemory(buf, sizeof(buf));

strcpy(buf, 3 );

SOCKADDR_IN clientAddr;

clientAddr.sin_family=AF_INET;

clientAddr.sin_addr.s_addr=inet_addr(4);

clientAddr.sin_port=htons((short)5);

int nClientlen=sizeof(clientAddr);

if(sendto(s,buf,sizeof(buf),0, ( SOCKADDR*)amp; clientAddr, nClientlen)==SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1; //Error al enviar datos al servidor

}

closesocket(s);

WSACleanup();

5. Comunicación punto a punto

/*

#include lt;winsock2.hgt;

#pragma comment(lib, "WS2_32 .lib")

*/

WSADATA wsd;

SOCKADDR_IN addrServ, addrServ2;

SOCKET sServer, sClient, sHost;

int retVal;

sockaddr_in addrClient;

char buf[1024];

puerto UINT estático=2;

BOOL listenerRun=TRUE;

UINT Listen(LPVOID pParam)

{

addrServ.sin_family=AF_INET;

addrServ. sin_port=htons((UINT) pParam);

addrServ.sin_addr.s_addr=INADDR_ANY;

retVal=bind(sServer, (LPSOCKADDR)amp; addrServ, sizeof(SOCKADDR_IN)) ;

si(SOCKET_ERROR==retVal)

{

closesocket(sServer);

WSACleanup();

return -1 // Error al vincular el socket

p>

}

retVal=listen(sServer, 1);

if(SOCKET_ERROR==retVal)

{

closesocket(sServer);

WSACleanup();

return -1; //No se pudo iniciar la escucha

}

int addrClientlen=sizeof(addrClient);

sClient=accept(sServer, (sockaddr FAR*)&addrClient,&addClientlen);

if (INVALID_SOCKET==sClient)

{

closesocket(sServer);

WSACleanup();

return -1 //No se pudo recibir la solicitud del cliente

p>

}

while(listenerRun)

{

ZeroMemory(buf, sizeof(buf));

retVal =recv( sClient, buf, sizeof(buf));

if(SOCKET_ERROR==retVal)

{

closesocket(sServer);

closesocket(sClient);

WSACleanup();

return -1 // Error al recibir datos del cliente

}

CString 4(buf);

}

}

if(WSAStartup(MAKEWORD(2, 2), amp; wsd)!=0 )

{

return -1; //Error

}

sServer=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(INVALID_SOCKET==sServer)

{

WSACleanup();

return -1 //Error al crear el socket

}

CWinThread *pThread=AfxBeginThread(Escucha, puerto amp;);

sHost=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); (INVALID_SOCKET==sHost)

{

WSACleanup();

return -1; //Error al crear el socket

}

servAddr2.sin_family=AF_INET;

servAddr2.sin_addr.s_addr=inet_addr(1);

servAddr.sin_port=

htons((short)3);

int nServerAddrlen=sizeof(servAddr2);

retVal=connect(sHost, (LPSOCKADDR) & servAddr2, sizeof(servAddr2));

if(SOCKET_ERROR==retVal)

{

closesocket(sHost

WSACleanup(); return -1; //Error de conexión

}

zeroMemory(buf, sizeof(buf));

strcpy(buf, 5);

p>

retVal=send(sHost,buf,sizeof(buf),0);

if(SOCKET_ERROR==retVal)

{

closesocket(sHost);

WSACleanup();

return -1 // Error al enviar datos a

}

listenerRun= FALSE;

DWORD dwExitCode;

::GetExitCodeThread(pThread-gt;m_hThread,&dwExitCode);

pThread=null;

closesocket(sServer);

closesocket(sClient);

closesocket(sHost);

WSACleanup();

6 .Lado del servidor de sincronización de hora UDP

/*

#include lt; winsock2.hgt

#pragma comment(lib, "WS2_32.lib")

*/

WSADATA wsd;

SOCKET s;

char buf[1024];

if( WSAStartup(MAKEWORD(2, 2), amp; wsd)!=0)

{

return -1; //Error

}

s=socket(AF_INET,SOCK_DGRAM,0);

if(s==INVALID_SOCKET)

{

WSACleanup(); p>

return -1; //Error al crear el socket

}

SOCKADDR_IN servAddr;

servAddr.sin_family=AF_INET;

servAddr.sin_addr.s_addr=inet_addr("127.0.0.1");

servAddr.sin_port=htons(5000);

if(bind(s, ( SOCKADDR *)amp; servAddr, tamaño de(SOCKADDR_IN))==SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1; //No se pudo vincular el socket<

/p>

}

int nServAddrlen=sizeof(servAddr);

MemoriaCero(buf, sizeof(buf));

if(recvfrom( s,buf,sizeof(buf),0,(SOCKADDR*)amp;servAddr,amp;nServAddrlen)==SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1; //Error al recibir datos

}

CString str(buf);

if(str=="TimeNow")

{

SOCKADDR_IN clientAddr;

clientAddr.sin_family=AF_INET;

clientAddr .sin_addr.s_addr=inet_addr("127.0.0.1");

clientAddr.sin_port=htons((short)2000);

int nClientlen=sizeof(clientAddr);

p>

SYSTEMTIME systime

GetLocalTime(amp; systime

if(sendto(s, (char *)amp; systime, sizeof) (SYSTEMTIME), 0, (SOCKADDR*)amp; clientAddr, nClientlen)==SOCKET_ERROR)

{

closesocket(s); ;

return -1; //Error al enviar datos al servidor

}

}

closesocket(s);

WSACleanup( );

7. Cliente de sincronización de hora UDP

/*

#include lt; >

#pragma comentario (lib, "WS2_32.lib")

*/

WSADATA wsd;

SOCKET s;

char buf[1024] ;

if(WSAStartup(MAKEWORD(2,2),amp;wsd)!=0)

{

return -1; //Fallo

}

s=socket(AF_INET,SOCK_DGRAM,0);

if(s==INVALID_SOCKET)

{

WSACleanup();

return -1; //Error al crear el socket

}

SOCKADDR_IN servAddr;

servAddr.sin_family=AF_INET;

servAddr.sin_addr.s_addr=inet_addr("127.0.0.1");

servAddr.sin_port=htons(2000) ;

si(

bind(s, (SOCKADDR*)&servAddr, sizeof(SOCKADDR_IN))==SOCKET_ERROR)

{

closesocket(s);

WSACleanup( );

return -1; // Error al vincular el socket

}

int nServAddrlen=sizeof(servAddr);

ZeroMemory(buf) , sizeof(buf));

CString ss="TimeNow";

strcpy(buf, ss);

SOCKADDR_IN clientAddr;

clientAddr.sin_family=AF_INET;

clientAddr.sin_addr.s_addr=inet_addr("127.0.0.1");

clientAddr.sin_port=htons((corto) 5000);

int nClientlen=sizeof(clientAddr);

if(sendto(s,buf,sizeof(buf),0,(SOCKADDR*)amp;clientAddr,nClientlen) ==SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1 // al servidor No se pudo enviar; datos

}

memset(buf, 0, 1024

if(recvfrom(s,buf,sizeof(buf),0,( SOCKADDR*); )amp;servAddr,amp;nServAddrlen)==SOCKET_ERROR)

{

closesocket(s);

WSACleanup();

return -1; //Error al recibir datos

}

SYSTEMTIME systime;

memcpy(amp;systime,buf,16);

p>

SetLocalTime(amp; systime); //Establece la sincronización de la hora local y del servidor.

cerrar socket(s);

WSACleanup();