Sockets en Windows
Estos programas fueron la base de muchos otros
Cliente
Servidor
Código de Cliente
CSocket* sockClient = NULL; BOOL aceptar = FALSE; void CClienDlg::OnButton1(){ if(sockClient != NULL){ delete sockClient; m_envia = ""; m_recive = ""; UpdateData(FALSE); } UpdateData(FALSE); sockClient = new CSocket; sockClient->Create(); BOOL resul; int contador = 0; do{ contador ++; resul = sockClient->Connect("LOCALHOST", 1000); if(contador > 3){ MessageBox("Tiempor de espera superado", "Time Out"); return; } } while(resul == 0); aceptar = TRUE; char cadena[1000]; int datos = sockClient->Receive(cadena, 1000); cadena[datos] = 0; m_receive = cadena; //MessageBox(cadena, "Cadena Recibida"); UpdateData(FALSE); } void CClienDlg::OnEnviar(){ UpdateData(TRUE); char cadena[1000] = ""; if( m_envia.GetLength() == 0 ){ return; } strcpy(cadena,(const char*) m_envia); //MessageBox(m_envia,"Envio"); sockClient->Send(cadena,strlen(cadena)); strcpy(cadena,"Se rompio la conexion con el servidor"); int datos = sockClient->Receive(cadena,1000); cadena[datos] = 0; m_recive = cadena; UpdateData(FALSE); }
Código del Servidor
// SERVIDOR DE INFORMACION CSocket sockSrvr; // construct a socket if(sockSrvr.Create(1000)==0){ MessageBox("No puedo establecer conexion en servidor"); } if(sockSrvr.Listen()==0) {} CSocket sockRecv; // establecer el tiempo maximo de resppuesta DWORD inicio = GetTickCount(); while(sockSrvr.Accept( sockRecv )==0){ int resul = sockSrvr.GetLastError(); DWORD actual = GetTickCount(); if(actual > inicio + 10000) { MessageBox("Finalizado el tiemp maximo de espera","Time out"); return; } char cadena[100]; sprintf(cadena,"%ld",resul); //MessageBox(cadena); switch(resul){ case WSAEFAULT: { MessageBox("The lpSockAddrLen argument is too small (less than the size of a SOCKADDR structure)"); return; } case WSAEINVAL: { MessageBox("Listen was not invoked prior to accept"); return; } case WSAEWOULDBLOCK: { MessageBox("The socket is marked as nonblocking and no connections are present to be accepted. "); return; } case WSANOTINITIALISED: { MessageBox("A successful AfxSocketInit must occur before using this API."); return; } case WSAENETDOWN: { MessageBox(" The Windows Sockets implementation detected that the network subsystem failed."); return; } case WSAEADDRINUSE: { MessageBox("The specified address is already in use."); return; } case WSAEINPROGRESS: { MessageBox("A blocking Windows Sockets call is in progress."); return; } case WSAEISCONN: { MessageBox(" The socket is already connected."); return; } case WSAEMFILE: { MessageBox("No more file descriptors are available."); return; } case WSAENOBUFS: { MessageBox("No buffer space is available. The socket cannot be connected."); return; } case WSAENOTSOCK: { MessageBox("The descriptor is not a socket."); return; } case WSAEOPNOTSUPP: { MessageBox("no es soportado"); return; } default: { //MessageBox("Ninguno"); } } } char cadena[1000] = "Conexion establecida"; // MessageBox(cadena,"Cadena enviada"); sockRecv.Send(cadena,strlen(cadena)); int conta = 0; while(strcmp(cadena,"FIN" ) != 0 && conta < 5) { conta ++; strcpy(cadena,""); int datos = sockRecv.Receive(cadena,1000); cadena[datos] = 0; //MessageBox(cadena,"Cadena Recibida"); sockRecv.Send(cadena,strlen(cadena)); } MessageBox("Fin de servidor");