電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>C語言|源代碼>winsock編程實例

winsock編程實例

2007-12-25 | rar | 200 | 次下載 | 10積分

資料介紹

winsock編程實例

#include
#include
#include
#include
#include

void get_file(char *host_ip,char *file);
void get_ip(char *host_name,char *ip);
int main(int argc,char ** argv)
{
?char add_str[256];
?char *temp_str;
?char host_ip[16];
?char file_str[256]={0};
?int len=0;
?while(1)
?{
??memset(add_str,0,256);
??memset(host_ip,0,16);
??memset(file_str,0,256);
??printf("web address:");
??scanf("%s",add_str);
??if(strcmp(add_str,"exit") ==0 )break;
??//the style of web address like this
??//"??temp_str = strtok(add_str,": /");
??if(strcmp(temp_str,"http") != 0)
???printf("only 'http' be seported!!");
??//so it just like this
??//"http","
??temp_str = strtok(NULL,": /");
??get_ip(temp_str,host_ip);

??temp_str = strtok(NULL,": /");
??while(temp_str != NULL)
??{
???strcpy(file_str+len,temp_str);
???len += strlen(temp_str);
???file_str[len++] = '/';
???temp_str = strtok(NULL,": /");
??}
??file_str[len-1] = 0;
??get_file(host_ip,file_str);
?}
?printf("exi
ting...");
?return 0;
}

void get_file(char *host_ip,char *file)
{

?FILE *pfile;

?WSADATA ws;
?SOCKET sock;
?int result;
?struct sockaddr_in addr;
?char order[256];
?char buf[100];

?WSAStartup(0x0101,&ws);
?sock = socket(AF_INET,SOCK_STREAM,0);

?addr.sin_family = AF_INET;
?addr.sin_port = htons(80);
?addr.sin_addr.s_addr = inet_addr(host_ip);

?result = connect(sock,(struct sockaddr *)&addr,sizeof(addr));
?printf("\nconnecting to host %s ...\n",host_ip);
?if(SOCKET_ERROR == result)
?{
??WSACleanup();
??printf("\n can not connet to host %s\n",host_ip);
?}
?else
?{
??printf("\nconnect ok,start to get file...\n");
??strcpy(order,"GET /");
??strcpy(order+5,file);
??int len = strlen(order);
??strcpy(order + len ," \r\n");

//??if(strcmp(file,"") == 0)
???strcpy(file,"index.htm");
??pfile = fopen(file,"w");
??result = send(sock,order,strlen(order),0);
??do
??{
???result = recv(sock,buf,100,0);
???buf[result] = 0;
???fwrite(buf,1,strlen(buf),pfile);
???printf(buf);
??}while(result != 0);?
??fclose(pfile);
?}
?WSACleanup();
}
void get_ip(char *host_name,char *ip)
{
?WSADATA ws;
?WSAStartup(0x0101,&ws);
?hostent *phent;
?phent = gethostbyname(host_name);
?strcpy(ip,inet_ntoa(*(in_addr *)(phent->h_addr_list[0])));
?WSACleanup();
}

下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1電子電路原理第七版PDF電子教材免費下載
  2. 0.00 MB  |  1490次下載  |  免費
  3. 2單片機典型實例介紹
  4. 18.19 MB  |  92次下載  |  1 積分
  5. 3S7-200PLC編程實例詳細資料
  6. 1.17 MB  |  27次下載  |  1 積分
  7. 4筆記本電腦主板的元件識別和講解說明
  8. 4.28 MB  |  18次下載  |  4 積分
  9. 5開關(guān)電源原理及各功能電路詳解
  10. 0.38 MB  |  10次下載  |  免費
  11. 6基于AT89C2051/4051單片機編程器的實驗
  12. 0.11 MB  |  4次下載  |  免費
  13. 7藍牙設(shè)備在嵌入式領(lǐng)域的廣泛應(yīng)用
  14. 0.63 MB  |  3次下載  |  免費
  15. 89天練會電子電路識圖
  16. 5.91 MB  |  3次下載  |  免費

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234313次下載  |  免費
  3. 2PADS 9.0 2009最新版 -下載
  4. 0.00 MB  |  66304次下載  |  免費
  5. 3protel99下載protel99軟件下載(中文版)
  6. 0.00 MB  |  51209次下載  |  免費
  7. 4LabView 8.0 專業(yè)版下載 (3CD完整版)
  8. 0.00 MB  |  51043次下載  |  免費
  9. 5555集成電路應(yīng)用800例(新編版)
  10. 0.00 MB  |  33562次下載  |  免費
  11. 6接口電路圖大全
  12. 未知  |  30320次下載  |  免費
  13. 7Multisim 10下載Multisim 10 中文版
  14. 0.00 MB  |  28588次下載  |  免費
  15. 8開關(guān)電源設(shè)計實例指南
  16. 未知  |  21539次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935053次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537791次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420026次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234313次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233045次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191183次下載  |  免費
  13. 7十天學會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183277次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138039次下載  |  免費