Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
a connect request was made on an already connected socket

what type of msg this?

What I have tried:

C#
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                // m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                //String szIPSelected = "172.31.1.111";
                //String szPort = "80";

                String szIPSelected = "10.2.2.82";
                String szPort = "5800";
                int alPort = System.Convert.ToInt16(szPort, 10);


                System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
                System.Net.IPEndPoint ipEnd = new System.Net.IPEndPoint(remoteIPAddress, alPort);
                sock.Connect(ipEnd);
Posted
Updated 15-May-17 0:37am
v2
Comments
Richard MacCutchan 15-May-17 3:12am    
Looks like you are doing the connect more than once.

1 solution

You have not closed a previous connection or trying to re-connect without preparing for socket reuse.

You have to ensure that sockets are gracefully shutdown and closed. See Graceful Shutdown, Linger Options, and Socket Closure (Windows)[^].

Upon closing you should call the Socket.Disconnect Method (Boolean) (System.Net.Sockets)[^] with the reuseSocket parameter set. See also the example code at the above link that calls Shutdown first to signal the other site that the connection will be closed.

If you want to reuse a connection (using same IP and port shortly after a connection has been closed), you have to prepare the socket. Upon creation you can set the SocketOptionName.ReuseAddress option using the Socket.SetSocketOption Method (SocketOptionLevel, SocketOptionName, Object) (System.Net.Sockets)[^].

You might also set a linger time. When not doing so you have to wait until the socket is finally closed internally (usually 4 minutes). But you should not set it to very small values because that can result in losing pending data and will be a forced closing (in contrast to a graceful closing).
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900