469,332 Members | 6,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

How to get the data from TCPIP

hi all,

My requirment is i want to listen one perticular TCPIP port for one
IPAddress,Wheneverver the data in coming to this port that data i will be
save one text file..For example:www.msdn.microsoft.com is fixed with some
TCPIP port.Whene ever data is coming in to the TCPIP that data i will be
saved one text file.I have done the code regrading my requirement.Whenever i
have started my listner class it was giving one exception ,the exception
name is

"The request Address is not valid in this context"What i wrong done in my
coded section i didn't understand.Any modifications of my code kindly
accepted.This is very urgent.

public class TcpData

{

private TcpListener tcpListener;

private int Port;

private string UrlStr;

// private StringCollection quotes;

// private Random random;

private Thread listenerThread;

private TcpClient tcpClient;

private NetworkStream netStream;

public TcpData()

{

//

// TODO: Add constructor logic here

//

}

public TcpData(String Url,int port)

{

this.UrlStr=Url;

this.Port=port;

}

public void Start()

{

try

{

listenerThread=new Thread(new ThreadStart(this.Listener));

listenerThread.Start();

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

}

protected void Listener()

{

try

{

FileInfo fs=new FileInfo("StreamData.txt");
StreamWriter swr=new StreamWriter("StreamData.txt",true);

IPAddress ipAddress=Dns.Resolve(UrlStr).AddressList[0];

tcpListener=new TcpListener(ipAddress,Port);

//ipAddress);

//,Port);

Tcp1 tp=new Tcp1(ipAddress,Port);

tcpListener.Start();

while(true)

{

tcpClient=tcpListener.AcceptTcpClient();

netStream=tcpClient.GetStream();

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);

swr.WriteLine(returndata);


}

else

{

Console.WriteLine ("You cannot read data from this stream.");

tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
//return;

}

netStream.Close ();

swr.Flush();

swr.Close();

}

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}
}
public void Stop()

{

tcpListener.Stop();

}

public void Resume()

{

listenerThread.Resume();

}

public void Suspend()

{

listenerThread.Suspend();

}

class Tcp1:TcpListener

{

public Tcp1(System.Net.IPAddress IP,int prt):base(IP,prt)

{

//IPAddress ip=IPAddress.Parse("192.168.0.170");

//base(IP,prt);
Server.SetSocketOption(SocketOptionLevel.Socket,So cketOptionName.ReuseAddres
s, 1);

}

}

Thanks in Advance,

Tulasi Kumar

Jan 6 '06 #1
3 3980
You can only listen to the IP numbers and port addresses of your own
computer, you seem to assume that you can eavesdrop on any IP address you
like. If as you seem to be doing you resolve the IP address of msdn and then
try and listen to it, the listener will just reject the address as it is not
valid for your machine.

"TulasiKumar" wrote:
hi all,

My requirment is i want to listen one perticular TCPIP port for one
IPAddress,Wheneverver the data in coming to this port that data i will be
save one text file..For example:www.msdn.microsoft.com is fixed with some
TCPIP port.Whene ever data is coming in to the TCPIP that data i will be
saved one text file.I have done the code regrading my requirement.Whenever i
have started my listner class it was giving one exception ,the exception
name is

"The request Address is not valid in this context"What i wrong done in my
coded section i didn't understand.Any modifications of my code kindly
accepted.This is very urgent.

public class TcpData

{

private TcpListener tcpListener;

private int Port;

private string UrlStr;

// private StringCollection quotes;

// private Random random;

private Thread listenerThread;

private TcpClient tcpClient;

private NetworkStream netStream;

public TcpData()

{

//

// TODO: Add constructor logic here

//

}

public TcpData(String Url,int port)

{

this.UrlStr=Url;

this.Port=port;

}

public void Start()

{

try

{

listenerThread=new Thread(new ThreadStart(this.Listener));

listenerThread.Start();

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

}

protected void Listener()

{

try

{

FileInfo fs=new FileInfo("StreamData.txt");
StreamWriter swr=new StreamWriter("StreamData.txt",true);

IPAddress ipAddress=Dns.Resolve(UrlStr).AddressList[0];

tcpListener=new TcpListener(ipAddress,Port);

//ipAddress);

//,Port);

Tcp1 tp=new Tcp1(ipAddress,Port);

tcpListener.Start();

while(true)

{

tcpClient=tcpListener.AcceptTcpClient();

netStream=tcpClient.GetStream();

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);

swr.WriteLine(returndata);


}

else

{

Console.WriteLine ("You cannot read data from this stream.");

tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
//return;

}

netStream.Close ();

swr.Flush();

swr.Close();

}

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}
}
public void Stop()

