- 论坛徽章:
- 0
|
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define MAX_LINE 4096
int main(int argc,char* argv[])
{
int socketfd,n;
char recline[MAX_LINE+1];
struct sockaddr_in servaddr;
if(argc !=2)
{
perror("useage:<ipaddress>");
exit(1);
}
if((socketfd = socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error!\n");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
{
printf("inet_pton error in%s\n",argv[1]);
exit(1);
}
if(connect(socketfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
perror("error in connect!\n");
exit(1);
}
while(n = recv(socketfd,recline,MAX_LINE,0) >0)
{
recline[n]='\0';
if(fputs(recline,stdout)==EOF)
{
perror("fputs error!\n");
exit(1);
}
}
if(n<0)
{
perror("recv error!\n");
exit(1);
}
exit(0);
}
ping shou.com
得到的ip地址是218.5.79.226
然后我在shell下输入./simple 218.5.79.226
结果是:
error in connect!
: Connection refused
请问大家这是什么问题? |
|