how TcpLinstenr is fired in windows service

T

TulasiKumar

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
 
V

Vadym Stetsyak

Are there any exceptions in this code?
what is url parameter, can it be that url will specify remote server?
 
T

TulasiKumar

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 said:
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 said:
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
 
V

Vadym Stetsyak

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 said:
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 said:
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 said:
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
 
T

TulasiKumar

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 said:
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 said:
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 said:
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

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
 
V

Vadym Stetsyak

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(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();


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

TulasiKumar said:
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 said:
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 said:
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
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

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
 
T

TulasiKumar

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(SocketOptionLevel.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 said:
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(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();


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

TulasiKumar said:
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 said:
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


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

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
 
V

Vadym Stetsyak

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 said:
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(SocketOptionLevel.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 said:
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(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();


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

TulasiKumar said:
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();
}


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


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

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
 
T

TulasiKumar

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,SocketOptionName.ReuseAddres
s, 1);

}

}

Error:No overload for method 'TcpListener' takes '0' arguments


Thanks in advance

Regrads,

TulasiKumar


Vadym Stetsyak said:
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 said:
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(SocketOptionLevel.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 said:
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(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
//------------

tcpListener.Start();


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


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


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


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

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

TulasiKumar said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top