Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Bug fix: address in use.


Godlord
 Share

Recommended Posts

Currently, all versions of Eclipse do lack the ability to re-use addresses. The correct manner to deal with this is by telling the system that the address should be re-used. This article will tell you how you should approach this issue, and give you insightful code to use. You will however have to modify the code to fit the engine you are using, because I am only showing how you can tell the system to re-use the address for a specific socket.

First, you'll need these declarations and such:
```
Const SOCKET_ERROR = -1

Const SOL_SOCKET = 65535
Const IPPROTO_TCP = 6

Const SO_DEBUG = &H1&
Const SO_ACCEPTCONN = &H2&
Const SO_REUSEADDR = &H4&
Const SO_KEEPALIVE = &H8&
Const SO_DONTROUTE = &H10&
Const SO_BROADCAST = &H20&
Const SO_USELOOPBACK = &H40&
Const SO_LINGER = &H80&
Const SO_OOBINLINE = &H100&

Const SO_DONTLINGER = Not SO_LINGER
Const SO_EXCLUSIVEADDRUSE = Not SO_REUSEADDR

Const SO_SNDBUF = &H1001&
Const SO_RCVBUF = &H1002&
Const SO_ERROR = &H1007&
Const SO_TYPE = &H1008&

Const TCP_NODELAY = &H1&

Private Type LINGER_STRUCT
  l_onoff As Integer
  l_linger As Integer
End Type

Private Declare Function setsockopt Lib "wsock32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
Private Declare Function getsockopt Lib "wsock32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, optlen As Long) As Long
```
Now, a lot of these declarations aren't exactly essential, but will allow you to set other options as well. So, how do we tell the system to actually re-use the address? The following snippet will show it for a Winsock control named 'objWinsock':
```
  Dim iResult As Long

  iResult = setsockopt(objWinsock.SocketHandle, SOL_SOCKET, SO_REUSEADDR, 1, 4)

  If (iResult = SOCKET_ERROR) Then
    MsgBox "Error setting SO_REUSEADDR option: " & CStr(Err.LastDllError)
  End If
```
This socket option should be set only for sockets that are to be bound and made to listen, therefore server sockets. This socket option should be set before binding or making the socket listen, otherwise it won't work.

Yours faithfully
  Stephan.
Link to comment
Share on other sites

if I may ask, um.. where to you use this code? and, I suppose if I don't know this, I shouldn't expect to be able to do much with the program, but I would still like to try.  You say in your descrpition, that it was need tweaking depending on what engine I'm using, so how do I know what way to tweak it. Thanks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...