A Winsock component

Source: Internet
Author: User
Tags date integer connect socket error server port
I wrote a Winsock component of my own, very simply, send the message to the specified IP specified port, and return the answer message. Technical rough, please advise. Attached source code is as follows:

-------------------------------------


VERSION 1.0 CLASS
BEGIN
MultiUse =-1 ' True
persistable = 0 ' notpersistable
Databindingbehavior = 0 ' Vbnone
DataSourceBehavior = 0 ' Vbnone
MTSTransactionMode = 0 ' NotAnMTSObject
End
Attribute vb_name = "Clswinsockem"
Attribute Vb_globalnamespace = False
Attribute vb_creatable = True
Attribute Vb_predeclaredid = False
Attribute vb_exposed = True
'******************************************************************************
'
' Clswinsockem.cls
' Embedded WinSocket class
' Input: Server IP, server port, outgoing delivery text
' Output: Receive Message
'
'******************************************************************************

Option Explicit

'******************************************************************************

Const recv_time_out = 60 ' Receive timeout
Const send_time_out = 60 ' Send timeout
Const conn_time_out = 120 ' Connection timeout
Const Recv_max_len = 2048 ' Maximum number of received messages

'******************************************************************************

Private WithEvents Wskem as Mswinsocklib.winsock ' Winsock control
Attribute Wskem.vb_varhelpid =-1
Private Mstrserverip as String ' remote server IP
Private mintserverport as Integer ' remote server port
Private mstrtexttosend as String ' send message
Private mstrtextreceived as String ' Receive message
Private bconnected as Boolean ' connection flag

'******************************************************************************

' Initialization
Public Sub Init (strServerIP as String, Intserverport as Integer)
Mstrserverip = strServerIP
Mintserverport = Intserverport
Mstrtexttosend = ""
mstrtextreceived = ""
bconnected = False
End Sub

' Return message
Public Function Returntext (strtexttosend As String) as String
Dim Dtstart as Date
Dtstart = Now

Set Wskem = New Mswinsocklib.winsock

' Get send string
Mstrtexttosend = Strtexttosend
Mstrtexttosend = mstrtexttosend & Chr (0)

ConnectServer
SendText

' Receive Message timeout control
Do Until not (mstrtextreceived = "")
DoEvents
If DateDiff ("s", Dtstart, now) > Recv_time_out Then
Err.Raise vbObjectError, "Socket error", "Communication timeout"
End If
Loop

Wskem.close
Set Wskem = Nothing

Returntext = mstrtextreceived
End Function

' Connect to the server
Private Sub ConnectServer ()
Dim Dtstart as Date

Dtstart = Now ()

Wskem.remotehost = Mstrserverip
Wskem.remoteport = Mintserverport
Wskem.connect

Do Until bconnected
DoE



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.