AI智能
改变未来

(TCP IP网络编程)实验一 流式套接字编程


一.实验目的

1)熟悉Linux环境下流式套接字编程的基本模型、函数使用细节等,掌握Linux环境下流式套接字编程的具体过程。
2)熟悉Windows环境下流式套接字编程的基本模型、函数使用细节等,掌握Windows环境下流式套接字编程的具体过程。

二.实验内容

(1) 使用流式套接字实现一个Linux环境下的迭代回声服务器端(及客户端);
(2) 使用流式套接字实现一个Windows环境下的迭代回声服务器端(及客户端)。
(3)使服务器端和客户端各传递1次字符串。考虑到使用TCP协议,所以传递字符串前先以4字节整数型方式传递字符串长度。连接时服务器端和客户端数据传输格式如下。


另外,不限制字符串传输顺序及种类,但须进行3次数据交换。
(4)创建收发文件的服务器端/客户端,实现顺序如下。
 客户端接受用户输入的传输文件名。
 客户端请求服务器端传输该文件名所指文件。
 如果指定文件存在,服务器端就将其发送给客户端;反之,则断开连接。

三.实验过程

(1)Linux实现

Server.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>void error_handling(char *message);int main(int argc, char *argv[]){int serv_sock;int clnt_sock;int str_len, i;struct sockaddr_in serv_addr;struct sockaddr_in clnt_addr;socklen_t clnt_addr_size;char message[]=\"Hello World!\";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\");memset(&serv_addr, 0, sizeof(serv_addr));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, sizeof(serv_addr))==-1 )error_handling(\"bind() error\");if(listen(serv_sock, 5)==-1)error_handling(\"listen() error\");clnt_addr_size=sizeof(clnt_addr);for(i=0; i<5; i++){clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);if(clnt_sock==-1)error_handling(\"accept() error\");elseprintf(\"Connected client %d \\n\", i+1);write(clnt_sock, message, sizeof(message));close(clnt_sock);}close(serv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Client.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>void error_handling(char *message);int main(int argc, char* argv[]){int sock;struct sockaddr_in serv_addr;char message[30];int str_len;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\");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!\");str_len=read(sock, message, sizeof(message)-1);if(str_len==-1)error_handling(\"read() error!\");printf(\"Message from server: %s \\n\", message);close(sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:

(2)Windows实现:

Server_win.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA	wsaData;SOCKET hServSock, hClntSock;SOCKADDR_IN servAddr, clntAddr;int strLen, i;int szClntAddr;char message[] = \"Hello World!\";if (argc != 2){printf(\"Usage : %s <port>\\n\", argv[0]);exit(1);}if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)ErrorHandling(\"WSAStartup() error!\");hServSock = socket(PF_INET, SOCK_STREAM, 0);if (hServSock == INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));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, sizeof(servAddr)) == SOCKET_ERROR)ErrorHandling(\"bind() error\");if (listen(hServSock, 5) == SOCKET_ERROR)ErrorHandling(\"listen() error\");szClntAddr = sizeof(clntAddr);for (i = 0; i < 5; i++){hClntSock = accept(hServSock, (SOCKADDR*)&clntAddr, &szClntAddr);if (hClntSock == -1)ErrorHandling(\"accept() error\");elseprintf(\"Connected client %d \\n\", i + 1);send(hClntSock, message, sizeof(message), 0);closesocket(hClntSock);}closesocket(hServSock);WSACleanup();return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Client_win.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA wsaData;SOCKET hSocket;SOCKADDR_IN servAddr;char message[30];int strLen;if (argc != 3){printf(\"Usage : %s <IP> <port>\\n\", argv[0]);exit(1);}if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)ErrorHandling(\"WSAStartup() error!\");hSocket = socket(PF_INET, SOCK_STREAM, 0);if (hSocket == INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));servAddr.sin_family = AF_INET;servAddr.sin_addr.s_addr = inet_addr(argv[1]);servAddr.sin_port = htons(atoi(argv[2]));if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)ErrorHandling(\"connect() error!\");strLen = recv(hSocket, message, sizeof(message) - 1, 0);if (strLen == -1)ErrorHandling(\"read() error!\");printf(\"Message from server: %s \\n\", message);closesocket(hSocket);WSACleanup();return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:

(3)Linux实现

Server.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>void error_handling(char *message);int main(int argc, char *argv[]){int serv_sock;int clnt_sock;int str_len, i;struct sockaddr_in serv_addr;struct sockaddr_in clnt_addr;socklen_t clnt_addr_size;char msg1[]=\"Hello client!\";char msg2[]=\"I\'m server.\";char msg3[]=\"Nice to meet you.\";char* str_arr[]={msg1, msg2, msg3};char read_buf[100];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\");memset(&serv_addr, 0, sizeof(serv_addr));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, 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\");for(i=0; i<3; i++){str_len=strlen(str_arr[i])+1;write(clnt_sock, (char*)(&str_len), 4);write(clnt_sock, str_arr[i], str_len);read(clnt_sock, (char*)(&str_len), 4);read(clnt_sock, read_buf, str_len);puts(read_buf);}close(clnt_sock);close(serv_sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Client.c:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>void error_handling(char *message);int main(int argc, char* argv[]){int sock;struct sockaddr_in serv_addr;char msg1[]=\"Hello server!\";char msg2[]=\"I\'m client.\";char msg3[]=\"Nice to meet you too!\";char* str_arr[]={msg1, msg2, msg3};char read_buf[100];int str_len, i;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\");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!\");for(i=0; i<3; i++){read(sock, (char*)(&str_len), 4);read(sock, read_buf, str_len);puts(read_buf);str_len=strlen(str_arr[i])+1;write(sock, (char*)(&str_len), 4);write(sock, str_arr[i], str_len);}close(sock);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:

(3)Windows实现

Server_win.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA	wsaData;SOCKET hServSock, hClntSock;SOCKADDR_IN servAddr, clntAddr;int szClntAddr;char msg1[]=\"Hello client!\";char msg2[]=\"I\'m server.\";char msg3[]=\"Nice to meet you.\";char* str_arr[]={msg1, msg2, msg3};char read_buf[100];int str_len, i;if(argc!=2){printf(\"Usage : %s <port>\\n\", argv[0]);exit(1);}if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)ErrorHandling(\"WSAStartup() error!\");hServSock=socket(PF_INET, SOCK_STREAM, 0);if(hServSock==INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));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, sizeof(servAddr))==SOCKET_ERROR)ErrorHandling(\"bind() error\");if(listen(hServSock, 5)==SOCKET_ERROR)ErrorHandling(\"listen() error\");szClntAddr=sizeof(clntAddr);hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr,&szClntAddr);if(hClntSock==INVALID_SOCKET)ErrorHandling(\"accept() error\");for(i=0; i<3; i++){str_len=strlen(str_arr[i])+1;send(hClntSock, (char*)(&str_len), 4,0);send(hClntSock, str_arr[i], str_len,0);recv(hClntSock, (char*)(&str_len), 4,0);recv(hClntSock, read_buf, str_len,0);puts(read_buf);}closesocket(hClntSock);closesocket(hServSock);WSACleanup();return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Client_win.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA wsaData;SOCKET hSocket;SOCKADDR_IN servAddr;char msg1[]=\"Hello server!\";char msg2[]=\"I\'m client.\";char msg3[]=\"Nice to meet you too!\";char* str_arr[]={msg1, msg2, msg3};char read_buf[100];int str_len, i;if(argc!=3){printf(\"Usage : %s <IP> <port>\\n\", argv[0]);exit(1);}if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)ErrorHandling(\"WSAStartup() error!\");hSocket=socket(PF_INET, SOCK_STREAM, 0);if(hSocket==INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));servAddr.sin_family=AF_INET;servAddr.sin_addr.s_addr=inet_addr(argv[1]);servAddr.sin_port=htons(atoi(argv[2]));if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr))==SOCKET_ERROR)ErrorHandling(\"connect() error!\");for(i=0; i<3; i++){recv(hSocket, (char*)(&str_len), 4,0);recv(hSocket, read_buf, str_len,0);puts(read_buf);str_len=strlen(str_arr[i])+1;send(hSocket, (char*)(&str_len), 4,0);send(hSocket, str_arr[i], str_len,0);}closesocket(hSocket);WSACleanup();return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:

(4)Linux实现

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 30void error_handling(char *message);int main(int argc, char *argv[]){int serv_sd, clnt_sd;FILE * fp;char buf[BUF_SIZE];char file_name[BUF_SIZE];int read_cnt;struct sockaddr_in serv_adr, clnt_adr;socklen_t clnt_adr_sz;if(argc!=2) {printf(\"Usage: %s <port>\\n\", argv[0]);exit(1);}serv_sd=socket(PF_INET, SOCK_STREAM, 0);memset(&serv_adr, 0, sizeof(serv_adr));serv_adr.sin_family=AF_INET;serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);serv_adr.sin_port=htons(atoi(argv[1]));bind(serv_sd, (struct sockaddr*)&serv_adr, sizeof(serv_adr));listen(serv_sd, 5);clnt_adr_sz=sizeof(clnt_adr);clnt_sd=accept(serv_sd, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);read(clnt_sd, file_name, BUF_SIZE);fp=fopen(file_name, \"rb\");if(fp!=NULL){while(1){read_cnt=fread((void*)buf, 1, BUF_SIZE, fp);if(read_cnt<BUF_SIZE){write(clnt_sd, buf, read_cnt);break;}write(clnt_sd, buf, BUF_SIZE);}}fclose(fp);close(clnt_sd); close(serv_sd);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}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 30void error_handling(char *message);int main(int argc, char *argv[]){int sd;FILE *fp;char buf[BUF_SIZE];char file_name[BUF_SIZE];int read_cnt;struct sockaddr_in serv_adr;if(argc!=3) {printf(\"Usage: %s <IP> <port>\\n\", argv[0]);exit(1);}printf(\"Input file name: \");scanf(\"%s\", file_name);fp=fopen(file_name, \"wb\");sd=socket(PF_INET, SOCK_STREAM, 0);memset(&serv_adr, 0, sizeof(serv_adr));serv_adr.sin_family=AF_INET;serv_adr.sin_addr.s_addr=inet_addr(argv[1]);serv_adr.sin_port=htons(atoi(argv[2]));connect(sd, (struct sockaddr*)&serv_adr, sizeof(serv_adr));write(sd, file_name, strlen(file_name)+1);while((read_cnt=read(sd, buf, BUF_SIZE))!=0)fwrite((void*)buf, 1, read_cnt, fp);printf(\"发送成功!\\n\");fclose(fp);close(sd);return 0;}void error_handling(char *message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:


(4)Windows实现

Server.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>#define BUF_SIZE 30void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA	wsaData;SOCKET hServSock, hClntSock;SOCKADDR_IN servAddr, clntAddr;FILE* fp;char buf[BUF_SIZE];char file_name[BUF_SIZE];int read_cnt;int szClntAddr;if (argc != 2){printf(\"Usage : %s <port>\\n\", argv[0]);exit(1);}if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)ErrorHandling(\"WSAStartup() error!\");hServSock = socket(PF_INET, SOCK_STREAM, 0);if (hServSock == INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));servAddr.sin_family = AF_INET;servAddr.sin_addr.s_addr = htonl(INADDR_ANY);servAddr.sin_port = htons(atoi(argv[1]));bind(hServSock, (SOCKADDR*)&servAddr, sizeof(servAddr));listen(hServSock, 5);szClntAddr = sizeof(clntAddr);hClntSock = accept(hServSock, (SOCKADDR*)&clntAddr, &szClntAddr);if (hClntSock == INVALID_SOCKET)ErrorHandling(\"accept() error\");recv(hClntSock, file_name, BUF_SIZE, 0);fp = fopen(file_name, \"rb\");if (fp != NULL){while (1){read_cnt = fread((void*)buf, 1, BUF_SIZE, fp);if (read_cnt < BUF_SIZE){send(hClntSock, buf, read_cnt, 0);break;}send(hClntSock, buf, BUF_SIZE, 0);}}fclose(fp);closesocket(hClntSock); closesocket(hServSock);return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}Client.c:#include <stdio.h>#include <stdlib.h>#include <winsock2.h>#define BUF_SIZE 30void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA wsaData;SOCKET hSocket;SOCKADDR_IN servAddr;FILE* fp;char buf[BUF_SIZE];char file_name[BUF_SIZE];int read_cnt;int strLen;if (argc != 3){printf(\"Usage : %s <IP> <port>\\n\", argv[0]);exit(1);}printf(\"Input file name: \");scanf(\"%s\", file_name);fp = fopen(file_name, \"wb\");if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)ErrorHandling(\"WSAStartup() error!\");hSocket = socket(PF_INET, SOCK_STREAM, 0);if (hSocket == INVALID_SOCKET)ErrorHandling(\"socket() error\");memset(&servAddr, 0, sizeof(servAddr));servAddr.sin_family = AF_INET;servAddr.sin_addr.s_addr = inet_addr(argv[1]);servAddr.sin_port = htons(atoi(argv[2]));connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr));send(hSocket, file_name, strlen(file_name) + 1,0);while ((read_cnt = recv(hSocket, buf, BUF_SIZE,0)) != 0)fwrite((void*)buf, 1, read_cnt, fp);printf(\"发送成功!\");fclose(fp);closesocket(hSocket);WSACleanup();return 0;}void ErrorHandling(char* message){fputs(message, stderr);fputc(\'\\n\', stderr);exit(1);}

结果:

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » (TCP IP网络编程)实验一 流式套接字编程