471,317 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how TcpLinstenr is fired in windows service

Hi all,

What is my requirement is i want to get the TCPIP data from TCP Port.I had
written the code in c#.Net.What i had written the code is pasted below.what
i written the code is correct or not according to my requirement.Using this
code i didn't get any TCPIP pcakects data.Any one can modify my code or my
way of approach is wrong,please tell me.

Your suggestions ar kindly accepted.

Thanks in advance.

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

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

}

Regards,

TulasiKumar

Jan 4 '06 #1
9 1399
Are there any exceptions in this code?
what is url parameter, can it be that url will specify remote server?

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
Hi all,

What is my requirement is i want to get the TCPIP data from TCP Port.I had
written the code in c#.Net.What i had written the code is pasted
below.what
i written the code is correct or not according to my requirement.Using
this
code i didn't get any TCPIP pcakects data.Any one can modify my code or my
way of approach is wrong,please tell me.

Your suggestions ar kindly accepted.

Thanks in advance.

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

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

}

Regards,

TulasiKumar


Jan 4 '06 #2

Hi Vadyam Stetsyak,
Thank u for giving immediate response.
The above code raise one exception In listner method TcpListner.start()
there raises one exception.The Exception name is "Only One usage of each
socket address(protocol/network Address/Port)is normally permitted"This
exception raised.
Give me ur suggestion
thanks in advance,
Regards,
TulasiKumar
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Are there any exceptions in this code?
what is url parameter, can it be that url will specify remote server?

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
Hi all,

What is my requirement is i want to get the TCPIP data from TCP Port.I had written the code in c#.Net.What i had written the code is pasted
below.what
i written the code is correct or not according to my requirement.Using
this
code i didn't get any TCPIP pcakects data.Any one can modify my code or my way of approach is wrong,please tell me.

Your suggestions ar kindly accepted.

Thanks in advance.

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

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

}

Regards,

TulasiKumar





Jan 4 '06 #3
This exception occurs when you want to listen on the address:port that are
already used by other application.

use netstat -an -p tcp to determine what addresses:ports are occupied.
Normaly only one socket can listen on particular port.

In order to listen on the address already occupied by someone else - you can
set socket option
SocketOptionName.ReuseAddress

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:Oa*************@TK2MSFTNGP14.phx.gbl...

Hi Vadyam Stetsyak,
Thank u for giving immediate response.
The above code raise one exception In listner method TcpListner.start()
there raises one exception.The Exception name is "Only One usage of each
socket address(protocol/network Address/Port)is normally permitted"This
exception raised.
Give me ur suggestion
thanks in advance,
Regards,
TulasiKumar
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
> Are there any exceptions in this code?
> what is url parameter, can it be that url will specify remote server?
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > Hi all,
> >
> > What is my requirement is i want to get the TCPIP data from TCP Port.I
had > > written the code in c#.Net.What i had written the code is pasted
> > below.what
> > i written the code is correct or not according to my requirement.Using > > this
> > code i didn't get any TCPIP pcakects data.Any one can modify my code or my > > way of approach is wrong,please tell me.
> >
> > Your suggestions ar kindly accepted.
> >
> > Thanks in advance.
> >
> > 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);
> >
> > 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();
> >
> > }
> >
> > Regards,
> >
> > TulasiKumar
> >
> >
> >
> >
> >

>
>


Jan 4 '06 #4

Hi VadyamStetsyak,
Thank u very much for giving immediate response.
SocketOptionName.ReuseAddress
Where can i inclue the above statment in my program.i have tried but ,it
is given the error.Can u please modify the below code or can u tell me where
can i include the above statement.Please tell me.This is very urgent.

Thanks in advance,
public class TcpData
{

private TcpListener tcpListener;

private int Port;
private string UrlStr;
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);

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();
}
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:uE**************@TK2MSFTNGP11.phx.gbl...
This exception occurs when you want to listen on the address:port that are already used by other application.

use netstat -an -p tcp to determine what addresses:ports are occupied.
Normaly only one socket can listen on particular port.

In order to listen on the address already occupied by someone else - you can set socket option
SocketOptionName.ReuseAddress

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:Oa*************@TK2MSFTNGP14.phx.gbl...

