#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int aiptoi(char * pszip,unsigned int* piip)
{
char psziphere[17],*psztmp1,*psztmp2,*pchar;
int i;
bzero(psziphere,17);
strcpy(psziphere,pszip);
strcat(psziphere,".");
for(i=0,psztmp1=psziphere,pchar=(char *)piip;i<4;i++)
{
if((psztmp2=strstr(psztmp1,"."))==NULL)
return 0;
psztmp2[0]=0;
*(pchar+i)=atoi(psztmp1);
psztmp1=psztmp2+1;
}
return 1;
}
int main(int argc,char **argv)
{
int sockfd;
unsigned int uiip;
char szsendbuf[1024];
char head[8];
int * phead=head+4,nsize=1024,allsize=0;
struct sockaddr_in servaddr;
FILE* fp;
sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = 8888;
if(!aiptoi(argv[1],&uiip)||argc<=1)
{
printf("the ip is not correct or lose the ip input!\n");
return 0;
}
servaddr.sin_addr.s_addr=uiip;
if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr)))
{
printf("Can't connect to the server!\n");
return 0;
}
if((fp = fopen("test.bmp","r"))==NULL)
{
printf("Can't find the test.bmp file");
return 0;
}
sprintf(head,"hhcn");
while(nsize == 1024)
{
bzero(szsendbuf,1024);
nsize = *phead = fread(szsendbuf,1,1024,fp);
write(sockfd,head,8);
nsize =write(sockfd,szsendbuf,nsize);
allsize+=nsize;
if(!(allsize&0x3fff))
printf("now size :%d this time %d times :%d\n",allsize,nsize,allsize/1024);
if(nsize <= 0)
{
printf("Can't send data!\n");
return 0;
}
}
fclose(fp);
return 0;
}