Member1

Member1

  • NA
  • 169
  • 50.9k

How to close TcpClient object ?

Jul 17 2015 6:25 AM
I want to close and dispose TcpClient object and TcpListener object in proper way in form clossing event how to do that?. I tried some code for this but exception still displays. help me .
Thanks
here is my code<

public void startServer()
{

int port = Convert.ToInt32(txtport.Text);
string ipaddre = getip();
IPAddress iP = IPAddress.Parse(ipaddre);
var serverSocket = new TcpListener(iP, port);
var clientSocket = default(TcpClient);
int counter = 0;
Form1 f1 = new Form1();
serverSocket.Start();
AppendTxtdata("Server Started , Waiting for Client Connection");

IsRunning = true;

try
{

while (IsRunning)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
AppendTxtdata(" >> " + "Client No:" + Convert.ToString(counter) + " Connected-- IP --" + clientSocket.Client.RemoteEndPoint.ToString() + "");
AppendTxtdata(Environment.NewLine);
handleClient client = new connectSuccess.handleClient();
client.startClient(clientSocket, Convert.ToString(counter));
// break;
}

AppendTxtdata("Data sent to Client No :" + Convert.ToString(counter));
//clientSocket.Close();
//serverSocket.Stop();
}
catch { }
finally
{
clientSocket.Close();
serverSocket.Stop();
}
}

I don't have access of these objects out of this method.
 

Answers (1)