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 ...
随机推荐
- Cisco路由器的6种模式
Cisco路由器的6种模式 -------------------------------------------------------------------------------------- ...
- 【LeetCode OJ】Surrounded Regions
Problem Link: http://oj.leetcode.com/problems/surrounded-regions/ We can do follows in the 2D board. ...
- MooFest_二维树状数组
Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...
- busybox inetd tftpd
/*************************************************************************** * busybox inetd tftpd * ...
- the grave of my scripts
不定期更新.......... 1,fetch_seq.py https://github.com/freemao/AHRD/blob/master/fetch_seq.py 提取出你想要得染色体的某 ...
- (实用篇)PHP页面跳转到另一个页面的方法总结
一.用HTTP头信息 也就是用PHP的header函数.PHP里的header函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("C ...
- 编程工具系列之一------使用GDB的堆栈跟踪功能
在调试程序的过程中,查看程序的函数调用堆栈是一项最基本的任务,几乎所有的图形调试器都支持这项特性. GDB调试器当然也支持这一特性,但是功能更加灵活和丰富. GDB将当前函数的栈帧编号为0,为外层函数 ...
- JQuery onload、ready概念介绍及使用方法
页面加载完成有两种事件,一是ready,表示文档结构已经加载完成,onload,ready概念容易混淆,下面为大家详细介绍下 页面加载完成有两种事件,一是ready,表示文档结构已经加载完成(不包 ...
- jq版本1.7以上on方法使用结构
<script> $(function(){ $(document).on(" ...
- hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...