/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 80
#define BUFFER_SIZE 1024
int main ( int argc , char *argv[ ] )
{
int sockfd , sendbytes ;
char buf[BUFFER_SIZE]="GET / HTTP/1.1\r\nhost: www.baidu.com\r\n\r\n";
struct hostent *host ;
struct sockaddr_in serv_addr ;
/*if ( argc < 2 )
{
fprintf (stderr , "USAGE:./client ip Text \n ") ;
exit ( 1 ) ;
}*/
if ( (sockfd = socket ( AF_INET , SOCK_STREAM ,0 ) ) == -1 )
{
perror ( "socket" ) ;
exit ( 1 ) ;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_port = htons ( PORT ) ;
//serv_addr.sin_addr = * ( ( struct in_addr * ) host->h_addr ) ;
inet_pton(AF_INET, "220.181.111.86", &serv_addr.sin_addr); //把百度ip加进去
bzero ( & ( serv_addr.sin_zero ) , 8 ) ;
if ( connect ( sockfd ,(struct sockaddr * ) &serv_addr ,sizeof(struct sockaddr ) ) == -1 )
{
perror ( "connect " ) ;
exit ( 1 ) ;
}
if ( ( sendbytes = send ( sockfd , buf , strlen(buf), 0 ) ) == -1 )
{
perror ( "send" ) ;
exit ( 1 ) ;
}
else
printf("send ok,send=%d\n",sendbytes);
char bbb[1000]={0};
if ( ( sendbytes = recv ( sockfd , bbb , 5000, 0 ) ) == -1 )
{
perror ( "recv" ) ;
exit ( 1 ) ;
}
printf("recv%d:\n%s\n",sendbytes,bbb);
close ( sockfd ) ;
exit ( 0 ) ;
}
c 模拟 httpget,主要是发送的数据的问题
最新推荐文章于 2023-12-20 15:12:58 发布