各位大虾,vb网络编程你们采用的winsock还是api? winsock运行是否稳定?进者有分!

CyberWalker99 2003-08-20 05:33:23
各位大虾,vb网络编程你们采用的winsock还是api? winsock运行是否稳定?进者有分!
...全文
363 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
rockrabbit 2004-03-24
  • 打赏
  • 举报
回复
看看。
xiaohuangtao 2003-09-21
  • 打赏
  • 举报
回复
Public Sub SendData(varData As Variant)
Attribute SendData.VB_Description = "Send data to remote computer"
'
'data to send - will be built from the varData argument
Dim arrData() As Byte
'value returned by the send(sendto) Winsock API function
Dim lngRetValue As Long
'length of the data to send - needed to call the send(sendto) Winsock API function
Dim lngBufferLength As Long
'this strucure just contains address of the remote socket to send data to;
'only for UDP sockets when the sendto Winsock API function is used
Dim udtSockAddr As sockaddr_in
'
On Error GoTo SendData_Err_Handler
'
'If a connection-oriented (TCP) socket was not created or connected to the
'remote host before calling the SendData method, the MS Winsock Control
'raises the sckBadState error.
If mvarProtocol = sckTCPProtocol Then
'
If m_lngSocketHandle = INVALID_SOCKET Then
Err.Raise sckBadState, "CSocket.SendData", _
"Wrong protocol or connection state for the requested transaction or request."
Exit Sub
End If
'
Else
'
'If the socket is a message-oriented one (UDP), this is OK to create
'it with the call of the SendData method. The SocketExists function
'creates a new socket.
If Not SocketExists Then Exit Sub
'
End If
'
Select Case varType(varData)
Case vbArray + vbByte
'--------------------------------
'Dim strArray As String
'strArray = CStr(varData)
arrData() = varData
'--------------------------------
Case vbBoolean
Dim blnData As Boolean
blnData = CBool(varData)
ReDim arrData(LenB(blnData) - 1)
CopyMemory arrData(0), blnData, LenB(blnData)
Case vbByte
Dim bytData As Byte
bytData = CByte(varData)
ReDim arrData(LenB(bytData) - 1)
CopyMemory arrData(0), bytData, LenB(bytData)
Case vbCurrency
Dim curData As Currency
curData = CCur(varData)
ReDim arrData(LenB(curData) - 1)
CopyMemory arrData(0), curData, LenB(curData)
Case vbDate
Dim datData As Date
datData = CDate(varData)
ReDim arrData(LenB(datData) - 1)
CopyMemory arrData(0), datData, LenB(datData)
Case vbDouble
Dim dblData As Double
dblData = CDbl(varData)
ReDim arrData(LenB(dblData) - 1)
CopyMemory arrData(0), dblData, LenB(dblData)
Case vbInteger
Dim intData As Integer
intData = CInt(varData)
ReDim arrData(LenB(intData) - 1)
CopyMemory arrData(0), intData, LenB(intData)
Case vbLong
Dim lngData As Long
lngData = CLng(varData)
ReDim arrData(LenB(lngData) - 1)
CopyMemory arrData(0), lngData, LenB(lngData)
Case vbSingle
Dim sngData As Single
sngData = CSng(varData)
ReDim arrData(LenB(sngData) - 1)
CopyMemory arrData(0), sngData, LenB(sngData)
Case vbString
Dim strData As String
strData = CStr(varData)
ReDim arrData(Len(strData) - 1)
arrData() = StrConv(strData, vbFromUnicode)
Case Else
'
'Unknown data type
'
End Select
'
'Store all the data to send in the module level
'variable m_strSendBuffer.
m_strSendBuffer = StrConv(arrData(), vbUnicode)
'
'Call the SendBufferedData subroutine in order to send the data.
'The SendBufferedData sub is just a common procedure that is
'called from different places in this class.
'Nothing special - just the code reuse.
m_blnSendFlag = True
Call SendBufferedData
'
EXIT_LABEL:
'
Exit Sub
'
SendData_Err_Handler:
'
If Err.LastDllError = WSAENOTSOCK Then
Err.Raise sckBadState, "CSocket.SendData", "Wrong protocol or connection state for the requested transaction or request."
Else
Err.Raise Err.Number, "CSocket.SendData", Err.Description
End If
'
GoTo EXIT_LABEL
'
End Sub


