TCP/IP网络编程——基于TCP的半关闭(完成文件传输)
程序员文章站
2022-06-30 18:21:28
...
半关闭:只断开一个流
file_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in serv_addr;
FILE *fp;
int read_cnt;
char buf[BUF_SIZE];
if(argc!=3)
{
exit(1);
}
fp=fopen("receive.dat", "wb");
sock=socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket error!");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1)
error_handling("connect error");
else
puts("Connected.....");
//从服务器接受数据并写入文件
while((read_cnt=read(sock, buf, BUF_SIZE))!=0)
fwrite((void*)buf, 1, read_cnt, fp);
//接收完毕后返回thank you
puts("receive file data:");
write(sock, "thank you", 10);
fclose(fp);
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
file_server.c
//利用TCP发送文件
//shutdown()半关闭的实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
int read_cnt;
FILE *fp;
char buf[BUF_SIZE];
if(argc!=2)
{
exit(1);
}
fp=fopen("file_server.c", "rb");
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket error!");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; //IPV4协议族
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //主机字节序(host)转换成网络字节序(net)(大端序)
serv_addr.sin_port = htons(atoi(argv[1])); //端口号
if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind error");
if(listen(serv_sock, 5) == -1)
error_handling("listen error");
clnt_addr_size = sizeof(clnt_addr);
clnt_sock=accept(serv_sock, (struct sockaddr*) &clnt_addr, &clnt_addr_size);
if(clnt_sock == -1)
error_handling("accept error");
while(1)
{
//循环发送,每次发BUF_SIZE大小的数据,直到发完才会跳出循环
read_cnt=fread((void*)buf, 1, BUF_SIZE, fp);
if(read_cnt<BUF_SIZE)
{
write(clnt_sock, buf, read_cnt);
break;
}
write(clnt_sock, buf, BUF_SIZE);
}
//半关闭:关闭输出流
shutdown(clnt_sock, SHUT_WR);
read(clnt_sock, buf, BUF_SIZE);
printf("message from client: %s \n", buf);
fclose(fp);
close(clnt_sock);
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
上一篇: 18.5 TCP的半关闭
下一篇: TCP/IP的协议