Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket NET
I have studying the socket program using c#.
I am writing a simple chatroom with asynchronous socket .
this is code:
 

 
...
//creat the listensocket.
this.listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 
this.listenSocket.Bind(localEndPoint);
 
//Start the server.
listenSocket.Listen(ServerSocketSetting.Backlog);
 
SocketAsyncEventArgs acceptEventArg = CreateAcceptEventArg();
this.listenSocket.AcceptAsync(acceptEventArg);
 
 
I take the following math to stop the listenSocket:
 
internal void StopListening()
       {
          //close every client socket first.
           foreach (UserToken item in clientList)
           {
               this.CloseClientSocket(item.receiveSendEnventArg);
           }
           // andthen close the listen socket.
           if (this.listenSocket != null)
           {
               //this.listenSocket.Shutdown(SocketShutdown.Both);
               this.listenSocket.Close();
               //this.listenSocket.Disconnect(true);
           }
       }
 

Tragedy is occurred when "
this.listenSocket.Close();
" is execeted, the application of the chatroom is close auto.
I original idea is stoping listening only not exit the application, when user click the "start listening"
button , it will to relisten. So, there should have some errors i don't know.
Thanks for the good man giving me the answer.
Posted 15-Jul-11 21:33pm
szs121372
Edited 15-Jul-11 21:41pm
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First thing this link is about socket Close method
 
http://msdn.microsoft.com/en-us/library/wahsac9k.aspx[^]
 
"For connection-oriented protocols, it is recommended that you call Shutdown before calling the Close method. This ensures that all data is sent and received on the connected socket before it is closed."
 
Second thing - it is not clear what you ar doing inside CloseClientSocket.
 
Third thing - when you perform if (this.listenSocket != null), you should also assign it to null after opeations are finished.
 
And the last and most important - application closes, probably, because some exception happens during socket.Close method execution. You could try to place it inside try-catch statement and read an error message. Maybe this will help to locate and fix the error.
  Permalink  
Comments
SAKryukov at 17-Jul-11 15:32pm
   
This is the answer, my 5.
OP put replied in two "solutions" (removed but readable), please see.
--SA
skv_lviv at 17-Jul-11 22:08pm
   
Thanks.
However I don't see those replies, it looks like they are not available anymore. Will have to wait until there will be update in the original question or some new comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 17 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100