Public Sub PeekData(varData As Variant, Optional varType As Variant, Optional maxLen As Variant)
Attribute PeekData.VB_Description = "Look at incoming data without removing it from the buffer"
'
Dim lngBytesReceived As Long 'value returned by the RecvData function
'
On Error GoTo PeekData_Err_Handler
'
'The RecvData is a universal subroutine that can either to retrieve or peek
'data from the Winsock buffer. If a value of the second argument (blnPeek As Boolean)
'of the RecvData subroutine is True, it will be just peeking.
lngBytesReceived = RecvData(varData, True, IIf(IsMissing(varType), Empty, varType), _
IIf(IsMissing(maxLen), Empty, maxLen))
'
EXIT_LABEL:
'
Exit Sub
'
PeekData_Err_Handler:
'
Err.Raise Err.Number, "CSocket.PeekData", Err.Description
'
GoTo EXIT_LABEL
'
End Sub
xiaohuangtao 2003-09-21
  • 打赏
  • 举报
回复
'The CSocket state's constants as for
'the MS Winsock Control interface
Public Enum StateConstants
sckClosed = 0
sckOpen
sckListening
sckConnectionPending
sckResolvingHost
sckHostResolved
sckConnecting
sckConnected
sckClosing
sckError
End Enum
'
'In order to resolve a host name the MSocketSupport.ResolveHost
'function can be called from the Connect and SendData methods
'of this class. The callback acceptor for that routine is the
'PostGetHostEvent procedure. This procedure determines what to
'do next with the received host's address checking a value of
'the m_varInternalState variable.
Private Enum InternalStateConstants
istConnecting
istSendingDatagram
End Enum
'
Private m_varInternalState As InternalStateConstants
'
'Local (module level) variables to hold values of the
'properties of this (CSocket) class.
Private mvarProtocol As ProtocolConstants
Private mvarState As StateConstants
Private m_lngBytesReceived As Long
Private m_strLocalHostName As String
Private m_strLocalIP As String
Private m_lngLocalPort As Long
Private m_strRemoteHost As String
Private m_strRemoteHostIP As String
Private m_lngRemotePort As Long
Private m_lngSocketHandle As Long
'
'Resolving host names is performed in an asynchronous mode,
'the m_lngRequestID variable just holds the value returned
'by the ResolveHost function from the MSocketSupport module.
Private m_lngRequestID As Long
'
'Internal (for this class) buffers. They are the VB Strings.
'Don't trust that guy who told that the VB String data type
'cannot properly deal with binary data. Actually, it can, and
'moreover you have a lot of means to deal with that data -
'the VB string functions (such as Left, Mid, InStr and so on).
'If you need to get a byte array from a string, just call the
'StrConv function:
'
'byteArray() = StrConv(strBuffer, vbFromUnicode)
'
Private m_strSendBuffer As String 'The internal buffer for outgoing data
Private m_strRecvBuffer As String 'The internal buffer for incoming data
'
'Lenght of the Winsock buffers. By default = 8192 bytes for TCP sockets.
'These values are initialized in the SocketExists function.
'Now, I really don't know why I was in need to get these values.
Private m_lngSendBufferLen As Long
Private m_lngRecvBufferLen As Long
'
'Maximum size of a datagram that can be sent through
'a message-oriented (UDP) socket. This value is returned
'by the InitWinsock function from the MSocketSupport module.
Private m_lngMaxMsgSize As Long
'
'This flag variable indicates that the socket is bound to
'some local socket address
Private m_blnSocketIsBound As Boolean 'Added: 10-MAR-2002
'
Private m_blnSendFlag As Boolean 'Added: 12-SEP-2002
'
'This flag variable indicates that the SO_BROADCAST option
'is set on the socket
Private m_blnBroadcast As Boolean 'Added: 09-JULY-2002
'
'These are those MS Winsock's events.
'Pay attention that the "On" prefix is added.
Public Event OnClose()
Attribute OnClose.VB_Description = "Occurs when the connection has been closed"
Public Event OnConnect()
Attribute OnConnect.VB_Description = "Occurs connect operation is completed"
Public Event OnConnectionRequest(ByVal requestId As Long)
Public Event OnDataArrival(ByVal bytesTotal As Long)
Public Event OnError(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Public Event OnSendComplete()
Public Event OnSendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)



xiaohuangtao 2003-09-21
  • 打赏
  • 举报
