Hello,
This is on socket. i m using tcpclient and tcplistener
task is when the "Active " button is pressed according to user choice tool should work as server or client.
when "Inactive" button is pressed the tool should stop working.
My problem at first time every thing works fine(i.e both client and server).In server if i press "inactive" once and try to connect once again using "Active" , i m unable to connect.In the whole process client is always connected.
my code looks like this
when "Active" button is pressed
startserver(txtRmtIPAddr.Text.Trim(), txtRmtPort.Text.Trim());
public bool startserver(string strIpAdd, string strPort)
{
try
{
IPAddress ip = IPAddress.Parse(strIpAdd);
UInt16 iPortNo = Convert.ToUInt16(strPort, 10);
//create the end point
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
server = new TcpListener(IPAddress.Any,iPortNo);
// Set the event to nonsignaled state.
tcpClientConnected.Reset();
// Start to listen for connections from a client.
server.Start();
lblTCPStatus.Text = "waiting for connection....";
// Accept the connection.
// BeginAcceptSocket() creates the accepted socket.
// ??????????? In the first "active call" the control goes to "DoAcceptTcpClientCallback() method in second call it wont?????????????
server.BeginAcceptTcpClient(
new AsyncCallback(DoAcceptTcpClientCallback),
server);
// Wait until a connection is made and processed before
// continuing.
// tcpClientConnected.WaitOne();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
when "inactive" button is pressed call to close_server() method
public void close_server()
{
server.Stop();
// serverSocket.Close();
}
As i mentoined in comment
In the first "active call" the control goes to "DoAcceptTcpClientCallback() method in second call it wont?
Thnks in advance.