《TCP/IP网络编程》第7章

yxqq378287007 2022-01-15 02:48:39 阅读数:741

编程 网络 TCP ip

半关闭shutdown函数

#include <sys/socket.h>
//成功0,失败-1
int shutdown(int sock, int howto);
//SHUT_RD,断开输入流
//SHUT_WR,断开输出流
//SHUT_RDWR,断开输入输出流

半关闭文件传输服务器端

file_server.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 serv_sock;
int clnt_sock;
FILE *fp;
char buf[BUF_SIZE];
int read_cnt;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t addr_size;
if(argc!=2) {

printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock==-1)
error_handling("socket() error");
addr_size = sizeof(struct sockaddr_in);
memset(&serv_addr, 0, serv_addr_size);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*)&serv_addr, serv_addr_size)==-1)
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &addr_size );
if(clnt_sock==-1)
error_handling("accept() error");
fp = fopen("file_serve.c", "rb");
do {

read_cnt = fread((void*)buf, 1, BUF_SIZE, fp);
write(clnt_sock, buf, read_cnt);
} while(read_cnt==BUF_SIZE);
fclose(fp);
//关闭输出流,不再向客户端发送数据
shutdown(clnt_sock, SHUT_WR);
read(clnt_sock, buf, BUF_SIZE);
printf("Message from client: %s\n", buf);
close(clnt_sock);
close(serv_sock);
return 0;
}
void error_handling(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc file_server.c -o fserver
./fserver 9190

半关闭文件传输客户端

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;
char buf[BUF_SIZE];
int read_cnt;
struct sockaddr_in addr;
socklen_t addr_size;
if(argc!=3) {

printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock==-1)
error_handling("socket() error");
addr_size = sizeof(addr);
memset(&addr, 0, addr_size);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr*)&addr, addr_size)==-1)
error_handling("connect() error");
fp = fopen("receive.dat", "wb");
while( (read_cnt = read(sock, buf, BUF_SIZE)) != 0 )
fwrite((void*)buf, 1, read_cnt, fp);
fclose(fp);
puts("Received file data");
write(sock, "Thank you", 10);
close(sock);
return 0;
}
void error_handling(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc file_client.c -o fclient
./fclient 127.0.0.1 9190

Windows实现

#include <winsock2.h>
//成功0,失败SOCKET_ERROR
int shutdown(int sock, int howto);
//SD_RECEIVE,断开输入流
//SD_SEND,断开输出流
//SD_BOTH,断开输入输出流

file_server_win.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF_SIZE 30
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {

WSADATA wsaData;
SOCKET serv_sock;
SOCKET clnt_sock;
FILE *fp;
char buf[BUF_SIZE];
int read_cnt;
SOCKADDR_IN serv_addr;
SOCKADDR_IN clnt_addr;
int addr_size;
if(argc!=2) {

printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock==-1)
error_handling("socket() error");
addr_size = sizeof(SOCKADDR_IN);
memset(&serv_addr, 0, serv_addr_size);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (SOCKADDR*)&serv_addr, serv_addr_size)==SOCKET_ERROR)
error_handling("bind() error");
if(listen(serv_sock, 5)==SOCKET_ERROR)
error_handling("listen() error");
clnt_sock = accept(serv_sock, (SOCKADDR*)&clnt_addr, &addr_size);
if(clnt_sock==INVALID_SOCKET)
error_handling("accept() error");
fp = fopen("file_serve_win.c", "rb");
do {

read_cnt = fread((void*)buf, 1, BUF_SIZE, fp);
send(clnt_sock, buf, read_cnt, 0);
} while(read_cnt==BUF_SIZE);
fclose(fp);
//关闭输出流,不再向客户端发送数据
shutdown(clnt_sock, SD_SEND);
recv(clnt_sock, buf, BUF_SIZE, 0);
printf("Message from client: %s\n", buf);
close(clnt_sock);
close(serv_sock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc file_server_win.c /Fe:fServerWin.exe /link ws2_32.lib
fServerWin 9190

file_client_win.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF_SIZE 30
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {

WSADATA wsaData;
SOCKET sock;
FILE *fp;
char buf[BUF_SIZE];
int read_cnt;
SOCKADDR_IN addr;
int addr_size;
if(argc!=3) {

printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock==-1)
ErrorHanding("socket() error");
addr_size = sizeof(SOCKADDR_IN);
memset(&addr, 0, addr_size);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (SOCKADDR*)&addr, addr_size)==SOCKET_ERROR)
ErrorHanding("connect() error");
fp = fopen("receive.dat", "wb");
while( (read_cnt = recv(sock, buf, BUF_SIZE, 0)) != 0 )
fwrite((void*)buf, 1, read_cnt, fp);
fclose(fp);
puts("Received file data");
send(sock, "Thank you", 10, 0);
closesocket(sock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc file_client_win.c /Fe:fClientWin.exe /link ws2_32.lib
fClientWin 127.0.0.1 9190
版权声明:本文为[yxqq378287007]所创,转载请带上原文链接,感谢。 https://blog.csdn.net/oqqyx1234567/article/details/122357315