Acerca del socket de escritura vc++, error de vinculación
1. Servidor simple
//#include
//#pragma comment(lib,"WS2_32.lib") p>
WSADATA wsd;
puerto UINT estático=%%1;
Escucha UINT(LPVOID pParam)
{
SOCKET sServer,sClient;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)! =0)
{
return -1;//Error
}
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP) ;
if(INVALID_SOCKET==sServer)
{
WSACleanup();
return -1;//Crear socket Word falló
}
SOCKADDR_IN addrServ;
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons((short)pParam ) ;
addrServ.sin_addr.s_addr=INADDR_ANY;
retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if( SOCKET_ERROR ==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//bind falló para definir el socket
}
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//Error al comenzar a escuchar
} p >
sockaddr_in addrClient;
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
if(INVALID_SOCKET==sClient)
{
closesocket(sServer);
WSACleanup();
return - 1 ;//Error al comenzar a aceptar conexiones de clientes
}
ZeroMemory(buf,sizeof(buf));
retVal=recv(sClient,buf, sizeof (buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
closesocket ( sClient);
WSACleanup();
retu
rn -1;//Error al recibir datos
}
CString %%2(buf);
closesocket(sServer);
closesocket(sClient);
WSACleanup();
devuelve 0;
}
CWinThread *pThread=AfxBeginThread(Escuchar , &port);
2. Cliente simple
//#include
//#pragma comment(lib,"WS2_32.lib ")
WSADATA wsd;
SOCKET sHost;
SOCKADDR_IN servAddr;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&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;< / p>
servAddr.sin_addr.s_addr=inet_addr(%%3);
servAddr.sin_port=htons((short)%%2);
int nServAddlen= tamaño de (servAddr);
retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(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;//Error al enviar datos al servidor
}
closesocket(sHost);
WSACleanup();
3. Obtener la IP local
//#include
//#pragma comment(lib,"WS2_32.lib")
WSADATA wsd ;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;// falló
}
char szHostname[100],szHostaddress[20
0];
if(gethostname(szHostname,sizeof(szHostname))!=SOCKET_ERROR)
{
HOSTENT *pHostEnt=gethostbyname(szHostname);
if(pHostEnt!=NULL){
sprintf(szHostaddress,"%d.%d.%d.%d",
( pHostEnt-> h_addr_list[0][0]&0x00ff ),
( pHostEnt->h_addr_list[0][1]&0x00ff ),
( pHostEnt->h_addr_list[0][2]&0x00ff ),
( pHostEnt->h_addr_list[0][3]&0x00ff ));
}
}
else p>
return;
CString %%1(szHostaddress);
4. Comunicación de extremo a extremo
//#include //#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 el socket } SOCKADDR_IN servAddr; servAddr.sin_family=AF_INET; servAddr.sin_addr.s_addr=inet_addr(%%1); servAddr.sin_port=htons(INADDR_ANY); if(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)); if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)&servAddr,&nServAddrlen)==SOCKET_ERROR) { closesocket(s); WSACleanup(); return -1;//Error al recibir 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*)&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 //#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 Escucha(LPVOID pParam) { addrServ.sin_family=AF_INET; addrServ.sin_port=htons((UINT)pParam); addrServ.sin_addr .s_addr=INADDR_ANY; retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN)); if(SOCKET_ERROR==retVal) { closesocket(sServer); WSACleanup(); return -1;//Error al vincular el socket } retVal=listen(sServer,1); if(SOCKET_ERROR==retVal) { closesocket(sServer) ; WSACleanup(); return -1;//Error al iniciar la supervisión } 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 } >mientras(listenerRun) { MemoriaCero(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),&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(Escuchar,&portar); sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(INVALID_SOCKET==sHost) { WSACleanup(); return -1;//Error al crear el socket } servAddr2. 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) p> { closesocket(sHost); WSACleanup(); return -1; //La conexión falló } zeroMemory(buf,sizeof(buf)); strcpy(buf,%%5); 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->m_hThread,&dwExitCode); pThread=null; closesocket(sServer); closesoc ket(sClient); closesocket(sHost); WSACleanup();