Avatar billede vbnewbie Nybegynder
11. november 2001 - 20:09 Der er 5 kommentarer

winsock ???

Hej
er der en som kan fortælle mig hvordan jeg bruger setsockopt og bind i visual basic ???

funktionerne ser sådan ud:

Public Declare Function bind Lib \"Winsock.dll\" (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
Public Declare Function setsockopt Lib \"Winsock.dll\" (ByVal s As Integer, ByVal level As Integer, ByVal optname As Integer, optval As Any, ByVal optlen As Integer) As Integer

Avatar billede mikker Nybegynder
12. november 2001 - 16:37 #1
Ville det ikke være nemmere blot at smide en winsock.ocx på din form?
Så burde du kunne gøre det en del nemmere så vidt jeg kan se...

- Mikker
Avatar billede vbnewbie Nybegynder
12. november 2001 - 17:20 #2
man kan ikke bruge multicasting med winsck controllen!
Avatar billede vbnewbie Nybegynder
12. november 2001 - 17:21 #3
(det er ikke broadcast jeg mener men multicasting)
Avatar billede jper Nybegynder
16. november 2001 - 12:18 #4
Lidt fra msdn, som viser hvordan man bruger setsockopt.

Option Explicit

\' Error returned by Winsock API.
Const SOCKET_ERROR = -1

\' Level number for (get/set)sockopt() to apply to socket itself.
Const SOL_SOCKET = 65535      \' Options for socket level.
Const IPPROTO_TCP = 6        \' Protocol constant for TCP.

\' option flags per socket
Const SO_DEBUG = &H1&        \' Turn on debugging info recording
Const SO_ACCEPTCONN = &H2&    \' Socket has had listen() - READ-ONLY.
Const SO_REUSEADDR = &H4&    \' Allow local address reuse.
Const SO_KEEPALIVE = &H8&    \' Keep connections alive.
Const SO_DONTROUTE = &H10&    \' Just use interface addresses.
Const SO_BROADCAST = &H20&    \' Permit sending of broadcast msgs.
Const SO_USELOOPBACK = &H40&  \' Bypass hardware when possible.
Const SO_LINGER = &H80&      \' Linger on close if data present.
Const SO_OOBINLINE = &H100&  \' Leave received OOB data in line.

Const SO_DONTLINGER = Not SO_LINGER
Const SO_EXCLUSIVEADDRUSE = Not SO_REUSEADDR \' Disallow local address reuse.

\' Additional options.
Const SO_SNDBUF = &H1001&    \' Send buffer size.
Const SO_RCVBUF = &H1002&    \' Receive buffer size.
Const SO_ERROR = &H1007&      \' Get error status and clear.
Const SO_TYPE = &H1008&      \' Get socket type - READ-ONLY.

\' TCP Options
Const TCP_NODELAY = &H1&      \' Turn off Nagel Algorithm.

\' linger structure
Private Type LINGER_STRUCT
  l_onoff As Integer          \' Is linger on or off?
  l_linger As Integer        \' Linger timeout in seconds.
End Type

\' Winsock API declares
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

Private Sub Command1_Click()
  \' Read all the options and present in a message box.
  Dim socket As Long
 
  socket = Winsock1.SocketHandle
 
  If socket = 0 Then
    MsgBox \"No Socket\"
  Else
    MsgBox \"Socket Options:\" & vbCrLf & _
          \"    SO_DEBUG: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_DEBUG)) & vbCrLf & _
          \"    SO_ACCEPTCONN: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_ACCEPTCONN)) & vbCrLf & _
          \"    SO_REUSEADDR: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_REUSEADDR)) & vbCrLf & _
          \"    SO_KEEPALIVE: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_KEEPALIVE)) & vbCrLf & _
          \"    SO_DONTROUTE: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_DONTROUTE)) & vbCrLf & _
          \"    SO_BROADCAST: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_BROADCAST)) & vbCrLf & _
          \"    SO_USELOOPBACK: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_USELOOPBACK)) & vbCrLf & _
          \"    SO_LINGER: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_LINGER)) & vbCrLf & _
          \"    SO_OOBINLINE: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_OOBINLINE)) & vbCrLf & _
          \"    SO_DONTLINGER: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_DONTLINGER)) & vbCrLf & _
          \"    SO_EXCLUSIVEADDRUSE: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_EXCLUSIVEADDRUSE)) & vbCrLf & _
          \"    SO_SNDBUF: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_SNDBUF)) & vbCrLf & _
          \"    SO_RCVBUF: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_RCVBUF)) & vbCrLf & _
          \"    SO_ERROR: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_ERROR)) & vbCrLf & _
          \"    SO_TYPE: \" & CStr(GetSocketOption(socket, SOL_SOCKET, SO_TYPE)) & vbCrLf & vbCrLf & _
          \"TCP Options:\" & vbCrLf & _
          \"    TCP_NODELAY: \" & CStr(GetSocketOption(socket, IPPROTO_TCP, TCP_NODELAY))
    End If
