int appTCP::Open()
{
//-------- preparo el socket
sockaddr_in service;
ZeroMemory(&service,sizeof(service));
service.sin_family = AF_INET;
service.sin_addr.s_addr = htonl(INADDR_ANY);
service.sin_port = htons(Port);
//creamos un socket para TCP
this->Socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (this->Socket==SOCKET_ERROR) return 1;
//limpiamos nuestra address para hacer de host
memset((void *)&Host, ' ', sizeof(struct sockaddr_in));
//tomo los datos del host
/* Set family and port */
Host.sin_family = AF_INET;
Host.sin_port = htons(Port);
/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
HostProperties = gethostbyname(host_name); //cojemos el nombre de la maquina local
/* Check for NULL pointer */
if (HostProperties == NULL)
return 1;
/* Assign the address */
Host.sin_addr.S_un.S_un_b.s_b1 = HostProperties->h_addr_list[0][0];
Host.sin_addr.S_un.S_un_b.s_b2 = HostProperties->h_addr_list[0][1];
Host.sin_addr.S_un.S_un_b.s_b3 = HostProperties->h_addr_list[0][2];
Host.sin_addr.S_un.S_un_b.s_b4 = HostProperties->h_addr_list[0][3];
/*tenemos la ip , hagamosla string*/
sprintf(StringIP,"%d.%d.%d.%d",Host.sin_addr.S_un.S_un_b.s_b1,Host.sin_addr.S_un.S_un_b.s_b2,Host.sin_addr.S_un.S_un_b.s_b3,Host.sin_addr.S_un.S_un_b.s_b4);
TimeOut.tv_usec = msTimeout%1000;
TimeOut.tv_sec = msTimeout/1000; // Time in seconds
u_long iMode = 1;
ioctlsocket (Socket, FIONBIO, &iMode);
err=WSAGetLastError();
FD_ZERO (&fdSocket);
FD_SET (Socket, &fdSocket); //ponemos el socket en vigilancia
bind(Socket,(struct sockaddr *)&Host,sizeof(sockaddr));
listen(Socket,SOMAXCONN);
unsigned int i=0;
err=WSAGetLastError();
sockaddr Incoming;
int size=sizeof(Incoming);
do
{
err=0;
SOCKET r=accept(Socket,&Incoming,&size);
if (r==-1)
err=WSAGetLastError();
}while (err==WSAEWOULDBLOCK);
return 0;
}