1、linux c代码实现
复制代码 代码如下:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
typedef enum {false,true}bool;
int main()
{
bool flag;
int sock;
char **pptr = NULL;
struct sockaddr_in destAddr;
struct hostent *ptr = NULL;
char destIP[128];
char szBuffer[] = {"GET /ip2city.asp HTTP/1.1\r\nHost:
if(strlen(szBuffer) != send(sock,szBuffer,strlen(szBuffer),0)){
perror("send error");
exit(0);
}
//接收数据包
if(-1 == recv(sock,res,1024,0)){
perror("recv error");
exit(0);
}
printf("res:\n%s\n",res);
return 0;
}
2、将上述代码保存getip.c,并使用如下命令编译
复制代码 代码如下:
gcc -o getip getip.c
3、运行程序
复制代码 代码如下:
./getip
