#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "Datatype.h"
#include "fcntl.h"
#include <iconv.h>
/*net header*/
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*net header*/
#include "netdb.h"
int Client(char * IpString,int Port,char * host,char *getfile,char *Header,char *urlParam);
int Server(int Port,int (*func)(int x,struct sockaddr_in sockaddrs));
int delprocess(int x,struct sockaddr_in sockaddrs)
{
int ret =0;
char buff[100]={0};
while(1)
{
ret = read(x,buff,sizeof(buff));
if(ret <= 0)
{
printf("A client is exit IP=%s:%d\n",
inet_ntoa(sockaddrs.sin_addr), ntohs(sockaddrs.sin_port));
return ERROR;
}
}
}
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,
char *outbuf, size_t outlen) {
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
if (cd == 0)
return -1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
return -1;
iconv_close(cd);
*pout = '\0';
return 0;
}
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
int main(int avgc,char *avgv[])
{
Get("http://apis.baidu.com/apistore/iplookupservice/iplookup","4ba22baecaffaa517b2efdeacca5ad18","ip=117.89.35.58");
}
/*创建一个客户端,并链接到服务器,发送请求字段,接受字段返回信息33*/
int Client(char * IpString,int Port,char * host,char *getfile,char *Header,char *urlParam)
{
char *GetMessage = (char *)malloc(4096);
char *ResultMessage = (char *)malloc(4096);
char *buff = (char *)malloc(4096);
int socketFD = 0;
int i =0;
struct sockaddr_in server_addr = {0};
int ret =0;
int headlen =0;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(Port);
server_addr.sin_addr.s_addr = inet_addr(IpString != NULL?IpString:"127.0.0.1");
socketFD = socket(PF_INET, SOCK_STREAM, 0);
if(socketFD == ERROR)
{
printf("socket create error\n");
return ERROR;
}
if(connect(socketFD, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) == -1)
{
printf("connect error\n");
return ERROR;
}
printf("socket connect ok\n");
memset( GetMessage, 0, 4096);
memset( ResultMessage, 0, 4096);
memset( buff, 0, 4096);
sprintf( GetMessage,
"GET %s?%s HTTP/1.1\r\n\
Host: %s\r\n\
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0)\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
Accept-Encoding: gzip, deflate\r\n\
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \r\n\
Apikey: %s \r\n\
Connection: close \r\n\
\r\n\
%s\r\n",
getfile,urlParam,host,Header,urlParam
);
printf("send:%s\n",GetMessage);
ret = write( socketFD, GetMessage, strlen( GetMessage));
if(ret < 0 || ret != strlen( GetMessage))
{
printf("send message error\n");
return -1;
}
ret = read( socketFD, ResultMessage,4096);
if(ret < 0)
{
printf("read message error\n");
return -1;
}
headlen = strlen(ResultMessage);
printf("read:%d\n",ret);
printf("headlen:%d\n",headlen);
printf("READ:%s\n",ResultMessage);
return 0;
}
int Get(char *url,char *Header,char *urlParam)
{
struct hostent *host;
struct in_addr addr;
char newurl[128] ={0};
char getfile[128] ={0};
char *str1 = NULL;
char *str2 = NULL;
/*截取有效字段*/
str1 = strstr(url,"//");
if(str1 != NULL)
strcpy( newurl, str1+2);
else
strcpy( newurl, url);
str1 = strstr( newurl, "com");
if(str1 == NULL)
{
printf("newurl no .com\n");
return -1;
}
newurl[(str1 - newurl)+3] = '\0';
/*截取有效字段*/
printf("%s\n",newurl);
//获取主机信息
host = gethostbyname( newurl ); /*通过域名获取服务器的IP地址*/
if( host == NULL )
{
printf("gethostbyname error\n");
return -1;
}
/*获取求求的文件*/
str1 = strstr(url,"com");
if(str1 == NULL)
{
str1 = strstr(url,"COM");
if(str1 != NULL)
{
printf("url error\n");
return -1;
}
else
{
printf("url error\n");
return -1;
}
}
strcpy(getfile,str1+3);
addr.s_addr = *(unsigned long * )host->h_addr;
printf("hostname:%s\n",host->h_name);
printf("hostip:%s\n",inet_ntoa(addr));
Client(inet_ntoa(addr),80,newurl,getfile,Header,urlParam);
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "Datatype.h"
#include "fcntl.h"
#include <iconv.h>
/*net header*/
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*net header*/
#include "netdb.h"
int Client(char * IpString,int Port,char * host,char *getfile,char *Header,char *urlParam);
int Server(int Port,int (*func)(int x,struct sockaddr_in sockaddrs));
int delprocess(int x,struct sockaddr_in sockaddrs)
{
int ret =0;
char buff[100]={0};
while(1)
{
ret = read(x,buff,sizeof(buff));
if(ret <= 0)
{
printf("A client is exit IP=%s:%d\n",
inet_ntoa(sockaddrs.sin_addr), ntohs(sockaddrs.sin_port));
return ERROR;
}
}
}
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,
char *outbuf, size_t outlen) {
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
if (cd == 0)
return -1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
return -1;
iconv_close(cd);
*pout = '\0';
return 0;
}
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
int main(int avgc,char *avgv[])
{
Get("http://apis.baidu.com/apistore/iplookupservice/iplookup","4ba22baecaffaa517b2efdeacca5ad18","ip=117.89.35.58");
}
/*创建一个客户端,并链接到服务器,发送请求字段,接受字段返回信息33*/
int Client(char * IpString,int Port,char * host,char *getfile,char *Header,char *urlParam)
{
char *GetMessage = (char *)malloc(4096);
char *ResultMessage = (char *)malloc(4096);
char *buff = (char *)malloc(4096);
int socketFD = 0;
int i =0;
struct sockaddr_in server_addr = {0};
int ret =0;
int headlen =0;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(Port);
server_addr.sin_addr.s_addr = inet_addr(IpString != NULL?IpString:"127.0.0.1");
socketFD = socket(PF_INET, SOCK_STREAM, 0);
if(socketFD == ERROR)
{
printf("socket create error\n");
return ERROR;
}
if(connect(socketFD, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) == -1)
{
printf("connect error\n");
return ERROR;
}
printf("socket connect ok\n");
memset( GetMessage, 0, 4096);
memset( ResultMessage, 0, 4096);
memset( buff, 0, 4096);
sprintf( GetMessage,
"GET %s?%s HTTP/1.1\r\n\
Host: %s\r\n\
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0)\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
Accept-Encoding: gzip, deflate\r\n\
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \r\n\
Apikey: %s \r\n\
Connection: close \r\n\
\r\n\
%s\r\n",
getfile,urlParam,host,Header,urlParam
);
printf("send:%s\n",GetMessage);
ret = write( socketFD, GetMessage, strlen( GetMessage));
if(ret < 0 || ret != strlen( GetMessage))
{
printf("send message error\n");
return -1;
}
ret = read( socketFD, ResultMessage,4096);
if(ret < 0)
{
printf("read message error\n");
return -1;
}
headlen = strlen(ResultMessage);
printf("read:%d\n",ret);
printf("headlen:%d\n",headlen);
printf("READ:%s\n",ResultMessage);
return 0;
}
int Get(char *url,char *Header,char *urlParam)
{
struct hostent *host;
struct in_addr addr;
char newurl[128] ={0};
char getfile[128] ={0};
char *str1 = NULL;
char *str2 = NULL;
/*截取有效字段*/
str1 = strstr(url,"//");
if(str1 != NULL)
strcpy( newurl, str1+2);
else
strcpy( newurl, url);
str1 = strstr( newurl, "com");
if(str1 == NULL)
{
printf("newurl no .com\n");
return -1;
}
newurl[(str1 - newurl)+3] = '\0';
/*截取有效字段*/
printf("%s\n",newurl);
//获取主机信息
host = gethostbyname( newurl ); /*通过域名获取服务器的IP地址*/
if( host == NULL )
{
printf("gethostbyname error\n");
return -1;
}
/*获取求求的文件*/
str1 = strstr(url,"com");
if(str1 == NULL)
{
str1 = strstr(url,"COM");
if(str1 != NULL)
{
printf("url error\n");
return -1;
}
else
{
printf("url error\n");
return -1;
}
}
strcpy(getfile,str1+3);
addr.s_addr = *(unsigned long * )host->h_addr;
printf("hostname:%s\n",host->h_name);
printf("hostip:%s\n",inet_ntoa(addr));
Client(inet_ntoa(addr),80,newurl,getfile,Header,urlParam);
return 0;
}