1

I've started to do some basic network programming.

I have read/written my own programs using TcpClient and TcpListener and that has worked fine.

However, the application I am working on now works a bit differently.

I want to set up a program that listens for tcp/ip packets without having to connect.

For example, have a packet sending app send a packet to my program with the appropriate ip add and port number.

I have also looked into using Sharppcap and packet.net but all of the examples I've found only listens on devices found locally (no opportunity to set parameters such as port number and ip add).

Does anyone have a suggestion on how to go about doing this?

4
  • 1
    What exactly are you trying to solve here? It is not clear what the issue is. You say "without having to connect" but don't explain what you expect it not to connect to. Do you expect to be able to listen to a remote device somehow? Commented Mar 13, 2012 at 19:23
  • Have you looked at UdpClient and UdpListner? UDP is a connection-less protocol. Commented Mar 13, 2012 at 19:24
  • @Oded, yes I have a device that is transmitting ip/tcp packets to my program. Therefore there is not a connection to the listener as you would with a tcpclient/server. I have looked into Udp, my issue with that is that its not that reliable. I need to make sure these packets get to my program and with udp there is no ack. Commented Mar 13, 2012 at 19:51
  • What's the device? Just curious.. Commented Mar 13, 2012 at 20:04

1 Answer 1

2

You should look at using the UDP protocol instead of TCP/IP.

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

Here is the code for the client:

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();            
}
Sign up to request clarification or add additional context in comments.

3 Comments

The question is tagged C#, not C, so your code example will not be that helpful to the OP.
I change the language to C# @Oded
You should also take a look at this project: codeproject.com/Articles/2614/… @rick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.