End Sub
Private Sub Command2_Click()
  Dim lResult As Long    \' Results of 1st option.
  Dim lResult2 As Long    \' Results of 2nd option.
  Dim linger As LINGER_STRUCT
 
  If (Winsock1.Protocol = sckTCPProtocol) Then
    \' Change two options valid for TCP Sockets.
    lResult = setsockopt(Winsock1.SocketHandle, IPPROTO_TCP, TCP_NODELAY, 1, 4)
    If (lResult = SOCKET_ERROR) Then
      MsgBox \"Error setting TCP_NODELAY option: \" & CStr(Err.LastDllError)
    End If
   
    \' Set up the linger structure.
    linger.l_onoff = 1
    linger.l_linger = 5
    lResult2 = setsockopt(Winsock1.SocketHandle, SOL_SOCKET, SO_LINGER, linger, LenB(linger))
    If (lResult2 = SOCKET_ERROR) Then
      MsgBox \"Error setting SO_LINGER option: \" & CStr(Err.LastDllError)
    End If
  Else
    \' Change two options valid for UDP sockets.
    lResult = setsockopt(Winsock1.SocketHandle, SOL_SOCKET, SO_REUSEADDR, 1, 4)
    If (lResult = SOCKET_ERROR) Then
      MsgBox \"Error setting SO_REUSEADDR option: \" & CStr(Err.LastDllError)
    End If

    lResult2 = setsockopt(Winsock1.SocketHandle, SOL_SOCKET, SO_BROADCAST, 0, 4)
    If (lResult2 = SOCKET_ERROR) Then
      MsgBox \"Error setting SO_BROADCAST option: \" & CStr(Err.LastDllError)
    End If
  End If
 
  If (lResult = 0) And (lResult2 = 0) Then
    MsgBox \"Options Set\"
  End If
End Sub
Public Function GetSocketOption(lSocket As Long, lLevel As Long, lOption As Long) As Long
  Dim lResult As Long      \' Result of API call.
  Dim lBuffer As Long      \' Buffer to get value into.
  Dim lBufferLen As Long    \' len of buffer.
  Dim linger As LINGER_STRUCT
 
  \' Linger requires a structure so we will get that option differently.
  If (lOption <> SO_LINGER) And (lOption <> SO_DONTLINGER) Then
    lBufferLen = LenB(lBuffer)
    lResult = getsockopt(lSocket, lLevel, lOption, lBuffer, lBufferLen)
  Else
    lBufferLen = LenB(linger)
    lResult = getsockopt(lSocket, lLevel, lOption, linger, lBufferLen)
    lBuffer = linger.l_onoff
  End If
 
  If (lResult = SOCKET_ERROR) Then
    GetSocketOption = Err.LastDllError
  Else
    GetSocketOption = lBuffer
  End If
End Function
Private Sub Form_Load()
  Winsock1.Bind 8377      \' Set up socket enough to get nonzero socket. handle
End Sub
Avatar billede jper Nybegynder
16. november 2001 - 12:24 #5
Men ved multicast skal du bruge UDP, så mon ikke at WinSock kan det hvis du ændre Protocol property\'en til sckUDPProtocol?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester