|
发表于 2005-10-14 14:18:31
|
显示全部楼层
客户端的应该是这样的:
include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#define HTTPDATALEN 1024
#define SERVERIP "*.*.*.*"//此为服务器的IP地址
#define SERVERHOST "**"//此为服务器的名字
#define SERVERGET "/*/*"//此为要访问的网站
#define SERVERPORT 80
#define DEBUG
int DoInitialize();
int SendRequest(int socket);
int GetResponse(int socket);
int main (int argc, char ** argv)
{
int ret;
int socksd;
socksd = DoInitialize();
if (ret <0)
{
close (socksd);
return -1;
}
ret = SendRequest(socksd);
if (ret <0)
{
close (socksd);
return -1;
}
ret = GetResponse(socksd);
if (ret <0)
{
close (socksd);
return -1;
}
close (socksd);
}
int DoInitialize()
{
int sock,conn_ret,ret;
struct sockaddr_in addr;
struct hostent *servhost;
struct sockaddr_in server;
struct servent *service;
if (inet_aton(SERVERIP,&addr.sin_addr)==0)
{
#ifdef DEBUG
printf("sockaddr init error!%s\n","exit");
#endif
return -1;
}
servhost = gethostbyaddr((char *)&addr.sin_addr,4,AF_INET);
#ifdef DEBUG
printf("sockaddr init ok!%s\n","continue");
#endif
bzero((char *)&server, sizeof(server));
server.sin_family = AF_INET;
bcopy(servhost->h_addr, (char *)&server.sin_addr,
servhost->h_length);
server.sin_port = htons(SERVERPORT);//
#ifdef DEBUG
printf("sockaddr init ok!%s\n","continue");
#endif
// init sock
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <0)
{
printf("socket init error!%s\n","exit");
return -1;
}
#ifdef DEBUG
printf("socket init ok!%s\n","continue");
#endif
// connect sock
conn_ret = connect (sock, (struct sockaddr*) &
server, sizeof (server));
// connect sock error
if (conn_ret <0)
{
#ifdef DEBUG
printf("connnection error!%s\n","exit");
#endif
return -1;
}
#ifdef DEBUG
printf("connnection ok!%s\n","continue");
#endif
return sock;
}
int SendRequest( int sock)
{
int nwbytes;
char *sendmsg;
char strReq[HTTPDATALEN*64];
memset(strReq, 0, sizeof(strReq));
// set the request to accord with http protocal
strcat(strReq, "GET ");
strcat(strReq, SERVERGET);
strcat(strReq, " HTTP/1.0\r\n");
strcat(strReq, "Content-length: ");
strcat(strReq, "0");
strcat(strReq, "\r\n");
strcat(strReq, "Content-type: ");
strcat(strReq, "application/x-www-form-urlencoded");
strcat(strReq, "\r\n");
strcat(strReq, "Host: ");
strcat(strReq, SERVERHOST);
strcat(strReq, "\r\n");
strcat(strReq, "User-Agent: ");
strcat(strReq,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
strcat(strReq, "\r\n");
strcat(strReq, "\r\n");
strcat(strReq, "\r\n");
sendmsg = strReq;
// send the request to server
nwbytes = write(sock,sendmsg,strlen(sendmsg));
if(nwbytes<0)
{
#ifdef DEBUG
printf("send message error!%s\n","exit");
printf("close the sock !\n");
#endif
close (sock);
return -1;
}
#ifdef DEBUG
printf("send message ok!%s\n","continue");
#endif
return 0;
}
int GetResponse(int sock)
{
unsigned int j;
int ret;
//http://pc065:8080/applyld/counterStatic_Init.do
char rbuf[1024];
char strRes[HTTPDATALEN*64];
#ifdef DEBUG
printf("============= REQUEST BEGIN ==============%s\n","=");
#endif
memset(strRes, 0, sizeof(strRes));
strcpy(strRes, "");
while ((ret=read(sock, rbuf, sizeof(rbuf)-1)) > 0)
{
rbuf[ret] = '\0';
strcat(strRes, rbuf);
memset(rbuf, 0, sizeof(rbuf));
}
#ifdef DEBUG
printf("the response data is:\n");
for (j=0;j<(HTTPDATALEN*64);j++)
{
printf("%c",strRes[j]);
}
#endif
if(strstr(strRes,"OK")||strstr(strRes,"200"))
{
printf("sucess!\n\n");
}
else
{
printf("have problem !\n\n");
}
#ifdef DEBUG
printf("============= REQUEST END ==============%s\n","=");
printf("close the sock !\n");
#endif
//close sock
close (sock);
return 0;
}
已经测试过,可以使用 |
|