题目要求
利用TCP Socket实现PC机与目标板的通讯,目标板作为服务端,服务端按要求给两个客户端传送文件。 作业要求: 1)交电子文档,需要源代码。 2)上机演示
一、软硬件准备
1、硬件准备
Linux开发箱
2、软件准备
Ubuntu VMware 超级终端
二、编译生成可执行文件
1、新建server.c并gcc编译生成fuwuqi
此过程参考上一篇文章,略
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <pthread.h>
#define portnum 12345
#define FILE_SIZE 500
#define BUFFER_SIZE 1024
void *net_thread(void * fd);
int main()
{
int server_fd=socket(AF_INET,SOCK_STREAM,0);
if(-1==server_fd)
{
perror("socket");
exit(1);
}
struct sockaddr_in server_addr;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnum);
(server_addr.sin_addr).s_addr=htonl(INADDR_ANY);
if(-1==bind(server_fd,(struct sockaddr *)&server_addr,sizeof(server_addr)))
{
perror("bind");
exit(6);
}
if(-1==listen(server_fd,5))
{
perror("listen");
exit(7);
}
while(1)
{
struct sockaddr_in client_addr;
int size=sizeof(client_addr);
int new_fd=accept(server_fd,(struct sockaddr *)&client_addr,&size);
if(-1==new_fd)
{
perror("accept");
continue;
}
printf("accept client ip:%s:%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port);
printf("new_fd=%d\n",new_fd);
FILE *file_fp = fopen("01.file_fp", "w+");
if(NULL == file_fp)
{
printf(" open 01.file_fp failure\n" );
exit(1);
}
else
{
int a=new_fd;
fprintf(file_fp,"%d\n",new_fd);
fclose(file_fp);
}
int pthread_id;
int ret = pthread_create((pthread_t *)&pthread_id,NULL,net_thread,(void *)&new_fd);
if(-1==ret)
{
perror("pthread_create");
close(new_fd);
continue;
}
}
close(server_fd);
return 0;
}
void *net_thread(void * fd)
{
pthread_detach(pthread_self());
int new_fd=*((int *)fd);
int file2_fp;
while(1)
{
char buffer[BUFFER_SIZE];
memset( buffer,0, sizeof(buffer) );
if(read(new_fd, buffer, sizeof(buffer)) < 0)
{
perror("Server Recieve Data Failed:");
break;
}
char file_name[FILE_SIZE];
memset( file_name,0, sizeof(file_name) );
strncpy(file_name, buffer, strlen(buffer)>FILE_SIZE?FILE_SIZE:strlen(buffer));
memset( buffer,0, sizeof(buffer) );
printf("%s\n", file_name);
if( strcmp(file_name,"null")==0 )
{
break;
close(new_fd);
}
file2_fp = open(file_name,O_RDONLY,0777);
if(file2_fp<0)
{
printf("File:%s Not Found\n", file_name);
}
else
{
int length = 0;
memset( buffer,0, sizeof(buffer) );
while( (length = read(file2_fp, buffer, sizeof(buffer))) > 0 )
{
if( write(new_fd, buffer, length) < 0)
{
printf("Send File:%s Failed.\n", file_name);
break;
}
memset( buffer,0, sizeof(buffer) );
}
close(file2_fp);
printf("File:%s Transfer Successful!\n", file_name);
}
}
close(new_fd);
}
2.新建client.c并gcc生成kehu
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#define portnum 12345
#define FILE_SIZE 500
#define BUFFER_SIZE 1024
int sendfile(int sockfd);
int main()
{
char name[30]={0};
printf("请输入服务器的主机名或者ip\n");
scanf("%s",name);
struct hostent *h;
h=gethostbyname(name);
if(NULL==h)
{
perror("geyhostbyname");
exit(1);
}
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(-1==sockfd)
{
perror("socket");
exit(2);
}
struct sockaddr_in server_addr;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnum);
server_addr.sin_addr=*((struct in_addr *)h->h_addr_list[0]);
if(-1==connect(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)))
{
perror("connect");
exit(3);
}
while(1)
{
sendfile(sockfd);
}
return 0;
}
int sendfile(int sockfd)
{
int file_fp;
char file_name[FILE_SIZE];
memset( file_name,0, sizeof(file_name) );
printf("Please Input File Name On Server: ");
scanf("%s", file_name);
char buffer[BUFFER_SIZE];
memset( buffer,0, sizeof(buffer) );
strncpy(buffer, file_name, strlen(file_name)>sizeof(buffer)?sizeof(buffer):strlen(file_name));
if(write(sockfd, buffer, sizeof(buffer)) < 0)
{
perror("Send File Name Failed:");
exit(1);
}
if( strcmp(file_name,"null")==0 )
{
exit(1);
close(sockfd);
}
file_fp = open(file_name,O_CREAT|O_RDWR,0777);
if( file_fp<0 )
{
printf("File:\t%s Can Not Open To Write\n", file_name);
exit(1);
}
int length = 0;
memset( buffer,0, sizeof(buffer) );
while((length = read(sockfd, buffer, sizeof(buffer))) > 0)
{
if( write( file_fp, buffer, length ) < length)
{
printf("File:\t%s Write Failed\n", file_name);
break;
}
if(length < sizeof(buffer))
{
break;
}
memset( buffer,0, sizeof(buffer) );
}
printf("Receive File:\t%s From Server IP Successful!\n", file_name);
close(file_fp);
}
运行并传输文件
将fuwuqi传入Linux开发板并执行 将kehu在VMware中执行 执行结果:
|