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

Yxqq378287007 2022-01-15 02:57:18 阅读数:825

chapitre la programmation du seau

UDPFonction de corrélation

#include <sys/socket.h>
//Octets réussis,Échec-1
ssize_t sendto(int sock, void *buff, size_t nbytes, int flags, struct sockaddr *to, socklen_t addrlen);
#include <sys/socket.h>
//Octets réussis,Échec-1
ssize_t recvfrom(int sock, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t addrlen);

UDPCôté serveur Echo

uecho_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;
char message[BUF_SIZE];
int str_len;
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_DGRAM, 0);
if(serv_sock==-1)
error_handling("socket() error");
addr_size = sizeof(struct sockaddr_in);
memset(&serv_addr, 0, 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, addr_size)==-1)
error_handling("bind() error");
while(1) {

str_len = recvfrom(serv_sock, message, BUF_SIZE, 0, (struct sockaddr*)&clnt_addr, &addr_size);
sendto(serv_sock, message, str_len, 0, (struct sockaddr*)&clnt_addr, addr_size);
}
close(serv_sock);
return 0;
}
void error_handling(char *message) {

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

UDPEcho client

uecho_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 message[BUF_SIZE];
int str_len;
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_DGRAM, 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]));
while(1) {

fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n")
break;
sendto(sock, message, strlen(message), 0, (struct sockaddr*)&addr, addr_size);
str_len = recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr*)&addr, &addr_size);
message[str_len] = 0;
printf("Message from server : %s\n", message);
}
close(sock);
return 0;
}
void error_handling(char *message) {

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

Avec des limites de donnéesUDPCôté serveur

bound_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 sock;
char message[BUF_SIZE];
int str_len;
int i;
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);
}
sock = socket(PF_INET, SOCK_DGRAM, 0);
if(sock==-1)
error_handling("socket() error");
addr_size = sizeof(struct sockaddr_in);
memset(&serv_addr, 0, 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(sock, (struct sockaddr*)&serv_addr, addr_size)==-1)
error_handling("bind() error");
for(i=0; i<3; i++) {

sleep(5);
str_len = recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr*)&clnt_addr, &addr_size);
printf("Message %d: %s\n", i+1, message);
}
close(serv_sock);
return 0;
}
void error_handling(char *message) {

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

Avec des limites de donnéesUDPClient

bound_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 message1[] = "Hi!";
char message2[] = "I'm another UDP host!";
char message3[] = "Nice to meet you!";
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_DGRAM, 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]));
sendto(sock, message1, strlen(message1), 0, (struct sockaddr*)&addr, addr_size);
sendto(sock, message2, strlen(message2), 0, (struct sockaddr*)&addr, addr_size);
sendto(sock, message3, strlen(message3), 0, (struct sockaddr*)&addr, addr_size);
close(sock);
return 0;
}
void error_handling(char *message) {

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

Objectifs de l'inscriptionIPEtPortDeUDPSocket

uecho_con_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 message[BUF_SIZE];
int str_len;
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_DGRAM, 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]));
//VersUDP Objectifs d'enregistrement des sockets IPEt informations sur le port
connect(sock, (struct sockaddr*)&addr, addr_size);
while(1) {

fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n")
break;
/* sendto(sock, message, strlen(message), 0, (struct sockaddr*)&addr, addr_size); */
write(sock, message, strlen(message));
/* str_len = recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr*)&addr, &addr_size); */
str_len = read(sock, message, sizeof(message)-1);
message[str_len] = 0;
printf("Message from server : %s\n", message);
}
close(sock);
return 0;
}
void error_handling(char *message) {

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

WindowsMise en œuvre de la plateforme

#include <winsock2.h>
//Octets réussis,ÉchecSOCKET_ERROR
int sendto(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int toLen);
#include <winsock2.h>
//Octets réussis,ÉchecSOCKET_ERROR
int recvfrom(SOCKET s, char* buf, int len, int flags, const struct sockaddr* from, int fromLen);

uecho_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;
char message[BUF_SIZE];
int str_len;
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_DGRAM, 0);
if(serv_sock==INVALID_SOCKET)
error_handling("socket() error");
addr_size = sizeof(SOCKADDR_IN);
memset(&serv_addr, 0, 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, addr_size)==SOCKET_ERROR)
error_handling("bind() error");
while(1) {

str_len = recvfrom(serv_sock, message, BUF_SIZE, 0, (SOCKADDR*)&clnt_addr, &addr_size);
sendto(serv_sock, message, str_len, 0, (SOCKADDR*)&clnt_addr, addr_size);
}
close(serv_sock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

cl /EHsc uecho_server_win.c /Fe:uServerWin.exe /link ws2_32.lib
uServerWin 9190

uecho_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;
char message[BUF_SIZE];
int str_len;
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_DGRAM, 0);
if(sock==INVALID_SOCKET)
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]));
//VersUDP Objectifs d'enregistrement des sockets IPEt informations sur le port
connect(sock, (SOCKADDR*)&addr, addr_size);
while(1) {

fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n")
break;
send(sock, message, strlen(message), 0);
str_len = recv(sock, message, sizeof(message)-1, 0);
message[str_len] = 0;
printf("Message from server : %s\n", message);
}
close(sock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc uecho_client_win.c /Fe:uClientWin.exe /link ws2_32.lib
uClientWin 127.0.0.1 9190
版权声明:本文为[Yxqq378287007]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/01/202201080603388764.html