2012-05-19 2 views
1

방금 ​​Poco 라이브러리를 사용하기 시작했습니다. Poco의 DatagramSocket 객체를 사용하여 두 대의 컴퓨터가 통신하는 데 문제가 있습니다. 특히, receiveBytes 함수는 Wireshark를 실행하고 목적지의 컴퓨터에 도착한 UDP 패킷을 보내고 있음에도 불구하고 반환하지 않는 것처럼 보입니다. 나는 내가 간단한 것을 생략하고 있다고 가정하고 이것은 내 모든 부분에서 바보 같은 실수로 인한 것이다. Visual Studio Express 2010을 사용하여 Windows 7에서 Poco 1.4.3p1을 컴파일했습니다. 다음은 Poco를 사용하는 방법을 보여주는 코드 단편입니다. 모든 조언을 주시면 감사하겠습니다.Poco C++ 1.4.3p1의 DatagramSocket ReceiveBytes()는 결코 반환하지 않습니다. 나는 그 기능을 잘못 사용하고 있는가?

(나는 문제가 있어요 곳)

#include "Poco\Net\DatagramSocket.h" #include "Poco\Net\SocketAddress.h" #include "Serializer.h" #include <iostream> int main() { Poco::Net::SocketAddress remoteAddr("192.168.1.116", 5678); //The IP address of the remote (sending) machine Poco::Net::DatagramSocket mSock; //We make our socket (its not connected currently) mSock.connect(remoteAddr); //Sends/Receives are restricted to the inputted IPAddress and port //Now lets try to get some datas std::cout << "Waiting for float" << std::endl; unsigned char float_bytes[4]; mSock.receiveBytes((void*)float_bytes, 4); //The code is stuck here waiting for a packet. It never returns... //Finally, lets convert it to a float and print to the screen float net_float; BinToFloat(float_bytes, &net_float); //Converting the binary data to a float and storing it in net_float std::cout << net_float << std::endl; system("PAUSE"); return 0; } 

이 시간 내 주셔서 감사 받기

#include "Poco\Net\DatagramSocket.h" 
#include "Serializer.h" //A library used for serializing data 

int main() 
{ 
    Poco::Net::SocketAddress remoteAddr("192.168.1.140", 5678); //The IP address of the remote (receiving) machine 
    Poco::Net::DatagramSocket mSock; //We make our socket (its not connected currently) 
    mSock.connect(remoteAddr); //Sends/Receives are restricted to the inputted IPAddress and port 
    unsigned char float_bytes[4]; 
    FloatToBin(1234.5678, float_bytes); //Serializing the float and storing it in float_bytes 
    mSock.sendBytes((void*)float_bytes, 4); //Bytes AWAY! 
    return 0; 
} 

보내기.

+0

왜 지구에서 'C'에 태그를 지정 했습니까? – Puppy

+0

그건 내 실수 였어. 당신이 맞습니다, 제 질문은 C와 아무 관련이 없습니다. 정정에 감사드립니다. – user1405648

답변

2

POCO 소켓은 버클리 소켓에서 모델링됩니다. Berkeley 소켓 API에 대한 기본 자습서를 읽어야합니다. 그러면 POCO OOP 소켓 추상화를 더 쉽게 이해할 수 있습니다.

클라이언트와 서버 모두에서 연결할 수 없습니다(). 클라이언트에서만 connect()를 사용합니다. UDP의 경우 connect()는 선택 사항이며 건너 뛸 수 있습니다. 그런 다음 SendBytes() 대신 sendTo()를 사용해야합니다.

서버에서 와일드 카드 IP 주소 (호스트의 사용 가능한 모든 네트워크 인터페이스에서 수신함) 또는 특정 IP 주소에서 bind()를 수행하면 다음과 같은 의미를 갖습니다. IP 주소).

수신자/서버 코드를 보면 원격 클라이언트의 주소를 필터링하려는 것 같습니다. 당신은 connect()로 할 수 없다. 당신은 receiveFrom (buffer, length, address)로 읽은 다음 "address"에서 자신을 필터링해야한다.

보안상의 이유로, 수신하는 UDP 패킷의 원본 주소로 가정 할 때주의하십시오. UDP 패킷 스푸핑은 간단합니다. 다른 방법으로 말하자면, IP 주소 (또는 적절한 암호화로 보안되지 않은 모든 것)를 기반으로 인증 또는 승인 결정을 내리지 마십시오.

POCO 프레젠테이션 http://pocoproject.org/slides/200-Network.pdf은 POCO를 사용하여 네트워크 프로그래밍을 수행하는 방법을 코드 스 니펫과 함께 설명합니다. DatagramSocket에 대해서는 슬라이드 15, 16을 참조하십시오. 슬라이드 15에는 오타가 있습니다. msg.data(), msg.size()를 syslogMsg.data(), syslogMsg.size()로 바꿔서 다음을 컴파일하십시오.

" poco/net/samples "디렉토리에서 POCO를 사용하는 모범 사례를 보여주는 간단한 예제를 제공합니다.

+0

예. 나는 이것을 더 빨리보아야했다. 다른 버클리 소켓 함수가 네트워크 소켓을 사용하는 과정에서 어떻게 작용하는지 훨씬 더 명확하게 이해했습니다. 당신의 도움을 주셔서 감사합니다. 관심이있는 사람들은 양쪽 끝에 소켓을 만들고 원하는 포트와 주소를 바인딩했습니다. Poco :: Net :: IPAddress() (포트에 특정 IP 주소의 패킷을 받아들이라고 알려줍니다.) 포트). 그런 다음 SendTo 및 ReceiveFrom을 사용하여 통신합니다. – user1405648