开发平台(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;
}