Click here to Skip to main content
15,875,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I made application which receives tcp packets from all ports on local ip. It's like sniffer.
But as result I "listen" my own application too. The question is it possible to configure socket to don't receive tcp packets from my application?.

I've read tcp header definition here and there is no word about for example process PID from which packet was sent or something. Here is piece of code

C#
Socket socket = null;

try
{
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
    socket.Bind(new IPEndPoint(IPAddress.Parse(MonitoredIPAddress), 0));
    socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
    socket.IOControl(unchecked((int)0x98000001), new byte[4] { 1, 0, 0, 0 }, new byte[4]);

    while (IsMonitoringTurnedOn)
    {
        IAsyncResult ar = socket.BeginReceive(PacketBuffer, 0, PacketBufferSize,
                          SocketFlags.None, new AsyncCallback(CallPacketReceive), this);

        while (socket.Available == 0)
        {
            Thread.Sleep(1);
            if (!IsMonitoringTurnedOn)
            {
                break;
            }
        }
        int size = socket.EndReceive(ar);

        ExtractPacketBuffer(); //Extract buffer with saving queue
     }
}
Posted

1 solution

Hi,

Sorry to say but no, it is not in the scope of the IP protocol to care about the application it came from. Any decisions should be made on either the Port, IP or content of the data packet.

If you are having the trouble that you are handling data from your own app it should be a small step to exclude that IP/port from the received data; if you do want to handle the data from others which may share your port you'll have to investigate the packet data and see if it is one of yours.

Hope this helps,

Cheers, AT
 
Share this answer
 
Comments
TimGameDev 4-Feb-12 13:59pm    
I was afraid that...Anyway thanks for the clarification!

Timur

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900