Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem disconnecting asynchronous sockets

Status
Not open for further replies.

spyG5

Programmer
Jun 3, 2003
10
PH
hi all,

i am using asynchronous sockets in my program. i'm able to establish the connection and the application works just fine. my problem is w/ disconnecting. i seem to be able to disconnect the client but is unable to do this w/ the server. below are portions of my code for establishing the server, the client, as well as the code that i'm using for disconnecting.

appreciate if somebody could tell me what i'm doing wrong.

thanks!

declarations
-----------------
int PortNum = 0;
string Hostname = "localhost";
public AsyncCallback pfnWorkerCallBack; //server
public Socket m_socListener; //server
public Socket m_socWorker; //server
public Socket m_socClient; //client


server code
----------------
m_socListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any , PortNum);
m_socListener.Bind( ipLocal );
m_socListener.Listen (4);
m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
// etc.

client code
-----------
m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );
IPHostEntry ipEntry = Dns.GetHostByName(Hostname);
IPAddress ip = ipEntry.AddressList[0];
IPEndPoint ipEnd = new IPEndPoint (ip.Address, PortNum);
m_socClient.Connect ( ipEnd );
//etc.

disconnect code
----------------
for client
----------
m_socClient.Close ();
m_socClient = null;

for server
----------
m_socWorker.Close ();
m_socWorker = null;
m_socListener.Close();
m_socListener = null;

 
Try calling the Dispose method on your socket.

Chip H.
 
I'll try it tonight Chip... thanks.

Btw, How do I prevent a second client from connecting to the server if a client has already connected to it?

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top