当前位置: 技术问答>linux和unix
自己写的第一个客户端程序想连接baidu的服务器,它为何不甩我??
来源: 互联网 发布时间:2017-05-17
本文导语: #include #include #include #include #include #include #include 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("so...
#include
#include
#include
#include
#include
#include
#include
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 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("%sn",buf2);
}
exit(0);
}
终端执行 $./client 115.239.210.27 80
connect success!
然后就一直没反应。。。咋回事??
|
看看http协议
|
度娘要求競價排名的,可能你在排除中
話說不可以連其他的嗎,
話說不可以連其他的嗎,