Hi Vadyam Stetsyak,
Thank u for giving immediate response.
The above code raise one exception In listner method TcpListner.start() there raises one exception.The Exception name is "Only One usage of each socket address(protocol/network Address/Port)is normally permitted"This exception raised.
Give me ur suggestion
thanks in advance,
Regards,
TulasiKumar
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
> Are there any exceptions in this code?
> what is url parameter, can it be that url will specify remote server? >
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > Hi all,
> >
> > What is my requirement is i want to get the TCPIP data from TCP

Port.I
had
> > written the code in c#.Net.What i had written the code is pasted
> > below.what
> > i written the code is correct or not according to my

requirement.Using
> > this
> > code i didn't get any TCPIP pcakects data.Any one can modify my code
or my
> > way of approach is wrong,please tell me.
> >
> > Your suggestions ar kindly accepted.
> >
> > Thanks in advance.
> >
> > 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);
> >
> > 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();
> >
> > }
> >
> > Regards,
> >
> > TulasiKumar
> >
> >
> >
> >
> >
>
>





Jan 4 '06 #5
to set the option you have to use

Socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);

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

//------------
tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();
--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...

Hi VadyamStetsyak,
Thank u very much for giving immediate response.
SocketOptionName.ReuseAddress
Where can i inclue the above statment in my program.i have tried but ,it
is given the error.Can u please modify the below code or can u tell me
where
can i include the above statement.Please tell me.This is very urgent.

Thanks in advance,
public class TcpData
{

private TcpListener tcpListener;

private int Port;
private string UrlStr;
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);

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();
}
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:uE**************@TK2MSFTNGP11.phx.gbl...
> This exception occurs when you want to listen on the address:port that

are
> already used by other application.
>
> use netstat -an -p tcp to determine what addresses:ports are occupied.
> Normaly only one socket can listen on particular port.
>
> In order to listen on the address already occupied by someone else -

you
can
> set socket option
> SocketOptionName.ReuseAddress
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:Oa*************@TK2MSFTNGP14.phx.gbl...
> >
> > Hi Vadyam Stetsyak,
> > Thank u for giving immediate response.
> > The above code raise one exception In listner method TcpListner.start() > > there raises one exception.The Exception name is "Only One usage of each > > socket address(protocol/network Address/Port)is normally permitted"This > > exception raised.
> > Give me ur suggestion
> > thanks in advance,
> > Regards,
> > TulasiKumar
> > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > news:%2***************@TK2MSFTNGP09.phx.gbl...
> > > Are there any exceptions in this code?
> > > what is url parameter, can it be that url will specify remote server? > > >
> > > --
> > > Vadym Stetsyak aka Vadmyst
> > > http://vadmyst.blogspot.com
> > >
> > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> > > news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > > > Hi all,
> > > >
> > > > What is my requirement is i want to get the TCPIP data from TCP
> > Port.I
> > had
> > > > written the code in c#.Net.What i had written the code is pasted
> > > > below.what
> > > > i written the code is correct or not according to my
> > requirement.Using
> > > > this
> > > > code i didn't get any TCPIP pcakects data.Any one can modify my code > > or my
> > > > way of approach is wrong,please tell me.
> > > >
> > > > Your suggestions ar kindly accepted.
> > > >
> > > > Thanks in advance.
> > > >
> > > > 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);
> > > >
> > > > 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();
> > > >
> > > > }
> > > >
> > > > Regards,
> > > >
> > > > TulasiKumar
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
> >
> >

>
>


Jan 4 '06 #6
Hi Vadym Stetsyak,
Thanks for giving immediate response.

Whenever i have pasted u have given code as follows
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);

//------------
tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();

it is getting the below error.This is given some protection level
errorr.Please tell me will i import any name space?. or i am doing any
think worng.
I have imported two main name sapces
using System.Net;

using System.Net.Sockets;

this is the error:'System.Net.Sockets.TcpListener.Server' is inaccessible
due to its protection level

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
to set the option you have to use

Socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);

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

//------------
tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();
--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...

Hi VadyamStetsyak,
Thank u very much for giving immediate response.
SocketOptionName.ReuseAddress
Where can i inclue the above statment in my program.i have tried but ,it is given the error.Can u please modify the below code or can u tell me
where
can i include the above statement.Please tell me.This is very urgent.

