自己写的第一个客户端程序想连接baidu的服务器,它为何不甩我??

如此美丽的你 2014-05-05 06:10:43

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int create_TCPClient(const char *serverIP,unsigned short port)
{
int sockfd;
int ret;
struct sockaddr_in client_sock;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket:");
return -1;
}
//自己IP和自己的port内核自动设置
memset(&client_sock,0,sizeof(client_sock));
client_sock.sin_family = AF_INET;
client_sock.sin_port = htons(port);
client_sock.sin_addr.s_addr = inet_addr(serverIP);

ret = connect(sockfd,(struct sockaddr *)&client_sock,sizeof(client_sock));
if(ret == -1){
perror("connect");
return -1;
}
return sockfd;
}

int main(int argc,char *argv[])
{
int sockfd,n;
unsigned short port;
char buf1[20]="GET / HTTP/1.0";
char buf2[100];
if(argc!=3)
{
printf("usage:client <serverIP> <port>\n");
exit(1);
}
port=(unsigned int )atoi(argv[2]);
sockfd=create_TCPClient(argv[1],port);
if(sockfd!=-1) printf("connect success!\n");
else
{
perror("connect:");
exit(1);
}
n=write(sockfd,buf1,20);
if(n==0) {perror("write:");exit(1);}
while(read(sockfd,buf2,100)>0)
{
printf("%s\n",buf2);
}
exit(0);
}


终端执行 $./client 115.239.210.27 80
connect success!
然后就一直没反应。。。咋回事??
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
braveboyny 2014-05-06
  • 打赏
  • 举报
回复
看看http协议
Rocloud 2014-05-05
  • 打赏
  • 举报
回复
度娘要求競價排名的,可能你在排除中 話說不可以連其他的嗎,

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