C_and_CPP 板


LINE

开发平台(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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1478618153.A.8E6.html
1F:→ s890269: 会跟程式码哪个先跑有关系吗?我是都先跑server 11/08 23:30
2F:推 bibo9901: 你可能要先念一下网路概论了解什麽是client/server 11/09 00:04
3F:→ bibo9901: 还有IP的意思 11/09 00:04







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灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP