- 求职 : 数据库管理员
- 论坛徽章:
- 0
|
請各位幫忙分析一下,問題在哪?
謝謝
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "socket/socketutl.h"
#define BSIZE 1024
struct sockfile
{
char buf[BSIZE +1];
char *tok , *bot;
char *lim;
int sock ;
int state ; //socket state
};
int sk_open(struct sockfile *sf, int sock)
{
sf->tok = sf->buf;
sf->lim = sf->buf ;
sf->bot = sf->buf;
sf->sock = sock ;
sf->state = 0;
return 0;
}
int sk_fill(struct sockfile *sf)
{
if (sf->state <0) {
return -1 ;
}
int cnt = sf->lim - sf->tok;
if(cnt>0){
memcpy(sf->buf, sf->tok, cnt);
sf->tok = sf->buf ;
sf->bot = sf->buf ;
sf->lim = sf->buf + cnt ;
}
int rcnt = BSIZE - cnt ;
if((cnt = recv(sf->sock, sf->lim, rcnt, 0)) >0){
sf->lim[cnt] = '\0';
sf->lim += cnt ;
} else {
sf->state=-1 ;
return -1 ;
}
return 0;
}
int sk_readline(struct sockfile *sf, char **line, int *len)
{
char *ptr ;
start:
sf->bot = sf->tok ;
ptr = sf->bot ;
while (ptr < sf->lim) {
if (*ptr== '\n') {
if (ptr[-1] =='\r') {
ptr[-1] = '\0' ;
}
*ptr='\0';
*len = ptr - sf->bot -1;
sf->tok = ++ptr ;
*line =sf->bot ;
return 0;
}
ptr ++;
}
if (sf->state <0){
return -1;
}
sk_fill(sf) ;
goto start ;
}
int main()
{
int sock;
char *host = "172.17.2.43";
if ((sock= tcp_connect(host, 80)) < 0) {
printf("connect error\n") ;
return -1 ;
}
char header[1024] ;
sprintf(header,"%s%s%s%s",
"Pragma:no-cache\r\n",
"Accept-Encoding:GZIP\r\n",
"Connection:close\r\n",
"User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT5)\r\n");
tcp_send(sock, "GET %s HTTP/1.0\r\n%s \r\n\r\n", "/auto_update/y3_cost/md5sum.txt", header);
char *line =NULL;
struct sockfile sk ;
sk_open(&sk, sock);
int cnt ;
int len=0;
while (1) {
cnt =sk_readline(&sk, &line, &len);
if (cnt <0) break ;
printf("size:%3d [%s]\n\n", len, line);
}
close(sock);
exit(1);
}
|
|
|