473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 4172
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.