电子发烧友App

硬声App

0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示
电子发烧友网>电子资料下载>C语言|源代码>winsock编程实例

winsock编程实例

2007-12-25 | rar | 200 | 次下载 | 免费

资料介绍

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
  //"http://www.54sh.com/index/index.aspx"
  temp_str = strtok(add_str,": /");
  if(strcmp(temp_str,"http") != 0)
   printf("only 'http' be seported!!");
  //so it just like this
  //"http","www.54sh.com","index","index.aspx"
  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("exiting...");
 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开关电源原理及各功能电路详解
  10. 0.38 MB  |  10次下载  |  免费
  11. 6基于AT89C2051/4051单片机编程器的实验
  12. 0.11 MB  |  4次下载  |  免费
  13. 7蓝牙设备在嵌入式领域的广泛应用
  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 专业版下载 (3CD完整版)
  8. 0.00 MB  |  51043次下载  |  免费
  9. 5555集成电路应用800例(新编版)
  10. 0.00 MB  |  33562次下载  |  免费
  11. 6接口电路图大全
  12. 未知  |  30320次下载  |  免费
  13. 7Multisim 10下载Multisim 10 中文版
  14. 0.00 MB  |  28588次下载  |  免费
  15. 8开关电源设计实例指南
  16. 未知  |  21539次下载  |  免费

总榜

  1. 1matlab软件下载入口
  2. 未知  |  935053次下载  |  免费
  3. 2protel99se软件下载(可英文版转中文版)
  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次下载  |  免费