How can i detect TCP/Ip pcaketsin cSharp

  • Thread starter Thread starter TulasiKumar
  • Start date Start date
T

TulasiKumar

Hi all,
I am new in Netwrok porgraming in cSharp.how can i detect Tcp/Ip packet in
c#.Net.Any body knows please tell me, what are the interfaces or classes
are
supported regrading on TCP/IP packets.Please tell me any website refrences
or any Source code examples.This is very urgent requirement.

Thanks in advance,
Regards,
Tulasi Kumar.
 
It depends on what level you want to work.

In .NET the functionality you need is in System.Net and System.NET.Sockets
namespaces.

Socket class supports different types of socket ( stream, dgram, raw )
 
Thank u for giving the reply.
When ever any data is coming to the TCP/IP port i will take the data and
do some operations on that packet.Before doing the operations I have to take
the Packet data.How can i get the packet data from TCP port? can u give me
ur help regarding on this.

thanks in advance.
Regards,
Tulaskumar
 
Check the docs for the TcpClient and TcpListener

TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);


NetworkStream netStream = tcpClient.GetStream ();

if (netStream.CanWrite)
{
Byte[] sendBytes = Encoding.UTF8.GetBytes ("Is anybody there?");
netStream.Write (sendBytes, 0, sendBytes.Length);
}
else
{
Console.WriteLine ("You cannot write data to this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}

if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString (bytes);

Console.WriteLine ("This is what the host returned to you: " +
returndata);

}
else
{
Console.WriteLine ("You cannot read data from this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}
netStream.Close();
 
My heartful thanks to Vadym Stetsyak giving ur immediate reply.I will try
it.In this way,I have struck up from few days.Where i have to go u r giving
the way.I will try it and i will give the result regarding on this.Once
again very very thanks Vadym Stetsyak
Regrads
tulasi kumar
Vadym Stetsyak said:
Check the docs for the TcpClient and TcpListener

TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);


NetworkStream netStream = tcpClient.GetStream ();

if (netStream.CanWrite)
{
Byte[] sendBytes = Encoding.UTF8.GetBytes ("Is anybody there?");
netStream.Write (sendBytes, 0, sendBytes.Length);
}
else
{
Console.WriteLine ("You cannot write data to this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}

if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString (bytes);

Console.WriteLine ("This is what the host returned to you: " +
returndata);

}
else
{
Console.WriteLine ("You cannot read data from this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}
netStream.Close();


--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

TulasiKumar said:
Thank u for giving the reply.
When ever any data is coming to the TCP/IP port i will take the data and
do some operations on that packet.Before doing the operations I have to
take
the Packet data.How can i get the packet data from TCP port? can u give me
ur help regarding on this.

thanks in advance.
Regards,
Tulaskumar
 
hi Vadym Stetsyak,
i have prepared one windows service in c#.net .This is service about TcpIp
packets data checking.I had wrriten the code as per your advice.so,my
problem is how can i insatll this windows service.please tell me ur advice
,i greatly revice ur advice.

thanks in advance
regards
tulasi



Vadym Stetsyak said:
Check the docs for the TcpClient and TcpListener

TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);


NetworkStream netStream = tcpClient.GetStream ();

if (netStream.CanWrite)
{
Byte[] sendBytes = Encoding.UTF8.GetBytes ("Is anybody there?");
netStream.Write (sendBytes, 0, sendBytes.Length);
}
else
{
Console.WriteLine ("You cannot write data to this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}

if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString (bytes);

Console.WriteLine ("This is what the host returned to you: " +
returndata);

}
else
{
Console.WriteLine ("You cannot read data from this stream.");
tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
netStream.Close ();
return;
}
netStream.Close();


--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

TulasiKumar said:
Thank u for giving the reply.
When ever any data is coming to the TCP/IP port i will take the data and
do some operations on that packet.Before doing the operations I have to
take
the Packet data.How can i get the packet data from TCP port? can u give me
ur help regarding on this.

thanks in advance.
Regards,
Tulaskumar
 
Back
Top