Thanks in advance,
public class TcpData
{

private TcpListener tcpListener;

private int Port;
private string UrlStr;
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);

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();
}
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:uE**************@TK2MSFTNGP11.phx.gbl...
> This exception occurs when you want to listen on the address:port that
are
> already used by other application.
>
> use netstat -an -p tcp to determine what addresses:ports are
occupied. > Normaly only one socket can listen on particular port.
>
> In order to listen on the address already occupied by someone else - you
can
> set socket option
> SocketOptionName.ReuseAddress
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:Oa*************@TK2MSFTNGP14.phx.gbl...
> >
> > Hi Vadyam Stetsyak,
> > Thank u for giving immediate response.
> > The above code raise one exception In listner method

TcpListner.start()
> > there raises one exception.The Exception name is "Only One usage
of each
> > socket address(protocol/network Address/Port)is normally

permitted"This
> > exception raised.
> > Give me ur suggestion
> > thanks in advance,
> > Regards,
> > TulasiKumar
> > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > news:%2***************@TK2MSFTNGP09.phx.gbl...
> > > Are there any exceptions in this code?
> > > what is url parameter, can it be that url will specify remote

server?
> > >
> > > --
> > > Vadym Stetsyak aka Vadmyst
> > > http://vadmyst.blogspot.com
> > >
> > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in
message > > > news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > > > Hi all,
> > > >
> > > > What is my requirement is i want to get the TCPIP data from TCP > > Port.I
> > had
> > > > written the code in c#.Net.What i had written the code is pasted > > > > below.what
> > > > i written the code is correct or not according to my
> > requirement.Using
> > > > this
> > > > code i didn't get any TCPIP pcakects data.Any one can modify my code
> > or my
> > > > way of approach is wrong,please tell me.
> > > >
> > > > Your suggestions ar kindly accepted.
> > > >
> > > > Thanks in advance.
> > > >
> > > > 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);
> > > >
> > > > 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();
> > > >
> > > > }
> > > >
> > > > Regards,
> > > >
> > > > TulasiKumar
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
> >
> >
>
>





Jan 5 '06 #7
what version of .NET framework do you use? I have 2.0 and TclListener.Server
property is public there.
AFAIR in .NET 1.1. it is protected that is you have to inherit from
TcpListener, and then you will get the access you need

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:es**************@TK2MSFTNGP15.phx.gbl...
Hi Vadym Stetsyak,
Thanks for giving immediate response.

Whenever i have pasted u have given code as follows
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);

//------------
tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();

it is getting the below error.This is given some protection level
errorr.Please tell me will i import any name space?. or i am doing any
think worng.
I have imported two main name sapces
using System.Net;

using System.Net.Sockets;