回复
大家看来也不咋样,没办法,我本来是免费送给大家,可惜大家不领情:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CSocket"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'********************************************************************************
'CSocket class
'********************************************************************************
'To use this class module you need:
' MSocketSupport code module
'********************************************************************************
'Version: 1.0.12 Modified: 17-OCT-2002
'smart mail 作者 myganlimei@163.com
'********************************************************************************
'********************************************************************************
Option Explicit
'
'Added: 23-AUG-2002
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'
'The CSocket protocol's constants as for
'the MS Winsock Control interface
Public Enum ProtocolConstants
sckTCPProtocol = 0
sckUDPProtocol = 1
End Enum
'
'The CSocket error's constants as for
'the MS Winsock Control interface
Public Enum ErrorConstants
sckAddressInUse = 10048
sckAddressNotAvailable = 10049
sckAlreadyComplete = 10037
sckAlreadyConnected = 10056
sckBadState = 40006
sckConnectAborted = 10053
sckConnectionRefused = 10061
sckConnectionReset = 10054
sckGetNotSupported = 394
sckHostNotFound = 11001
sckHostNotFoundTryAgain = 11002
sckInProgress = 10036
sckInvalidArg = 40014
sckInvalidArgument = 10014
sckInvalidOp = 40020
sckInvalidPropertyValue = 380
sckMsgTooBig = 10040
sckNetReset = 10052
sckNetworkSubsystemFailed = 10050
sckNetworkUnreachable = 10051
sckNoBufferSpace = 10055
sckNoData = 11004
sckNonRecoverableError = 11003
sckNotConnected = 10057
sckNotInitialized = 10093
sckNotSocket = 10038
sckOpCanceled = 10004
sckOutOfMemory = 7
sckOutOfRange = 40021
sckPortNotSupported = 10043
sckSetNotSupported = 383
sckSocketShutdown = 10058
sckSuccess = 40017
sckTimedout = 10060
sckUnsupported = 40018
sckWouldBlock = 10035
sckWrongProtocol = 40026
End Enum
'
boyzhang 2003-09-20
  • 打赏
  • 举报
回复
好!强烈uping...
wx05 2003-09-20
  • 打赏
  • 举报
回复
没有用VB做过网络方面的,所以提不出什么好的建议
wxrwan 2003-09-20
  • 打赏
  • 举报
回复
有封装好的控件为什么不用,我用WINSOCK
xiaohuangtao 2003-09-20
  • 打赏
  • 举报
回复

我自己写了个sock类,因为WINSOCK不支持引用(做成安装盘有问题,并且不支持多线程)

超OUTLOOK软件,提供源代码
楼主: 本软件是我在业余时间完成,我的目标是将它做成国内一流的客户端邮件软件.现在初具模型

.正在修改中.它有一下特点:
1. 基于SDK模式开发
2. 大量源代码:软件中用到的所有组件,包括每一个按钮,均是自己所写.形成了自己风

格的完整的一套组件库.他们包括:列表,文件管理,菜单等
所有代码均参照标准协议写成

3. 由以下功能模块组成:邮件;新闻组;FTP;任务及其在之基础上的相应管理.如文件管

理等.
4. 合作方式:转让经营权;根据你们需要提供技术支持;作为贵公司产品发布等,我们可

以详细谈.我的联系方式是:myganlimei@163.com 13062323245

一下为该软件部分运行界面图:


安装盘下:ftp://uploads@2ccc.com/SmartMai_Setup.EXE

代码下:ftp://uploads@2ccc.com/SmartMail_Code.rar

ftp密码:uploads

建议大家先下安装盘,因为比较新,
aha99 2003-09-18
  • 打赏
  • 举报
回复
我想用一个集合来封装winsock,但是控件数组一封装就无法再响应事件,如不做成数组,则winsock就无法动态增减。 ???
wenejiang 2003-09-17
  • 打赏
  • 举报
回复
我想用一个集合来封装winsock,但是控件数组一封装就无法再响应事件,如不做成数组,则winsock就无法动态增减。
wenejiang 2003-09-17
  • 打赏
  • 举报
回复
用winsock是很好,但就是功能少了点。
guoyx 2003-09-17
  • 打赏
  • 举报
回复
我都是用winsock,稳定。我的程序8K/m都没什么问题,至少已经成功运行1年,还可以加大!!
Garfield 2003-09-17
  • 打赏
  • 举报
回复
分开发送啊。。。
我试过做多线程断点续传简易http服务器。。发送100M也没问题
oo渣渣oo 2003-09-17
  • 打赏
  • 举报
回复
在通讯数据量不大的情况下使用Winsock还是比较好的.最稳定是每次通讯在1-2K的通讯量以下,以后的数据通讯量越大,Winsock丢包的机率就越高.一旦数据量非常大,Winsock就显得十分的力不从心了
viena 2003-09-17
  • 打赏
  • 举报
回复
winsock,很稳定
viena 2003-09-17
  • 打赏
  • 举报
回复
winsock,很稳定
oo渣渣oo 2003-09-17
  • 打赏
  • 举报
回复
自己动手调用API对开发人员对API编程的熟悉程序有比较高的要求.
而Winsock现在是VB编写TCPIP通讯方式的主流

我个人感觉,Winsock封装得还比较好的,但是丢包的现象与调用API比起来还是要高一点.检测通讯异常的能力也要差一点.
aha99 2003-09-17
  • 打赏
  • 举报
回复
看你要实现什么功能了,有许多是控件实现不了的,到那时候,你就不会问这个问题了。。。。
gys_yxr 2003-09-17
  • 打赏
  • 举报
回复
winsock
liuzhijie0451 2003-08-28
  • 打赏
  • 举报
回复
winsock好
加载更多回复(14)

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