📄 lan-test.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define serverip 192.168.10.250
#define port 8000
#define package_size 512
//得到文件的大小//
long filesize(FILE *stream)
{
long curpos,length;
curpos=ftell(stream);
fseek(stream,0l,SEEK_END);
length=ftell(stream);
fseek(stream,curpos,SEEK_SET);
return length;
}
int main(int argc, char *argv[]) //argc为参数的个数,argv[]参数的内容
{
int sockfd;
char *hostip;
struct hostent *host;
struct sockaddr_in server_address;
char buf[package_size];
FILE *stream,*fp;
int sendbytes;
int allsenddata=0;
int readsizes=0;
hostip="192.168.10.250";
host=gethostbyname(hostip);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket error:");
exit(1);
}
server_address.sin_family=AF_INET;
server_address.sin_port=htons(port);
server_address.sin_addr=*((struct in_addr*)host->h_addr);
bzero(&(server_address.sin_zero),8);
stream=fopen("/home/test1.txt","rb");
printf("the file size is:%ld bytes!\n",filesize(stream));
if(stream==NULL)
{
perror("open file error:");
exit(1);
}
fp=fopen("/mnt/test2.txt","wb+");
if(fp==NULL)
perror("construct file error:");
else
{
fputs("hello,it is ok!\n",fp);
fclose(fp);
}
while(!feof(stream))
{
readsizes=fread(buf,1,package_size,stream);
sendbytes=sendto(sockfd,buf,readsizes,0,(struct sockaddr*)&server_address,sizeof(struct sockaddr));
if(sendbytes==-1)
{
perror("send data failed:");
exit(1);
}
allsenddata+=sendbytes;
}
printf("send sucess!\n");
printf("send data size:%d bytes\n",allsenddata);
fclose(stream);
close(sockfd);
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -