- 论坛徽章:
- 0
|
$ cat cctst.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define MAXDATASIZE 10*2048*2048 /*每次最大数据传输量 */
main(int argc, char **argv)
{
int sockfd, recvbytes,iconn,portno;
char buf[MAXDATASIZE],*p;
struct hostent *host;
struct sockaddr_in serv_addr;
if (argc < 2)
{
printf("Please enter the server's hostname!\n");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname出错!\n");
exit(1);
}
if((portno=atoi(argv[2]))==NULL)
{
printf("port出错!\n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket创建出错!\n");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(portno);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
CONN:
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr
)) == -1)
{
printf("connect出错!\n");
close(sockfd);
exit(1);
}
iconn=0;
while(1)
{
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) <=0 )
{
printf("recv出错!\n");
iconn++;
if(11==iconn)
{
close(sockfd);
goto CONN;
}
continue;
}
buf[recvbytes] = '\0';
p=(char *)buf;
while(*p!='\0')
{
if(*p=='\x0d')
strcpy(p,p+1);
p++;
}
printf("%s",buf);
fflush(0);
}
}
$
[[i] 本帖最后由 sdjnzhoumq 于 2007-4-24 14:37 编辑 [/i]] |
|