2012-03-13 4 views
1

몇 가지 기본 네트워크 프로그래밍을 시작했습니다.tcp/ip 패킷 수신기

내가 읽기/TcpClientTcpListener를 사용하여 내 자신의 프로그램을 작성하고 잘 근무하고있다.

그러나, 지금 작업하고있는 응용 프로그램은 약간 다르게 작동합니다.

나는 연결하지 않고도 TCP/IP 패킷을 수신하는 프로그램을 설정합니다.

예를 들어

은, 패킷 전송 응용 프로그램 추가 및 포트 번호를 해당 IP를 내 프로그램에 패킷을 보낼 수 있습니다.

는 또한 Sharppcap 및 packet.net를 사용하여 들여다했지만 내가 찾은 모든 예제는 로컬에서만 발견 장치 (예 : 포트 번호와 IP를 추가로 매개 변수를 설정할 수있는 기회를) 수신하지 않습니다.

누구든지이 작업을 수행하는 방법에 대한 제안이 있습니까?

+1

? 문제가 무엇인지는 분명하지 않습니다. 당신은 "연결하지 않고"라고 말하지만, 연결하지 않기를 기대하는 것을 설명하지는 마십시오. 어떻게 든 원격 장치를들을 수 있기를 기대하십니까? – Oded

+0

UdpClient와 UdpListner를 보았습니까? UDP는 연결없는 프로토콜입니다. –

+0

@Oded, 예 내 프로그램에 ip/tcp 패킷을 전송하는 장치가 있습니다. 따라서 tcpclient/server에서와 같이 리스너에 연결되지 않습니다. 나는 Udp를 살펴 봤는데, 그 문제는 신뢰할 수 없다는 것이다. 나는이 패킷들이 내 프로그램에 도착하는지 확인해야하고 udp에는 아무런 답이 없다. – Rick

답변

2

TCP/IP 대신 UDP 프로토콜을 사용해야합니다. 여기

http://en.wikipedia.org/wiki/User_Datagram_Protocol

는 클라이언트 코드입니다 :

using System.Net; 
using System.Net.Sockets; 

... 

/// <summary> 
/// Sends a sepcified number of UDP packets to a host or IP Address. 
/// </summary> 
/// <param name="hostNameOrAddress">The host name or an IP Address to which the UDP packets will be sent.</param> 
/// <param name="destinationPort">The destination port to which the UDP packets will be sent.</param> 
/// <param name="data">The data to send in the UDP packet.</param> 
/// <param name="count">The number of UDP packets to send.</param> 
public static void SendUDPPacket(string hostNameOrAddress, int destinationPort, string data, int count) 
{ 
    // Validate the destination port number 
    if (destinationPort < 1 || destinationPort > 65535) 
     throw new ArgumentOutOfRangeException("destinationPort", "Parameter destinationPort must be between 1 and 65,535."); 

    // Resolve the host name to an IP Address 
    IPAddress[] ipAddresses = Dns.GetHostAddresses(hostNameOrAddress); 
    if (ipAddresses.Length == 0) 
     throw new ArgumentException("Host name or address could not be resolved.", "hostNameOrAddress"); 

    // Use the first IP Address in the list 
    IPAddress destination = ipAddresses[0];    
    IPEndPoint endPoint = new IPEndPoint(destination, destinationPort); 
    byte[] buffer = Encoding.ASCII.GetBytes(data); 

    // Send the packets 
    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);   
    for(int i = 0; i < count; i++) 
     socket.SendTo(buffer, endPoint); 
    socket.Close();    
} 
정확히 당신이 여기에서 해결하고자하는 어떤
+0

질문은 C가 아니라 C#이므로, 코드 예는 OP에 도움이되지 않을 것입니다. – Oded

+0

나는 또한이 프로젝트를 살펴한다 C# @Oded –

+1

에 언어를 변경 : http://www.codeproject.com/Articles/2614/Testing-TCP-and-UDP-socket-servers-using-C- and-NET @rick –