Chapitre 7 de la programmation du réseau TCP / IP

Yxqq378287007 2022-01-15 02:57:20 阅读数:396

chapitre la programmation du seau

Demi - fermetureshutdownFonctions

#include <sys/socket.h>
//Succès0,Échec-1
int shutdown(int sock, int howto);
//SHUT_RD,Déconnecter le flux d'entrée
//SHUT_WR,Déconnecter le flux de sortie
//SHUT_RDWR,Déconnecter le flux d'entrée / sortie

Fermeture partielle du serveur de transfert de fichiers

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);
//Fermer le flux de sortie,Ne plus envoyer de données au client
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

Semi - fermer le client de transfert de fichiers

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

WindowsRéalisation

#include <winsock2.h>
//Succès0,ÉchecSOCKET_ERROR
int shutdown(int sock, int howto);
//SD_RECEIVE,Déconnecter le flux d'entrée
//SD_SEND,Déconnecter le flux de sortie
//SD_BOTH,Déconnecter le flux d'entrée / sortie

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);
//Fermer le flux de sortie,Ne plus envoyer de données au client
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://javamana.com/2022/01/202201080603388723.html