472,371 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 4103
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Harrison | last post by:
I will be distributing an application that works with our security panel. The application can be loaded on several workstations in a network but only one workstation actually communicates via...
0
by: srikash | last post by:
Not sure where to post this question as I didn't find any groups specific to enterprise library. So I'm posting here.. I'm trying to retrieve data from a DB2 database. It throws an Invalid...
1
by: tony zheng via DBMonster.com | last post by:
Hi,any one know ,in DRDA enviroment DB27.1 running in ZOS/E,I find when apllication program access DB2 through DB2connect ,in mainframe two address space TCPIP and DIST CPU usage were very high...
1
by: Xeqister | last post by:
Hi, May I know what is the possible cause of the following error? PID:59204(db2tcpcm) Appid:none common_communication sqlcctcpconnmgr_child Probe:125 DIA3003E Error encountered in "TCPIP"...
2
by: Mike | last post by:
I would really love to use port 80 for an socket/tcpip based app. I remember in java there was a method called HTTP tunneling where you could use port 80. Also in kazaa i believe there was/is...
1
by: peter.aghl | last post by:
Hi I need to send some raw data to a SATO printer I can easily send the textstring to a laserprinter But the SATO does nothing I am almost sure that the textdata is correct (taken from another...
0
by: Arno | last post by:
Hi, I've written a class for client-socket connection, but I get a lot of times the error message "Unable to read data from the transport connection" when restart reading the stream with...
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
9
by: =?Utf-8?B?U2hhdW5P?= | last post by:
I have a TCPIP socket providing data to my app. My app works on messages (not textual) with a predefined footer (eg 0x01 followed by 0x02) How should i go about buffering this and retrieving the...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.