C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) Win7 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) Microsoft Visual Studio C++ 问题(Question): 我之前有写过UDP Server以及UDP Client的函式,当时是把接收跟发送 都放在同一个Client里,共两个C++档案 现在我想将Client里的发送跟接收分开来,加上Server,总共会有三个C++档案 现在的情况是,Server可以连到Client的发送端,但却连不到Client的接收端 预期的正确结果(Expected Output): Server可以和两个Client都连接上并传输封包 错误结果(Wrong Output): Server只跟Client发送端连接上,没跟Client接收端连接上 程式码(Code):(请善用置底文网页, 记得排版) 大致简单讲一下程式码,再附上详细内容 Server.Cpp: 建了两个socket s s1,并且有两个PORT,一个为8888,一个为8889 8888和和发送端连,8889和接收端连,两个socket都有绑定,所以不太知道问题点在哪里 是我的接收端不能一开始就等待接收?但这样好像又有点怪 附上程式码: Server端 ///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_PORT 8888////给发送端 #define SERVER_PORT1 8889///给接收端 #define BUFLEN 1024 int _tmain(int argc, _TCHAR* argv[]){ struct sockaddr_in addr , si_other1; //address of this service *///for one port struct sockaddr_in addrsend , si_other2; SOCKET s; SOCKET s1; int length_addr; int length_addrsend; char buf[BUFLEN]; int sendbol; int receivebol; WSADATA wsa; printf("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){ printf("Failed. Error Code : %d",WSAGetLastError()); //exit(EXIT_FAILURE); } printf("Initialized.\n"); if ((s = socket(AF_INET, SOCK_DGRAM, 0)) <0){ perror ("socket failed"); //exit(EXIT_FAILURE); } if ((s1= socket(AF_INET, SOCK_DGRAM, 0)) <0){ perror ("socket failed"); //exit(EXIT_FAILURE); } memset(&addr,'\0',sizeof(addr)); memset(&addrsend,'\0',sizeof(addrsend)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(SERVER_PORT); addrsend.sin_family = AF_INET; addrsend.sin_addr.s_addr = htonl(INADDR_ANY); addrsend.sin_port = htons(SERVER_PORT1); if (bind(s,(struct sockaddr*)&addr,sizeof(addr)) <0) { perror ("bind failed\n"); } if (bind(s1,(struct sockaddr*)&addrsend,sizeof(addrsend)) <0) { perror ("bind failed\n"); } printf("bind done"); length_addr=sizeof(addr); length_addrsend=sizeof(addrsend); printf("Server is ready to receive !!\n"); printf("Can strike Cntrl-c to stop Server >>\n"); memset(buf,'\0', BUFLEN); while(1){ receivebol=recvfrom(s,buf,BUFLEN,0,(struct sockaddr*)&addr ,&length_addr); if (receivebol<0){ perror ("could not read datagram!!"); continue; } printf("Received data form %s : %d\n",inet_ntoa(addr.sin_addr), htons(addr.sin_port)); printf("%s\n",buf); sendbol=sendto(s1,buf,BUFLEN,0, (struct sockaddr*)&addrsend, length_addrsend); if (sendbol<0){ perror("Could not send datagram!!\n"); continue; } printf("Can Strike Crtl-c to stop Server >>\n"); } memset(buf,'\0',BUFLEN); closesocket(s); closesocket(s1); WSACleanup(); system("pause"); return 0; } Client发送端: ///client/send//// #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 8888 //send to 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}; double cur_ang[3]; int sendbol, receivebol; WSADATA wsa; //Initialise winsock printf("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){ printf("Failed. Error Code : %d",WSAGetLastError()); //exit(EXIT_FAILURE); } printf("Initialized.\n"); //create socket if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR){ printf("socket() failed with error code : %d" , WSAGetLastError()); //exit(EXIT_FAILURE); } 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); 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]); printf("%s\n",buf); sendbol=sendto(s,buf,BUFLEN,0,(struct sockaddr*)&server,size_server); if(sendbol<0) { perror("sned to server error !"); //Sleep(2000); //exit(1); } else{ printf("bytes have been sended\n"); } memset(buf,'\0',sizeof(buf)); } closesocket(s); WSACleanup(); system("pause"); return 0; } Client接收端: ///client//receive/// #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()); //exit(EXIT_FAILURE); } printf("Initialized.\n"); //create socket if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR){ printf("socket() failed with error code : %d" , WSAGetLastError()); //exit(EXIT_FAILURE); } 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); } memset(buf,'\0',sizeof(buf)); } closesocket(s); WSACleanup(); system("pause"); return 0; } 补充说明(Supplement): --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 39.10.38.124
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1478114724.A.010.html
1F:推 asilzheng: s1根本addrsend是要送往哪里啊 11/03 05:40
2F:→ asilzheng: 漏字,根本不知道 11/03 05:41
3F:推 noodleT: goo.gl/RhTbvN 11/04 07:45
4F:→ noodleT: https://goo.gl/RhTbvN 11/04 07:47







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP