[問題] UDP發送接收

看板C_and_CPP (C/C++)作者 (賽)時間9年前 (2016/11/08 23:15), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Microsoft Visual Studio C++ 問題(Question): 我最一開始有撰寫UDP Server以及UDP Client,UDP開著,client發送封包給server, server接收到後,會發送回給client,所以client可以發送跟接收 但我最近在改寫時就發現server跟client連不到,這次的寫法是 server發送封包給client接收 但就接收不到了,應該說根本沒有連上 但我不太覺得程式碼有錯誤,因為我是參考之前成功的範例去改的 所以不知道是有程式碼以外的問題嗎? 預期的正確結果(Expected Output): server發送的封包client可以接收成功 出現的error都是在接收發送那裏接下來進入的if bind或者初始的設定沒跑出error 程式碼(Code):(請善用置底文網頁, 記得排版) Server端 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <iostream> #include <sys/types.h> using namespace std; #pragma comment(lib,"ws2_32.lib") //Winsock Library #define SERVER_PORT1 8889 #define BUFLEN 1024 int _tmain(int argc, _TCHAR* argv[]){ struct sockaddr_in addrsend; SOCKET s1; int length_addrsend; char buf[BUFLEN]; int sendbol; int receivebol; double cur_ang[3]; WSADATA wsa; printf("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){ printf("Failed. Error Code : %d",WSAGetLastError()); } printf("Initialized.\n"); if ((s1= socket(AF_INET, SOCK_DGRAM, 0)) <0){ perror ("socket s1 failed"); } memset(&addrsend,'\0',sizeof(addrsend)); addrsend.sin_family = AF_INET; addrsend.sin_addr.s_addr = htonl(INADDR_ANY); addrsend.sin_port = htons(SERVER_PORT1); if (bind(s1,(struct sockaddr*)&addrsend,sizeof(addrsend)) <0) { perror ("bind s1 failed\n"); } printf("bind done"); length_addrsend=sizeof(addrsend); printf("Server is ready to receive !!\n"); printf("Can strike Cntrl-c to stop Server >>\n"); memset(buf,'\0', BUFLEN); cur_ang[0]=0.5678; cur_ang[1]=0.51278; cur_ang[2]=0.589678; while(1){ sprintf(buf,"%f %f %f ",cur_ang[0],cur_ang[1],cur_ang[2]); sendbol=sendto(s1,buf,BUFLEN,0,(struct sockaddr*)&addrsend,length_addrsend); if (sendbol<0){ perror("Could not send datagram!!\n"); continue; } memset(buf,'\0',BUFLEN); } closesocket(s1); WSACleanup(); system("pause"); return 0; } Client端 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <winsock2.h> #include <fcntl.h> #include <string.h> #include <iostream> #include <cstdio> #include <cassert> #include <cmath> #include <math.h> #include <time.h> #pragma comment(lib,"ws2_32.lib") using namespace std; #define SERV_PORT 8889 //receive from server #define BUFLEN 1024 #define SERVER "127.0.0.1" int main(int argc, char **argv){ struct sockaddr_in server;//one port struct hostent *hp; /* holds IP address of server */ SOCKET s; int size_server; char data; char buf[BUFLEN]={0}; char buf1[BUFLEN]={0}; int sendbol, receivebol; WSADATA wsa; //Initialise winsock printf("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){ printf("Failed. Error Code : %d",WSAGetLastError()); } printf("Initialized.\n"); //create socket if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR){ printf("socket() failed with error code : %d" , WSAGetLastError()); } server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_addr.s_addr=inet_addr(SERVER); server.sin_port = htons(SERV_PORT); size_server=sizeof(server); while(1){ receivebol=recvfrom(s,buf,BUFLEN,0,(struct sockaddr*)&server,&size_server); if (receivebol< 0) { printf("receive failed: %d\n" , WSAGetLastError()); } else{ printf("bytes have been received\n"); printf("%s\n",buf); } } closesocket(s); WSACleanup(); system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.234.232 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1478618153.A.8E6.html

11/08 23:30, , 1F
會跟程式碼哪個先跑有關係嗎?我是都先跑server
11/08 23:30, 1F

11/09 00:04, , 2F
你可能要先唸一下網路概論了解什麼是client/server
11/09 00:04, 2F

11/09 00:04, , 3F
還有IP的意思
11/09 00:04, 3F
文章代碼(AID): #1O8UmfZc (C_and_CPP)
文章代碼(AID): #1O8UmfZc (C_and_CPP)