{

tcpListener.Stop();

}

public void Resume()

{

listenerThread.Resume();

}

public void Suspend()

{

listenerThread.Suspend();

}

class Tcp1:TcpListener

{

public Tcp1(System.Net.IPAddress IP,int prt):base(IP,prt)

{

//IPAddress ip=IPAddress.Parse("192.168.0.170");

//base(IP,prt);
Server.SetSocketOption(SocketOptionLevel.Socket,So cketOptionName.ReuseAddres
s, 1);

}

}

Thanks in Advance,

Tulasi Kumar


Jan 6 '06 #2
Thank u for giving immediate reply.
How can i listen the MSDN site or another Sites IP addresses?Can u please
tell me at what way i can do my task. please tell me ur suggestion.this very
urgent.

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
You can only listen to the IP numbers and port addresses of your own
computer, you seem to assume that you can eavesdrop on any IP address you like. If as you seem to be doing you resolve the IP address of msdn and then try and listen to it, the listener will just reject the address as it is not valid for your machine.

"TulasiKumar" wrote:
hi all,

My requirment is i want to listen one perticular TCPIP port for one
IPAddress,Wheneverver the data in coming to this port that data i will be save one text file..For example:www.msdn.microsoft.com is fixed with some TCPIP port.Whene ever data is coming in to the TCPIP that data i will be saved one text file.I have done the code regrading my requirement.Whenever i have started my listner class it was giving one exception ,the exception name is

"The request Address is not valid in this context"What i wrong done in my coded section i didn't understand.Any modifications of my code kindly
accepted.This is very urgent.

public class TcpData

{

private TcpListener tcpListener;

private int Port;

private string UrlStr;

// private StringCollection quotes;

// private Random random;

private Thread listenerThread;

private TcpClient tcpClient;

private NetworkStream netStream;

public TcpData()

{

//

// TODO: Add constructor logic here

//

}

public TcpData(String Url,int port)

{

this.UrlStr=Url;

this.Port=port;

}

public void Start()

{

try

{

listenerThread=new Thread(new ThreadStart(this.Listener));

listenerThread.Start();

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

}

protected void Listener()

{

try

{

FileInfo fs=new FileInfo("StreamData.txt");
StreamWriter swr=new StreamWriter("StreamData.txt",true);

IPAddress ipAddress=Dns.Resolve(UrlStr).AddressList[0];

tcpListener=new TcpListener(ipAddress,Port);

//ipAddress);

//,Port);

Tcp1 tp=new Tcp1(ipAddress,Port);

tcpListener.Start();

while(true)

{

tcpClient=tcpListener.AcceptTcpClient();

netStream=tcpClient.GetStream();

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);

swr.WriteLine(returndata);


}

else

{

Console.WriteLine ("You cannot read data from this stream.");

tcpClient.Close ();

// Closing the tcpClient instance does not close the network stream.
//return;

}

netStream.Close ();

swr.Flush();

swr.Close();

}

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}
}
public void Stop()

{

tcpListener.Stop();

}

public void Resume()

{

listenerThread.Resume();

}

public void Suspend()

{

listenerThread.Suspend();

}

class Tcp1:TcpListener

{

public Tcp1(System.Net.IPAddress IP,int prt):base(IP,prt)

{

//IPAddress ip=IPAddress.Parse("192.168.0.170");

//base(IP,prt);
Server.SetSocketOption(SocketOptionLevel.Socket,So cketOptionName.ReuseAddres s, 1);

}

}

Thanks in Advance,

Tulasi Kumar




Jan 6 '06 #3
You cannot read messages sent to other machines, the messages are routed to
that machine and never arrive at your machine, you can only listen to
messages sent to your machine (The only exception is you could eavesdrop on
messages on your segment of the network but that is all and strictly limited).

"TulasiKumar" wrote:
Thank u for giving immediate reply.
How can i listen the MSDN site or another Sites IP addresses?Can u please
tell me at what way i can do my task. please tell me ur suggestion.this very
urgent.

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
> You can only listen to the IP numbers and port addresses of your own
> computer, you seem to assume that you can eavesdrop on any IP address

you
> like. If as you seem to be doing you resolve the IP address of msdn and

