Results 1 to 4 of 4

Thread: Winsocks

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80

    Winsocks

    I am new to Api's. Does anyone know of any site that teachs the winsocks api? I have seen vbnet and vbapi and all they have are examples. I would like to know the basic's of winsocks.

    Any suggestions?

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    This article is pretty good at explaining the APIs.

    http://www.15seconds.com/issue/990408.htm
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is some sample code:
    VB Code:
    1. 'This project needs a TextBox
    2. '-> (Name)=Text1
    3. '-> MultiLine=True
    4. 'in a form
    5. Private Sub Form_Load()
    6.     'KPD-Team 2000
    7.     'URL: [url]http://www.allapi.net/[/url]
    8.     'E-Mail: [email][email protected][/email]
    9.     Dim sSave As String
    10.     Me.AutoRedraw = True
    11.     Set Obj = Me.Text1
    12.     'Start subclassing
    13.     HookForm Me
    14.     'create a new winsock session
    15.     StartWinsock sSave
    16.     'show the winsock version on this form
    17.     If InStr(1, sSave, Chr$(0)) > 0 Then sSave = Left$(sSave, InStr(1, sSave, Chr$(0)) - 1)
    18.     Me.Print sSave
    19.     'connect to Microsoft.com
    20.     lSocket = ConnectSock("www.microsoft.com", 80, 0, Me.hwnd, False)
    21. End Sub
    22. Private Sub Form_Unload(Cancel As Integer)
    23.     'close our connection to microsoft.com
    24.     closesocket lSocket
    25.     'end winsock session
    26.     EndWinsock
    27.     'stop subclassing
    28.     UnHookForm Me
    29. End Sub
    30. 'in a module
    31. Public Const AF_INET = 2
    32. Public Const INVALID_SOCKET = -1
    33. Public Const SOCKET_ERROR = -1
    34. Public Const FD_READ = &H1&
    35. Public Const FD_WRITE = &H2&
    36. Public Const FD_CONNECT = &H10&
    37. Public Const FD_CLOSE = &H20&
    38. Public Const PF_INET = 2
    39. Public Const SOCK_STREAM = 1
    40. Public Const IPPROTO_TCP = 6
    41. Public Const GWL_WNDPROC = (-4)
    42. Public Const WINSOCKMSG = 1025
    43. Public Const WSA_DESCRIPTIONLEN = 256
    44. Public Const WSA_DescriptionSize = WSA_DESCRIPTIONLEN + 1
    45. Public Const WSA_SYS_STATUS_LEN = 128
    46. Public Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN + 1
    47. Public Const INADDR_NONE = &HFFFF
    48. Public Const SOL_SOCKET = &HFFFF&
    49. Public Const SO_LINGER = &H80&
    50. Public Const hostent_size = 16
    51. Public Const sockaddr_size = 16
    52. Type WSADataType
    53.     wVersion As Integer
    54.     wHighVersion As Integer
    55.     szDescription As String * WSA_DescriptionSize
    56.     szSystemStatus As String * WSA_SysStatusSize
    57.     iMaxSockets As Integer
    58.     iMaxUdpDg As Integer
    59.     lpVendorInfo As Long
    60. End Type
    61. Type HostEnt
    62.     h_name As Long
    63.     h_aliases As Long
    64.     h_addrtype As Integer
    65.     h_length As Integer
    66.     h_addr_list As Long
    67. End Type
    68. Type sockaddr
    69.     sin_family As Integer
    70.     sin_port As Integer
    71.     sin_addr As Long
    72.     sin_zero As String * 8
    73. End Type
    74. Type LingerType
    75.     l_onoff As Integer
    76.     l_linger As Integer
    77. End Type
    78. Public 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
    79. Public 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
    80. Public Declare Function WSAGetLastError Lib "wsock32.dll" () As Long
    81. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    82. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    83. Public Declare Function WSAIsBlocking Lib "wsock32.dll" () As Long
    84. Public Declare Function WSACleanup Lib "wsock32.dll" () As Long
    85. Public Declare Function Send Lib "wsock32.dll" Alias "send" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
    86. Public Declare Function recv Lib "wsock32.dll" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
    87. Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
    88. Public Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Long) As Integer
    89. Public Declare Function ntohs Lib "wsock32.dll" (ByVal netshort As Long) As Integer
    90. Public Declare Function socket Lib "wsock32.dll" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
    91. Public Declare Function closesocket Lib "wsock32.dll" (ByVal s As Long) As Long
    92. Public Declare Function Connect Lib "wsock32.dll" Alias "connect" (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
    93. Public Declare Function WSAAsyncSelect Lib "wsock32.dll" (ByVal s As Long, ByVal hwnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
    94. Public Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
    95. Public Declare Function gethostbyname Lib "wsock32.dll" (ByVal host_name As String) As Long
    96. Public Declare Sub MemCopy Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)
    97. Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inn As Long) As Long
    98. Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
    99. Public Declare Function WSACancelBlockingCall Lib "wsock32.dll" () As Long
    100. Public saZero As sockaddr
    101. Public WSAStartedUp As Boolean, Obj As TextBox
    102. Public PrevProc As Long, lSocket As Long
    103. 'subclassing functions
    104. 'for more information about subclassing,
    105. 'check out the subclassing tutorial at [url]http://www.allapi.net/[/url]
    106. Public Sub HookForm(F As Form)
    107.     PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf WindowProc)
    108. End Sub
    109. Public Sub UnHookForm(F As Form)
    110.     If PrevProc <> 0 Then
    111.         SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc
    112.         PrevProc = 0
    113.     End If
    114. End Sub
    115. Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    116.     If uMsg = WINSOCKMSG Then
    117.         ProcessMessage wParam, lParam
    118.     Else
    119.         WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
    120.     End If
    121. End Function
    122. 'our Winsock-message handler
    123. Public Sub ProcessMessage(ByVal lFromSocket As Long, ByVal lParam As Long)
    124.     Dim X As Long, ReadBuffer(1 To 1024) As Byte, strCommand As String
    125.     Select Case lParam
    126.         Case FD_CONNECT 'we are connected to microsoft.com
    127.         Case FD_WRITE 'we can write to our connection
    128.             'this is a part of the HTTP protocol
    129.             'for more information about this protocol, visit [url]http://www.w3c.org/[/url]
    130.             strCommand = "GET [url]http://www.microsoft.com/[/url] HTTP/1.0" + vbCrLf
    131.             strcomand = strCommand + "Pragma: no-cache" + vbCrLf
    132.             strCommand = strCommand + "Accept: */*" + vbCrLf
    133.             strCommand = strCommand + "Accept: text/html" + vbCrLf + vbCrLf
    134.             'send the data to our microsoft.com-connection
    135.             SendData lFromSocket, strCommand
    136.         Case FD_READ 'we have data waiting to be processed
    137.             'start reading the data
    138.             Do
    139.                 X = recv(lFromSocket, ReadBuffer(1), 1024, 0)
    140.                 If X > 0 Then
    141.                     Obj.Text = Obj.Text + Left$(StrConv(ReadBuffer, vbUnicode), X)
    142.                 End If
    143.                 If X <> 1024 Then Exit Do
    144.             Loop
    145.         Case FD_CLOSE 'the connection with microsoft.com is closed
    146.     End Select
    147. End Sub
    148. 'the following functions are standard WinSock functions
    149. 'from the wsksock.bas-file
    150. Public Function StartWinsock(sDescription As String) As Boolean
    151.     Dim StartupData As WSADataType
    152.     If Not WSAStartedUp Then
    153.         If Not WSAStartup(&H101, StartupData) Then
    154.             WSAStartedUp = True
    155.             sDescription = StartupData.szDescription
    156.         Else
    157.             WSAStartedUp = False
    158.         End If
    159.     End If
    160.     StartWinsock = WSAStartedUp
    161. End Function
    162. Sub EndWinsock()
    163.     Dim Ret&
    164.     If WSAIsBlocking() Then
    165.         Ret = WSACancelBlockingCall()
    166.     End If
    167.     Ret = WSACleanup()
    168.     WSAStartedUp = False
    169. End Sub
    170. Public Function SendData(ByVal s&, vMessage As Variant) As Long
    171.     Dim TheMsg() As Byte, sTemp$
    172.     TheMsg = ""
    173.     Select Case VarType(vMessage)
    174.         Case 8209   'byte array
    175.             sTemp = vMessage
    176.             TheMsg = sTemp
    177.         Case 8      'string, if we recieve a string, its assumed we are linemode
    178.             sTemp = StrConv(vMessage, vbFromUnicode)
    179.         Case Else
    180.             sTemp = CStr(vMessage)
    181.             sTemp = StrConv(vMessage, vbFromUnicode)
    182.     End Select
    183.     TheMsg = sTemp
    184.     If UBound(TheMsg) > -1 Then
    185.         SendData = Send(s, TheMsg(0), (UBound(TheMsg) - LBound(TheMsg) + 1), 0)
    186.     End If
    187. End Function
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Continuing..
    VB Code:
    1. Function ConnectSock(ByVal Host$, ByVal Port&, retIpPort$, ByVal HWndToMsg&, ByVal Async%) As Long
    2.     Dim s&, SelectOps&, Dummy&
    3.     Dim sockin As sockaddr
    4.     SockReadBuffer$ = ""
    5.     sockin = saZero
    6.     sockin.sin_family = AF_INET
    7.     sockin.sin_port = htons(Port)
    8.     If sockin.sin_port = INVALID_SOCKET Then
    9.         ConnectSock = INVALID_SOCKET
    10.         Exit Function
    11.     End If
    12.  
    13.     sockin.sin_addr = GetHostByNameAlias(Host$)
    14.  
    15.     If sockin.sin_addr = INADDR_NONE Then
    16.         ConnectSock = INVALID_SOCKET
    17.         Exit Function
    18.     End If
    19.     retIpPort$ = getascip$(sockin.sin_addr) & ":" & ntohs(sockin.sin_port)
    20.  
    21.     s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
    22.     If s < 0 Then
    23.         ConnectSock = INVALID_SOCKET
    24.         Exit Function
    25.     End If
    26.     If SetSockLinger(s, 1, 0) = SOCKET_ERROR Then
    27.         If s > 0 Then
    28.             Dummy = closesocket(s)
    29.         End If
    30.         ConnectSock = INVALID_SOCKET
    31.         Exit Function
    32.     End If
    33.     If Not Async Then
    34.         If Connect(s, sockin, sockaddr_size) <> 0 Then
    35.             If s > 0 Then
    36.                 Dummy = closesocket(s)
    37.             End If
    38.             ConnectSock = INVALID_SOCKET
    39.             Exit Function
    40.         End If
    41.         SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
    42.         If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
    43.             If s > 0 Then
    44.                 Dummy = closesocket(s)
    45.             End If
    46.             ConnectSock = INVALID_SOCKET
    47.             Exit Function
    48.         End If
    49.     Else
    50.         SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
    51.         If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
    52.             If s > 0 Then
    53.                 Dummy = closesocket(s)
    54.             End If
    55.             ConnectSock = INVALID_SOCKET
    56.             Exit Function
    57.         End If
    58.         If Connect(s, sockin, sockaddr_size) <> -1 Then
    59.             If s > 0 Then
    60.                 Dummy = closesocket(s)
    61.             End If
    62.             ConnectSock = INVALID_SOCKET
    63.             Exit Function
    64.         End If
    65.     End If
    66.     ConnectSock = s
    67. End Function
    68. Function GetHostByNameAlias(ByVal hostname$) As Long
    69.     On Error Resume Next
    70.     Dim phe&
    71.     Dim heDestHost As HostEnt
    72.     Dim addrList&
    73.     Dim retIP&
    74.     retIP = inet_addr(hostname)
    75.     If retIP = INADDR_NONE Then
    76.         phe = gethostbyname(hostname)
    77.         If phe <> 0 Then
    78.             MemCopy heDestHost, ByVal phe, hostent_size
    79.             MemCopy addrList, ByVal heDestHost.h_addr_list, 4
    80.             MemCopy retIP, ByVal addrList, heDestHost.h_length
    81.         Else
    82.             retIP = INADDR_NONE
    83.         End If
    84.     End If
    85.     GetHostByNameAlias = retIP
    86.     If Err Then GetHostByNameAlias = INADDR_NONE
    87. End Function
    88. Function getascip(ByVal inn As Long) As String
    89.     On Error Resume Next
    90.     Dim lpStr&
    91.     Dim nStr&
    92.     Dim retString$
    93.     retString = String(32, 0)
    94.     lpStr = inet_ntoa(inn)
    95.     If lpStr = 0 Then
    96.         getascip = "255.255.255.255"
    97.         Exit Function
    98.     End If
    99.     nStr = lstrlen(lpStr)
    100.     If nStr > 32 Then nStr = 32
    101.     MemCopy ByVal retString, ByVal lpStr, nStr
    102.     retString = Left(retString, nStr)
    103.     getascip = retString
    104.     If Err Then getascip = "255.255.255.255"
    105. End Function
    106. Public Function SetSockLinger(ByVal SockNum&, ByVal OnOff%, ByVal LingerTime%) As Long
    107.     Dim Linger As LingerType
    108.     Linger.l_onoff = OnOff
    109.     Linger.l_linger = LingerTime
    110.     If setsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
    111.         Debug.Print "Error setting linger info: " & WSAGetLastError()
    112.         SetSockLinger = SOCKET_ERROR
    113.     Else
    114.         If getsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
    115.             Debug.Print "Error getting linger info: " & WSAGetLastError()
    116.             SetSockLinger = SOCKET_ERROR
    117.         End If
    118.     End If
    119. End Function
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width