套接字通信

#include "../Common/common.h"

int main(int argc, char *argv[])
{
	char FileName[FILENAME_LENGTH] = {0};//定义并初始化清0
	printf("Please input filename you want to send:");
	scanf("%s", FileName);//输入要发送的文件名字
	
	int sockfd, connfd;//定义int型变量 sockfd,connfd
	struct sockaddr_in server, client;//定义2个socket结构体变量
	
	memset(&server, 0, sizeof(server));//清0,也可以用bzero(&server,sozeof(server));
	
	server.sin_family = AF_INET;//协议类型  ipv4协议
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(PORT);
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字,AF_INET协议类型
	if(sockfd < 0)//成功则返回套接字描述符,<0 创建失败
	{
		perror("socket");
		exit(1);
	}
	//绑定
	if(bind(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0)
	{
		perror("bind");
		exit(1);
	}
	//监听,等待连接队列的最大长度为3
	if(listen(sockfd, 3) < 0)
	{
		perror("listen");
		exit(1);
	}
	
	socklen_t length = sizeof(client);
	connfd = accept(sockfd, (struct sockaddr *)&client, &length);//接收客户机的连接请求
	//成功则返回新的套接字文件描述符,失败则返回-1
	//新创建的套接字将用于和客户机通信,而原来的套接字则仍然处于监听状态
	//处于监听状态的服务器在获得客户机的连接请求后,会将其放置在等待队列中,当系统空闲时将接收客户机的连接请求,
    //
	if(connfd < 0)
	{
		perror("accept");
		exit(1);
	}
	
	int count = 0;
	char buf[BUF_SIZE] = {0};
	strncpy(buf, FileName, strlen(FileName));
	//把FileName指向的前strlen(FileName)个字符复制到buf中
	
	count = send(connfd, buf, BUF_SIZE, 0);
//函数 int send( SOCKET s,const char FAR *buf,int len,int flags );	
//向套接字的另一端发送数据,connfd是指发送端的套接字文件描述符,
//buf指要发送的数据的缓冲区
//BUF_SIZE:指明要发送的数据的字节数
//最后一个参数一般设置为0
//注意send函数只是把buf中的数据copy到connfd的发送缓冲区中 即返回;
	if(count < 0)
	{
		perror("send file");
		exit(1);
	}
	
	FILE *fp = NULL;
	fp = fopen(FileName, "rb");//打开要发送的文件
	if(NULL == fp)
	{
		printf("file: %s not found\n", FileName);
	}
	else
	{
		bzero(buf, BUF_SIZE);//清0buf
		int file_block_length = 0;
		while((file_block_length = fread(buf, 1, BUF_SIZE, fp)) > 0)
//fread函数size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;
//buf 用于接收数据的内存地址
//1 表示要读的每个数据项的字节数为1
//BUF_SIZE表示要读这么多个数据项
//fp表示输入流
//返回实际读取的元素个数,如果返回值与BUF_SIZE不相同,则可能文件结尾或发送错误		
		{
			printf("file_block_length = %d\n", file_block_length);
			if(send(connfd, buf, file_block_length, 0) < 0)
			{
				perror("send file block");
				exit(1);
			}
			bzero(buf, BUF_SIZE);
		}
		fclose(fp);
	}
	close(connfd);
	close(sockfd);
	
	return 0;
}


套接字 --server

 

#include "../Common/common.h"

int main(int argc, char *argv[])
{
 char FileName[FILENAME_LENGTH] = {0};//定义并初始化清0
 printf("Please input filename you want to send:");
 scanf("%s", FileName);//输入要发送的文件名字
 
 int sockfd, connfd;//定义int型变量 sockfd,connfd
 struct sockaddr_in server, client;//定义2个socket结构体变量
 
 memset(&server, 0, sizeof(server));//清0,也可以用bzero(&server,sozeof(server));
 
 server.sin_family = AF_INET;//协议类型  ipv4协议
 server.sin_addr.s_addr = htonl(INADDR_ANY);
 server.sin_port = htons(PORT);
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建套接字,AF_INET协议类型
 if(sockfd < 0)
 {
  perror("socket");
  exit(1);
 }
 //绑定
 if(bind(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0)
 {
  perror("bind");
  exit(1);
 }
 //监听,等待连接队列的最大长度为3
 if(listen(sockfd, 3) < 0)
 {
  perror("listen");
  exit(1);
 }
 
 socklen_t length = sizeof(client);
 connfd = accept(sockfd, (struct sockaddr *)&client, &length);
 if(connfd < 0)
 {
  perror("accept");
  exit(1);
 }
 
 int count = 0;
 char buf[BUF_SIZE] = {0};
 strncpy(buf, FileName, strlen(FileName));
 count = send(connfd, buf, BUF_SIZE, 0);
 if(count < 0)
 {
  perror("send file");
  exit(1);
 }
 
 FILE *fp = NULL;
 fp = fopen(FileName, "rb");
 if(NULL == fp)
 {
  printf("file: %s not found\n", FileName);
 }
 else
 {
  bzero(buf, BUF_SIZE);
  int file_block_length = 0;
  while((file_block_length = fread(buf, 1, BUF_SIZE, fp)) > 0)
  {
   printf("file_block_length = %d\n", file_block_length);
   if(send(connfd, buf, file_block_length, 0) < 0)
   {
    perror("send file block");
    exit(1);
   }
   bzero(buf, BUF_SIZE);
  }
  fclose(fp);
 }
 close(connfd);
 close(sockfd);
 
 return 0;
}

 

套接字 --client

#include "../Common/common.h"

int main(int argc, char *argv[])
{
 if(argc != 2)
 {
  printf("Usage:./fileclient <IP_Address>\n");
  exit(1);
 }
 
 int clientfd;
 struct sockaddr_in server, client;
 memset(&client, 0, sizeof(client));
 
 client.sin_family = AF_INET;
 client.sin_addr.s_addr = htonl(INADDR_ANY);
 client.sin_port = htons(0);
 
 clientfd = socket(AF_INET, SOCK_STREAM, 0);
 if(clientfd < 0)
 {
  perror("scoket");
  exit(1);
 }
 
 if(bind(clientfd, (struct sockaddr *)&client, sizeof(client)) < 0)
 {
  perror("bind");
  exit(1);
 }
 
 memset(&server, 0, sizeof(server));
 
 server.sin_family = AF_INET;
 server.sin_port = htons(PORT);
 if(inet_aton(argv[1], &server.sin_addr) == 0)
 {
  perror("inet_aton");
  exit(1);
 }
 
 socklen_t serverlen = sizeof(server);
 if(connect(clientfd, (struct sockaddr *)&server, serverlen) < 0)
 {
  perror("connect");
  exit(1);
 }
 
 char buf[BUF_SIZE] = {0};
 char FileName[FILENAME_LENGTH] = {0};
 int count = 0;
 
 count = recv(clientfd, buf, BUF_SIZE, 0);
 if(count < 0)
 {
  perror("recv");
  exit(1);
 }
 strncpy(FileName, buf, strlen(buf));
 printf("recv FileName : %s\n", FileName);
 
 FILE *fp = fopen(FileName, "wb+");
 if(NULL == fp)
 {
  perror("fopen");
  exit(1);
 }
 
 int length = 0;
 while(length = recv(clientfd, buf, BUF_SIZE, 0))
 {
  if(length < 0)
  {
   perror("recv");
   exit(1);
  }
  int writelen = fwrite(buf, 1, length, fp);
  if(writelen < length)
  {
   perror("fwrite");
   exit(1);
  }
  bzero(buf, BUF_SIZE);
 }
 printf("recv file %s from %s finished\n", FileName, argv[1]);
 
 fclose(fp);
 close(clientfd);
 
 return 0;
}

 

 

server.c

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值