this is the error:'System.Net.Sockets.TcpListener.Server' is inaccessible
due to its protection level

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> to set the option you have to use
>
> Socket.SetSocketOption(SocketOptionLevel.Socket,
> SocketOptionName.ReuseAddress, 1);
>
> 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);
>
> //------------
> tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
> SocketOptionName.ReuseAddress, 1);
> //------------
>
> tcpListener.Start();
>
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:u6**************@TK2MSFTNGP10.phx.gbl...
> >
> > Hi VadyamStetsyak,
> > Thank u very much for giving immediate response.
> > SocketOptionName.ReuseAddress
> > Where can i inclue the above statment in my program.i have tried but ,it > > is given the error.Can u please modify the below code or can u tell me > > where
> > can i include the above statement.Please tell me.This is very urgent.
> >
> > Thanks in advance,
> >
> >
> > public class TcpData
> > {
> >
> > private TcpListener tcpListener;
> >
> > private int Port;
> > private string UrlStr;
> > 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);
> >
> > 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();
> > }
> >
> >
> > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > news:uE**************@TK2MSFTNGP11.phx.gbl...
> > > This exception occurs when you want to listen on the address:port that > > are
> > > already used by other application.
> > >
> > > use netstat -an -p tcp to determine what addresses:ports are occupied. > > > Normaly only one socket can listen on particular port.
> > >
> > > In order to listen on the address already occupied by someone else - > > you
> > can
> > > set socket option
> > > SocketOptionName.ReuseAddress
> > >
> > > --
> > > Vadym Stetsyak aka Vadmyst
> > > http://vadmyst.blogspot.com
> > >
> > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> > > news:Oa*************@TK2MSFTNGP14.phx.gbl...
> > > >
> > > > Hi Vadyam Stetsyak,
> > > > Thank u for giving immediate response.
> > > > The above code raise one exception In listner method
> > TcpListner.start()
> > > > there raises one exception.The Exception name is "Only One usage of > > each
> > > > socket address(protocol/network Address/Port)is normally
> > permitted"This
> > > > exception raised.
> > > > Give me ur suggestion
> > > > thanks in advance,
> > > > Regards,
> > > > TulasiKumar
> > > > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > > > news:%2***************@TK2MSFTNGP09.phx.gbl...
> > > > > Are there any exceptions in this code?
> > > > > what is url parameter, can it be that url will specify remote
> > server?
> > > > >
> > > > > --
> > > > > Vadym Stetsyak aka Vadmyst
> > > > > http://vadmyst.blogspot.com
> > > > >
> > > > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in message > > > > > news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > Hi all,
> > > > > >
> > > > > > What is my requirement is i want to get the TCPIP data from TCP > > > > Port.I
> > > > had
> > > > > > written the code in c#.Net.What i had written the code is pasted > > > > > > below.what
> > > > > > i written the code is correct or not according to my
> > > > requirement.Using
> > > > > > this
> > > > > > code i didn't get any TCPIP pcakects data.Any one can modify
my > > code
> > > > or my
> > > > > > way of approach is wrong,please tell me.
> > > > > >
> > > > > > Your suggestions ar kindly accepted.
> > > > > >
> > > > > > Thanks in advance.
> > > > > >
> > > > > > 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);
> > > > > >
> > > > > > 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();
> > > > > >
> > > > > > }
> > > > > >
> > > > > > Regards,
> > > > > >
> > > > > > TulasiKumar
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
> >
> >

>
>


Jan 5 '06 #8
hi,
thank u very much for giving information
i am using .net framwork1.1.In framwork 1.1 TcpListner.Server property is
protected.So,i have inherited the The TcpListner class in to my class it is
getting one error.
class Tcp1:TcpListener

{

public Tcp1(int x)

{

Server.SetSocketOption(SocketOptionLevel.Socket,So cketOptionName.ReuseAddres
s, 1);

}

}

Error:No overload for method 'TcpListener' takes '0' arguments
Thanks in advance

Regrads,

TulasiKumar
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:uI**************@TK2MSFTNGP14.phx.gbl...
what version of .NET framework do you use? I have 2.0 and TclListener.Server property is public there.
AFAIR in .NET 1.1. it is protected that is you have to inherit from
TcpListener, and then you will get the access you need

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

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:es**************@TK2MSFTNGP15.phx.gbl...
Hi Vadym Stetsyak,
Thanks for giving immediate response.

Whenever i have pasted u have given code as follows
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);

//------------
tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();

it is getting the below error.This is given some protection level
errorr.Please tell me will i import any name space?. or i am doing any think worng.
I have imported two main name sapces
using System.Net;

using System.Net.Sockets;

this is the error:'System.Net.Sockets.TcpListener.Server' is inaccessible due to its protection level

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> to set the option you have to use
>
> Socket.SetSocketOption(SocketOptionLevel.Socket,
> SocketOptionName.ReuseAddress, 1);
>
> 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);
>
> //------------
> tcpListener.Server.SetSocketOption(SocketOptionLev el.Socket,
> SocketOptionName.ReuseAddress, 1);
> //------------
>
> tcpListener.Start();
>
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "TulasiKumar" <tu*********@nannacomputers.com> wrote in message
> news:u6**************@TK2MSFTNGP10.phx.gbl...
> >
> > Hi VadyamStetsyak,
> > Thank u very much for giving immediate response.
> > SocketOptionName.ReuseAddress
> > Where can i inclue the above statment in my program.i have tried but
,it
> > is given the error.Can u please modify the below code or can u
tell me
> > where
> > can i include the above statement.Please tell me.This is very
urgent. > >
> > Thanks in advance,
> >
> >
> > public class TcpData
> > {
> >
> > private TcpListener tcpListener;
> >
> > private int Port;
> > private string UrlStr;
> > 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);
> >
> > 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();
> > }
> >
> >
> > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > news:uE**************@TK2MSFTNGP11.phx.gbl...
> > > This exception occurs when you want to listen on the address:port that
> > are
> > > already used by other application.
> > >
> > > use netstat -an -p tcp to determine what addresses:ports are

