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

Yxqq378287007 2022-01-15 02:57:15 阅读数:395

chapitre la programmation du seau

LinuxCôté serveur Echo itératif

echo_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 1024
void error_handling(char *message);
int main(int argc, char *argv[]) {

int serv_sock;
int clnt_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);
}
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, 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");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
for(i=0; i<5; i++) {

clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &addr_size );
if(clnt_sock==-1)
error_handling("accept() error");
else
printf("Connected client %d\n", i+1);
while((str_len=read(clnt_sock, message, BUF_SIZE))!=0)
write(clnt_sock, message, str_len);
close(clnt_sock);
}
close(serv_sock);
return 0;
}
void error_handling(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc echo_server.c -o eserver
./eserver 9190
//Connected client 1
//Connected client 2
//Connected client 3

LinuxClient Echo itératif

echo_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 1024
void error_handling(char *message);
int main(int argc, char *argv[]) {

int sock;
char message[BUF_SIZE];
int str_len;
int recv_len;
int recv_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");
else
puts("Connected......");
while(1) {

fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n")
break;
str_len = write(sock, message, strlen(message));
recv_len = 0;
while(recv_len<str_len) {

recv_cnt = read(sock, &message[recv_len], BUF_SIZE-1);
if(recv_cnt==-1)
error_handling("read() error");
recv_len+=recv_cnt;
}
message[recv_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 echo_client.c -o eclient
./eclient 127.0.0.1 9190

WindowsCôté serveur Echo itératif

echo_server_win.c

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

WSADATA wsaData;
SOCKET hServSock;
SOCKET hClntSock;
char message[BUF_SIZE];
int strLen;
int i;
SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
int szAddr;
if(argc!=2) {

printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
hServSock = socket(PF_INET, SOCK_STREAM, 0);
if(hServSock==INVALID_SOCKET)
ErrorHanding("socket() error");
szAddr = sizeof(SOCKADDR_IN);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(atoi(argv[1]));
if(bind(hServSock, (SOCKADDR *)&servAddr, szAddr)==SOCKET_ERROR)
ErrorHanding("bind() error");
if(listen(hServSock, 5)==SOCKET_ERROR)
ErrorHanding("listen() error");
for(i=0; i<5; i++) {

hClntSock = accept(hServSock, (SOCKADDR *)&clntAddr, &szAddr);
//if(hClntSock==INVALID_SOCKET)
if(hClntSock==-1)
ErrorHanding("accept() error");
else
printf("Connected client %d\n", i+1);
while((strLen=recv(hClntSock, message, BUF_SIZE, 0))!=0)
send(hClntSock, message, strLen, 0);
closesocket(hClntSock);
}
closesocket(hServSock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

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

WindowsClient Echo itératif

echo_client_win.c

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

WSADATA wsaData;
SOCKET hSock;
char message[BUF_SIZE];
int strLen;
int recvLen;
int recvCnt;
SOCKADDR_IN servAddr;
int szAddr;
if(argc!=3) {

printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
hSock = socket(PF_INET, SOCK_STREAM, 0);
if(hSock==INVALID_SOCKET)
ErrorHanding("socket() error");
szAddr = sizeof(SOCKADDR_IN);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(argv[1]);
servAddr.sin_port = htons(atoi(argv[2]));
if(connect(hSock, (SOCKADDR *)&servAddr, szAddr )==SOCKET_ERROR)
ErrorHanding("connect() error");
else
puts("Connected.......");
while(1) {

fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n")
break;
strLen = send(hSock, message, strlen(message), 0);
recvLen = 0;
while(recvLen<strLen) {

recvCnt = recv(hSock, &message[recvLen], BUF_SIZE-1, 0);
if(recvCnt==SOCKET_ERROR)
ErrorHanding("recv() error");
recvLen += recvCnt;
}
message[strLen] = 0;
printf("Message from server: %s", message);
}
closesocket(hSock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

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