vb6通send和recv请求网络资源
最近为了弄清楚send和recv的用法,特意用vb6测试了一下,头文件冗余的比较多:
Option Explicit 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 Declare Function WSAGetLastError Lib "wsock32.dll" () As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private 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
Private Declare Function WSAIsBlocking Lib "wsock32.dll" () As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private 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
Private Declare Function recv Lib "wsock32.dll" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Private Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Long) As Integer
Private Declare Function ntohs Lib "wsock32.dll" (ByVal netshort As Long) As Integer
Private Declare Function socket Lib "wsock32.dll" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
Private Declare Function closesocket Lib "wsock32.dll" (ByVal s As Long) As Long
Private Declare Function Connect Lib "wsock32.dll" Alias "connect" (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
Private Declare Function WSAAsyncSelect Lib "wsock32.dll" (ByVal s As Long, ByVal hwnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal host_name As String) As Long
Private Declare Sub MemCopy Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)
Private Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inn As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function WSACancelBlockingCall Lib "wsock32.dll" () As Long Private Const AF_INET =
Private Const INVALID_SOCKET = -
Private Const SOCKET_ERROR = -
Private Const FD_READ = &H1&
Private Const FD_WRITE = &H2&
Private Const FD_CONNECT = &H10&
Private Const FD_CLOSE = &H20&
Private Const PF_INET =
Private Const SOCK_STREAM =
Private Const IPPROTO_TCP =
Private Const GWL_WNDPROC = (-)
Private Const WINSOCKMSG =
Private Const WSA_DESCRIPTIONLEN =
Private Const WSA_DescriptionSize = WSA_DESCRIPTIONLEN +
Private Const WSA_SYS_STATUS_LEN =
Private Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN +
Private Const INADDR_NONE = &HFFFF
Private Const SOL_SOCKET = &HFFFF&
Private Const SO_LINGER = &H80&
Private Const hostent_size =
Private Const sockaddr_size =
Private Const WSAECONNRESET = & Private Type HostEnt
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type Private Type sockaddr
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String *
End Type Private Type LingerType
l_onoff As Integer
l_linger As Integer
End Type Private Type WSADataType
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSA_DescriptionSize
szSystemStatus As String * WSA_SysStatusSize
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type Function GetHostByNameAlias(ByVal hostName$) As Long
On Error Resume Next
Dim phe&
Dim heDestHost As HostEnt
Dim addrList&
Dim retIP&
retIP = inet_addr(hostName)
If retIP = INADDR_NONE Then
phe = gethostbyname(hostName)
If phe <> Then
MemCopy heDestHost, ByVal phe, hostent_size
MemCopy addrList, ByVal heDestHost.h_addr_list,
MemCopy retIP, ByVal addrList, heDestHost.h_length
Else
retIP = INADDR_NONE
End If
End If
GetHostByNameAlias = retIP
If Err Then GetHostByNameAlias = INADDR_NONE
End Function Function GetRequestPath(ByVal url As String) As String
Dim host As String
host = GetHostByNameAlias(url)
End Function Private Sub btnGO_Click()
If Trim(txtURL.Text) = "" Then
Exit Sub
End If Dim hostName As String
hostName = Mid(txtURL.Text, ) Dim StartupData As WSADataType
If WSAStartup(&H101, StartupData) <> Then
Exit Sub
End If Dim sck As Long
sck = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
If sck < Then
WSACleanup
Exit Sub
End If Dim sckAddr As sockaddr
sckAddr.sin_family = AF_INET
sckAddr.sin_addr = GetHostByNameAlias(hostName)
sckAddr.sin_port = htons() If Connect(sck, sckAddr, sockaddr_size) <> Then
If sck > Then
closesocket (sck)
End If
WSACleanup
Exit Sub
End If Dim bytesSent As Long
Dim bytesRecv As Long
Dim sendbuf() As Byte
Dim recvbuf() As Byte
Dim result As String bytesRecv =
sendbuf = "GET / HTTP/1.1" & vbCrLf _
& "HOST:" & hostName & vbCrLf _
& "Connection:Close" & vbCrLf _
& "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & vbCrLf _
& "Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3" & vbCrLf _
& "User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0" & vbCrLf & vbCrLf
sendbuf = StrConv(sendbuf, vbFromUnicode) bytesSent = Send(sck, sendbuf(), UBound(sendbuf) + , )
Debug.Print (bytesSent & "字节已发送") Do While (bytesRecv > )
ReDim recvbuf()
bytesRecv = recv(sck, recvbuf(), UBound(recvbuf) + , )
Debug.Print (bytesRecv & "字节已接收")
If (bytesRecv = Or bytesRecv = WSAECONNRESET) Then
Debug.Print ("连接已关闭")
Exit Do
End If
result = result & StrConv(recvbuf, vbUnicode) Erase recvbuf
Loop WSACleanup Debug.Print result End Sub
vb6通send和recv请求网络资源的更多相关文章
- send,recv,sendto,recvfrom ~转载
send,recv,sendto,recvfrom send函数 int send( SOCKET s, const char FAR *buf, int len, int fla ...
- socket使用TCP协议时,send、recv函数解析以及TCP连接关闭的问题
Tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...
- UNIX网络编程——send与recv函数详解
#include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags); ssize_ ...
- Linux-TCP之深入浅出send和recv
内容摘自:TCP之深入浅出send和recv.再次深入理解TCP网络编程中的send和recv 建议阅读时参考:Unix环境高级编程-TCP.UDP缓冲区 概念 先明确一个概念:每个TCP socke ...
- [转]socket使用TCP协议时,send、recv函数解析以及TCP连接关闭的问题
Tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...
- Linux-TCP之深入浅出send和recv【转】
转自:https://www.cnblogs.com/JohnABC/p/7238417.html 内容摘自:TCP之深入浅出send和recv.再次深入理解TCP网络编程中的send和recv 建议 ...
- UNIX网络编程-send、recv、sendto、recvfrom详解
send.recv和sendto.recvfrom,一般情况下,send.recv在TCP协议下使用,sendto.recvfrom在UDP协议下使用,也可以在TCP协议下使用,不过用的很少. 1.s ...
- 分片传输——send和recv函数
最近在写socket编程收发数据,对于如何发送和接收大量数据,一直在思考.send和recv一般缓存区大小为4K,但是如果你要传输的数据超过了这个标准该如何做呢. 我想到的就是如改写write和rea ...
- linux内核中send与recv函数详解
Linux send与recv函数详解 1.简介 #include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t n ...
随机推荐
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- 全面认识.NET框架(一)
重新学习下.NET框架,在这里将会对.net框架逐步的学.加深一下对.net的认识.如果那个地方有错误,希望大家能够指出来.谢谢. 知识有限,先逐步了解下.net包含的什么.我就是写写我目前想了解的内 ...
- magento在产品详细页面添加分享链接的方法
1,在产品详细页面的对用位置加入一下代码 <div class="sharethis_box"> <?php echo $this->ge ...
- 用NSOperation和NSOperationQueue实现多线程编程
1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation和NS ...
- Yii 中的createUrl和redirectUrl
Yii 这两个url表示方法容易混淆,区别如下: $this->CreateUrl('控制器/方法',get参数); $this->CreateUrl('方法'); ...
- session 原理 数据结构
[原创]转载请保留出处:shoru.cnblogs.com 晋哥哥的私房钱 引言 在web开发中,session是个非常重要的概念.在许多动态网站的开发者看来,session就是一个变量,而且其表现像 ...
- jQuery实现一个全选复选框联动效果
类似邮件列表里的复选框 要求双向联动 ☛ [实现]: <body> <div> <input type="checkbox" name="c ...
- 《JS高程》事件学习笔记
事件:文档或浏览器窗口中发生的一些特定的交互瞬间,也即用户或浏览器自身执行的某种动作. -------------------------------------------------------- ...
- 【Avalon源码】dateGetter
function dateGetter(name, size, offset, trim) { return function(date) { var value = date["get&q ...
- httpclient 发送一个请求
httpclient版本 4.1 发送一个post请求 public static JSONObject post(String url,JSONObject json){ HttpClient cl ...