occupied.
> > > Normaly only one socket can listen on particular port.
> > >
> > > In order to listen on the address already occupied by someone

else -
> > you
> > can
> > > set socket option
> > > SocketOptionName.ReuseAddress
> > >
> > > --
> > > Vadym Stetsyak aka Vadmyst
> > > http://vadmyst.blogspot.com
> > >
> > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in
message > > > news:Oa*************@TK2MSFTNGP14.phx.gbl...
> > > >
> > > > Hi Vadyam Stetsyak,
> > > > Thank u for giving immediate response.
> > > > The above code raise one exception In listner method
> > TcpListner.start()
> > > > there raises one exception.The Exception name is "Only One usage of
> > each
> > > > socket address(protocol/network Address/Port)is normally
> > permitted"This
> > > > exception raised.
> > > > Give me ur suggestion
> > > > thanks in advance,
> > > > Regards,
> > > > TulasiKumar
> > > > "Vadym Stetsyak" <va*****@ukr.net> wrote in message
> > > > news:%2***************@TK2MSFTNGP09.phx.gbl...
> > > > > Are there any exceptions in this code?
> > > > > what is url parameter, can it be that url will specify
remote > > server?
> > > > >
> > > > > --
> > > > > Vadym Stetsyak aka Vadmyst
> > > > > http://vadmyst.blogspot.com
> > > > >
> > > > > "TulasiKumar" <tu*********@nannacomputers.com> wrote in

message
> > > > > news:eZ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > Hi all,
> > > > > >
> > > > > > What is my requirement is i want to get the TCPIP data from TCP
> > > > Port.I
> > > > had
> > > > > > written the code in c#.Net.What i had written the code
is pasted
> > > > > > below.what
> > > > > > i written the code is correct or not according to my
> > > > requirement.Using
> > > > > > this
> > > > > > code i didn't get any TCPIP pcakects data.Any one can

modify
my
> > code
> > > > or my
> > > > > > way of approach is wrong,please tell me.
> > > > > >
> > > > > > Your suggestions ar kindly accepted.
> > > > > >
> > > > > > Thanks in advance.
> > > > > >
> > > > > > 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);
> > > > > >
> > > > > > 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();
> > > > > >
> > > > > > }
> > > > > >
> > > > > > Regards,
> > > > > >
> > > > > > TulasiKumar
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
> >
> >
>
>





Jan 5 '06 #9
Hi,

"TulasiKumar" <tu*********@nannacomputers.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
Hi all,

What is my requirement is i want to get the TCPIP data from TCP Port.I had
written the code in c#.Net.What i had written the code is pasted
below.what
i written the code is correct or not according to my requirement.Using
this
code i didn't get any TCPIP pcakects data.Any one can modify my code or my
way of approach is wrong,please tell me.


Find below a piece of code that does what you want, it's multi thread , so
you can handle more than one incoming connection

Queue connectionQueue= null;
protected override void OnStart(string[] args)
{
listenerThread = new Thread( new ThreadStart( ListenerMethod));
listenerThread.Start();
}
protected void ListenerMethod()
{

Thread workingthread;
Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized( unsyncq);
TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start();
while( true)
{
socket = listener.AcceptTcpClient();
connectionQueue.Enqueue( socket);
workingthread = new Thread( new ThreadStart( TheConnectionHandler));
workingthread.Start();
}
}
public void TheConnectionHandler()
{
TcpClient socket= (TcpClient)connectionQueue.Dequeue();
NetAccess netaccess = null;

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 5 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Neo The One | last post: by
5 posts views Thread by Yoavo | last post: by
2 posts views Thread by =?Utf-8?B?TWljaGVsIFNjaGF3YW5u?= | last post: by
reply views Thread by Geoffrey | last post: by

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.