473,387 Members | 1,583 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,387 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 1482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Newsgroups MICROSOFT | last post by:
Hi, I've search a lot over the Internet and I've found many similar problems. The CurrentChanged event of the BindingManagerBase class DOESNT get fired when the application is working with...
2
by: jajalc | last post by:
Hi all, WE have a windows service using .net and which uses a System.Timers.Timer..all the of code works fine..... but after a extended period of time the timer just stops firing the elasped...
2
by: Neo The One | last post by:
This is about ASP.NET 1.0, but it may still applies to 1.1 and 2.0. We have a very large web application, some part of the code calls EXE files to do jobs. It all worked fine on Windows 2000...
5
by: Yoavo | last post by:
Hi, I wrote a windows service using c#. Is there a way to start the service automatically after it has been installed ? Yoav.
2
by: =?Utf-8?B?TWljaGVsIFNjaGF3YW5u?= | last post by:
Hi all, I try to create a service witch handle the session switch events: Lock, unlocked etc... I can do this with a standard executable like this (it's just an example):...
1
by: trinityofsouls | last post by:
I am trying to run a windows application from a service. Both the Windows Form App and the Windows Service were created using the .NET 2005 "New Project" Templates. There service is working...
0
by: das | last post by:
Hello all, I am using the SqlDependency to subscribe to any new inserts into a database table, I enabled the DB to be borker ready and subscrbed to Query notifications on the database. My C#...
1
by: Stuart Parker | last post by:
Hi... I'm messing about with windows services in vb.net 2005 and have the following question. I have a service which has a timer in it, which fires every minute - Textbook stuff I have a...
41
by: pbd22 | last post by:
Hi. I know my windows service works when i run it in debug mode on my dev machine. It also works in release mode on my dev machine. But, when I move the service to a production server, it...
0
by: Geoffrey | last post by:
Hello, I'm developping a WCF service. All is good : I have a WPF app. On a button, I call a function to register 2 WCF service (one in tcp, the other in http) But, now, I want to use a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.