socket通訊(使用mingw環境下的g++來執行程式碼)
阿新 • • 發佈:2019-01-27
結果先放一下
客戶端程式碼:
#include <stdio.h> #include <winsock2.h> #pragma comment ( lib, "ws2_32.lib" ) int main() { printf("|=============================================|\n"); printf("| |\n"); printf("| 客戶端 |\n"); printf("| |\n"); printf("|=============================================|\n"); int len=1024; char Server_IP[20]={'0'}; char recvBuf[1024]="\0"; char sendBuf[1024]="\0"; char path[100]={'0'}; printf("請輸入服務端IP:"); gets(Server_IP); WORD wVersion = MAKEWORD(2, 0); WSADATA wsData; if (WSAStartup(wVersion, &wsData)!=0) { printf("初始化失敗!\n"); } SOCKET sockCli=socket(AF_INET,SOCK_STREAM,0); sockaddr_in addClient; addClient.sin_family=AF_INET; addClient.sin_addr.S_un.S_addr=inet_addr(Server_IP); addClient.sin_port=htons(8000); if(connect(sockCli,(SOCKADDR*)&addClient,sizeof(SOCKADDR))!=0) printf("連線伺服器失敗!\n"); else { printf("連線成功!\n"); while (1) { //fflush(stdin); recv(sockCli,recvBuf,len,0); if (strcmp(recvBuf,"exit")==0) { break; } else//接收儲存檔案 { printf("接受:%s\n",recvBuf); printf("傳送:"); gets(sendBuf); //小寫轉大寫 int i=0; while(sendBuf[i]!='\0'){ sendBuf[i]=sendBuf[i]-'a'+'A'; i++; } send(sockCli,sendBuf,strlen(sendBuf)+1,0); if (strcmp(sendBuf,"exit")==0) { break; } } } closesocket(sockCli); } return 0; }
服務端程式碼:
#include <stdio.h> #include <winsock2.h> #pragma comment ( lib, "ws2_32.lib" ) int main() { printf("|=============================================|\n"); printf("| |\n"); printf("| |\n"); printf("| 伺服器端 |\n"); printf("| |\n"); printf("| |\n"); printf("|=============================================|\n"); int len=1024; int flag=0; char Local_IP[20]={'0'}; char recvBuf[1024]="\0"; char sendBuf[1024]="\0"; char TempBuf[1024]="\0"; char path[100]={'0'}; printf("請輸入本地IP:"); gets(Local_IP); WORD wVersion = MAKEWORD(2, 0); WSADATA wsData; if (WSAStartup(wVersion, &wsData)!=0) { printf("初始化失敗!\n"); } SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); sockaddr_in addServer,addrClient; addServer.sin_family=AF_INET; addServer.sin_addr.S_un.S_addr=inet_addr(Local_IP); addServer.sin_port=htons(8000); if (bind(sockSrv,(SOCKADDR*)&addServer,sizeof(SOCKADDR))!=0) { printf("埠連線失敗!\n"); } else { loop: listen(sockSrv,5); addrClient.sin_family=AF_INET; addrClient.sin_addr.S_un.S_addr=INADDR_ANY; addrClient.sin_port=htons(8000); printf("等待客戶端連線……\n"); SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); printf("連線成功!\n"); while (1) { printf("傳送:"); fflush(stdin); gets(sendBuf); int i=0; while(sendBuf[i]!='\0'){ sendBuf[i]=sendBuf[i]-'a'+'A'; i++; } if (strcmp(sendBuf,"exit")==0) { send(sockConn,sendBuf,len,0); break; } else { send(sockConn,sendBuf,strlen(sendBuf)+1,0); recv(sockConn,recvBuf,1024,0); if (strcmp(recvBuf,"exit")==0) { printf("客戶端已下線!\n"); goto loop; } printf("接受:%s\n",recvBuf); fflush(stdin); } } closesocket(sockConn); } return 0; }
實驗過程:大部分時間都花在了mingw(minimai gnu for windows)和g++軟體安裝上,不停出錯,不停的百度,還好最後解決了。
最後執行過程客戶端連線不上伺服器,怎麼都找不到原因,問別人也不知道。最後ping了一下本地,發現超時,但是又能上網,百度了一下可能是因為電腦防火牆的原因,關了防火牆之後果然能連線上了,執行結果如上。