then
> try and listen to it, the listener will just reject the address as it is

not
> valid for your machine.
>
> "TulasiKumar" wrote:
>
> > hi all,
> >
> > My requirment is i want to listen one perticular TCPIP port for one
> > IPAddress,Wheneverver the data in coming to this port that data i will be > > save one text file..For example:www.msdn.microsoft.com is fixed with some > > TCPIP port.Whene ever data is coming in to the TCPIP that data i will be > > saved one text file.I have done the code regrading my requirement.Whenever i > > have started my listner class it was giving one exception ,the exception > > name is
> >
> > "The request Address is not valid in this context"What i wrong done in my > > coded section i didn't understand.Any modifications of my code kindly
> > accepted.This is very urgent.
> >
> >
> >
> > public class TcpData
> >
> > {
> >
> > private TcpListener tcpListener;
> >
> > private int Port;
> >
> > private string UrlStr;
> >
> > // private StringCollection quotes;
> >
> > // private Random random;
> >
> > private Thread listenerThread;
> >
> > private TcpClient tcpClient;
> >
> > private NetworkStream netStream;
> >
> > public TcpData()
> >
> > {
> >
> > //
> >
> > // TODO: Add constructor logic here
> >
> > //
> >
> > }
> >
> > public TcpData(String Url,int port)
> >
> > {
> >
> > this.UrlStr=Url;
> >
> > this.Port=port;
> >
> > }
> >
> > public void Start()
> >
> > {
> >
> > try
> >
> > {
> >
> > listenerThread=new Thread(new ThreadStart(this.Listener));
> >
> > listenerThread.Start();
> >
> > }
> >
> > catch(Exception ex)
> >
> > {
> >
> > Console.WriteLine(ex.Message);
> >
> > }
> >
> >
> >
> > }
> >
> > protected void Listener()
> >
> > {
> >
> > try
> >
> > {
> >
> > FileInfo fs=new FileInfo("StreamData.txt");
> >
> >
> > StreamWriter swr=new StreamWriter("StreamData.txt",true);
> >
> > IPAddress ipAddress=Dns.Resolve(UrlStr).AddressList[0];
> >
> > tcpListener=new TcpListener(ipAddress,Port);
> >
> > //ipAddress);
> >
> > //,Port);
> >
> >
> >
> > Tcp1 tp=new Tcp1(ipAddress,Port);
> >
> > tcpListener.Start();
> >
> > while(true)
> >
> > {
> >
> > tcpClient=tcpListener.AcceptTcpClient();
> >
> > netStream=tcpClient.GetStream();
> >
> > 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);
> >
> > swr.WriteLine(returndata);
> >
> >
> >
> >
> > }
> >
> > else
> >
> > {
> >
> > Console.WriteLine ("You cannot read data from this stream.");
> >
> > tcpClient.Close ();
> >
> > // Closing the tcpClient instance does not close the network stream.
> >
> >
> > //return;
> >
> > }
> >
> > netStream.Close ();
> >
> > swr.Flush();
> >
> > swr.Close();
> >
> > }
> >
> > }
> >
> > catch(Exception ex)
> >
> > {
> >
> > Console.WriteLine(ex.Message);
> >
> > }
> >
> >
> > }
> >
> >
> > public void Stop()
> >
> > {
> >
> > tcpListener.Stop();
> >
> > }
> >
> > public void Resume()
> >
> > {
> >
> > listenerThread.Resume();
> >
> > }
> >
> > public void Suspend()
> >
> > {
> >
> > listenerThread.Suspend();
> >
> > }
> >
> > class Tcp1:TcpListener
> >
> > {
> >
> > public Tcp1(System.Net.IPAddress IP,int prt):base(IP,prt)
> >
> > {
> >
> > //IPAddress ip=IPAddress.Parse("192.168.0.170");
> >
> > //base(IP,prt);
> >
> >
> > Server.SetSocketOption(SocketOptionLevel.Socket,So cketOptionName.ReuseAddres > > s, 1);
> >
> > }
> >
> >
> >
> > }
> >
> >
> >
> > Thanks in Advance,
> >
> > Tulasi Kumar
> >
> >
> >
> >
> >
> >



Jan 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by srikash | last post: by
1 post views Thread by tony zheng via DBMonster.com | last post: by
1 post views Thread by Xeqister | last post: by
1 post views Thread by peter.aghl | last post: by
9 posts views Thread by =?Utf-8?B?U2hhdW5P?= | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.