www.pudn.com > openwebspiderv0.1a.zip > socket.h


 
/* OpenWebSpider 
 *  
 *     Coded by Shen139 
 *     shen139 [at] eviltime (dot) com 
 * 
 * 
 * This file is part of OpenWebSpider 
 * 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 * 
 */ 
 
 
#ifdef linux
#include 
#include 
#include 
#include 
#include 
#else
#pragma comment(lib,"wsock32.lib") 
#include 
#include 
#endif

#include  
 
#include "htmlfnct.h"
#define PORT				80

#ifdef linux
struct sockaddr_in saddr;
#else
SOCKADDR_IN saddr;
#endif

struct hostent *hp; 
 
#ifdef linux
int setnonblock(int sock)
#else
int setnonblock(SOCKET sock)
#endif
{
struct timeval timeout;

	timeout.tv_sec = TIMEOUTs; 
	timeout.tv_usec = 0;
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char*) &timeout, sizeof(timeout)))
		return 0;
return 1;
}

#ifdef WIN32
int StartUpWinsock()
{
WSADATA wsadata;

	if((WSAStartup(MAKEWORD(1,1),&wsadata))!=0 || (LOBYTE(wsadata.wVersion )!=1 || HIBYTE(wsadata.wVersion)!=1))
		return 0;

return 1;
}
#endif

#ifdef linux
int LoadSocket(int *sock,	struct sHost* shost,char* LastHostOK)
#else
int LoadSocket(SOCKET *sock,struct sHost* shost,char* LastHostOK)
#endif
{ 
	*sock=socket(AF_INET,SOCK_STREAM,0);
#ifdef linux
	if (*sock<0)
#else
	if (*sock==INVALID_SOCKET)
#endif
	{
		return 0;
	}

	if((hp=gethostbyname(shost->Host))==0)
	{
		strncpy(shost->Page,shost->Host,MAXPAGESIZE);
		strncpy(shost->Host,LastHostOK,MAXHOSTSIZE);

		if((hp=gethostbyname(shost->Host))==0)		//nothing to do
				return 0;
	}

	saddr.sin_addr.s_addr=((struct in_addr *)(hp->h_addr))->s_addr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(PORT); 
	 
	setnonblock(*sock); 

return 1;
} 
 
#ifdef linux 
int RecvPackets(int sock,		char* packet, int maxlen) 
#else 
int RecvPackets(SOCKET sock,	char* packet, int maxlen) 
#endif 
{ 
int x=0; 
int recvdbytes=0; 
int iRetry=MAXRETRY; 
 
		memset(packet,0,maxlen); 
		x=0; 
		recvdbytes=0; 
		iRetry=0; 
 
		do 
		{ 
 
#ifdef linux 
		recvdbytes=recv(sock,packet+x,MAXPACKETSIZE-x,MSG_WAITALL); 
#else 
		recvdbytes=recv(sock,packet+x,MAXPACKETSIZE-x,0); 
#endif 
		if(recvdbytes!=-1) 
			x+=recvdbytes; 
		else 
			iRetry++; 
 
		} while(recvdbytes!=0 && x