socket重复调用send要怎么做?

sunlamb 2008-09-13 02:03:49
第一次调用可以,第二次调用Receive不到东西
这个项目只能用socket,因为需要用到socket5代理
部分代码
Function SocketRead(ByVal url As String, ByVal con As Socket) As String
Dim ascii As Encoding = Encoding.ASCII
Dim request As String = url
Dim bytesSent As [Byte]() = ascii.GetBytes(request)
Dim bytesReceived(255) As [Byte]

Dim s As Socket = con
If s Is Nothing Then
Return "Connection failed"
End If
s.Send(bytesSent, bytesSent.Length, 0)
Dim bytes As Int32
Dim page As [String] = ""
Do
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
page = page + Encoding.UTF8.GetString(bytesReceived, 0, bytes)
Loop While bytes > 0
Return page
End Function
...全文
653 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlongwuhui 2008-09-15
  • 打赏
  • 举报
回复
VB.Net中Socket异步编程的实例
http://www.lob.cn/sl/network/278.shtml
yanlongwuhui 2008-09-15
  • 打赏
  • 举报
回复
你的通讯采用的是阻塞模式,而你请求的操作的返回信息虽然接收完了,但操作没有完成,结果导致阻塞。你可以试试其它能正确执行的命令,或是调整实现方式。
你命令返回信息如下:
setp1:HTTP/1.0 400 Bad Request
Server: squid/2.6.STABLE20
Date: Mon, 15 Sep 2008 01:42:23 GMT
Content-Type: text/html
Content-Length: 1367
Expires: Mon, 15 Sep 2008 01:42:23 GMT
X-Squid-Error: ERR_INVALID_URL 0
X-Cache: MISS from httpd-20.verycd.com
Via: 1.0 httpd-20.verycd.com:80 (squid/2.6.STABLE20)
Connection: close
......
yanlongwuhui 2008-09-14
  • 打赏
  • 举报
回复
贴上你的实现代码吧
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
是否是因为我每次send,Receive以后没有释放缓存或者没有抛弃第一个http协议包?如果需要,请问该怎么做?
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 yanlongwuhui 的回复:]
引用 18 楼 sunlamb 的回复:
难道说这样的方法无法实现,一次Connect可多次使用 Send 和 Receive(下载网页)

可以一次Connect可多次使用 Send 和 Receive
[/Quote]
那我第二次使用send的时候总是取不到数据,请问知道是哪里出錯吗?
ldming 2008-09-14
  • 打赏
  • 举报
回复
顶!路过!!
yanlongwuhui 2008-09-14
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 sunlamb 的回复:]
难道说这样的方法无法实现,一次Connect可多次使用 Send 和 Receive(下载网页)
[/Quote]
可以一次Connect可多次使用 Send 和 Receive
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
恩,因为第一次连接后需要登录,然后获取登录后的一些网站信息,所以不能close,或者需用到异步调用?
ZengHD 2008-09-14
  • 打赏
  • 举报
回复
第一次之后sb.Close()再connect,倒是可以,但可能不是你要的
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
“setp2:”是第二次發送send的,这部分没有数据,在最下面一行
相关代码:
head = String.Format(headget, "www.verycd.com")
constr = SocketRead(head, sb)
TextBox1.Text = "setp1:" + constr + chr


head = String.Format(headget, "www.verycd.com")
constr = SocketRead(head, sb)
TextBox1.Text += "setp2:" + constr + chr
ZengHD 2008-09-14
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 ZengHD 的回复:]
只有一个setp2,没有setp1
奇怪
[/Quote]

错了,应该是这样

[code=HTML]setp1:HTTP/1.0 400 Bad Request
Server: squid/2.6.STABLE20
Date: Sun, 14 Sep 2008 06:58:28 GMT
Content-Type: text/html
Content-Length: 1367
Expires: Sun, 14 Sep 2008 06:58:28 GMT
X-Squid-Error: ERR_INVALID_URL 0
X-Cache: MISS from httpd-20.verycd.com
Via: 1.0 httpd-20.verycd.com:80 (squid/2.6.STABLE20)
Connection: close

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<TITLE>���������������ַ��URL���޷���ȡ</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>����</H1>
<H2>�����������ַ��URL���޷���ȡ</H2>
<HR noshade size="1px">
<P>
�����Զ�ȡ������ַ��URL��ʱ��
<A HREF="www.verycd.com">www.verycd.com</A>
<P>
���������еĴ���
<UL>
<LI>
<STRONG>
Invalid URL
<BR>
��Ч����ַ
</STRONG>
</UL>

<P>
Some aspect of the requested URL is incorrect. Possible problems:
<BR>
������ַ��ijЩ�ط����
ZengHD 2008-09-14
  • 打赏
  • 举报
回复
只有一个setp2,没有setp1
奇怪
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
test()里面有两次SocketRead(head, sb)了
所以调用一次test(),setp1为第一次调用,setp2为第二次调用
ZengHD 2008-09-14
  • 打赏
  • 举报
回复
我测试好像可以啊,我在Button里调用两次test(),都获取到数据啊
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
Receive不是接收吗?那请问该怎么做?
24楼那个是完整的代码
exceed_me 2008-09-14
  • 打赏
  • 举报
回复
很可能是第一次发送后没有接收,而第二次又发送过去,当然收不到
sunlamb 2008-09-14
  • 打赏
  • 举报
回复
 
Private Function Gettest(ByVal strIpAdd As String, ByVal iPort As Integer) As Socket

Dim hostadd As IPAddress = Dns.Resolve(strIpAdd).AddressList(0)
Dim EPhost As New IPEndPoint(hostadd, iPort)
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
s.Connect(EPhost)
Return s
End Function

Function SocketRead(ByVal request As String, ByVal s As Socket) As String
Dim ascii As Encoding = Encoding.UTF8
Dim bytesSent As [Byte]() = ascii.GetBytes(request)
Dim bytes As Int32
Dim page As [String] = ""
Dim bytesReceived(255) As [Byte]


s.Send(bytesSent)

Do
System.Array.Clear(bytesReceived, 0, bytesReceived.Length)
bytes = s.Receive(bytesReceived)
page = page + Encoding.UTF8.GetString(bytesReceived, 0, bytes)
Application.DoEvents()

Loop While s.Available > 0

Return page
End Function

Function test()
Dim regStr, constr, headget As String
Dim head As [String]
Dim chr As String = ControlChars.Cr + ControlChars.Lf
Dim host As String = "www.verycd.com"
Dim sb As Socket = Gettest("www.verycd.com", "80")

headget = "GET {0} HTTP/1.1" + chr & _
"Host: www.verycd.com" + chr & _
"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16" + chr & _
"Accept:application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + chr & _
"Accept-Language:zh-cn,zh;q=0.5" + chr & _
"Accept-Encoding:gzip,deflate" + chr & _
"Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7" + chr & _
"Keep-Alive: 300" + chr & _
"Connection: keep-alive" + chr & _
"Cookie: XXXXX" + chr & _
chr
head = String.Format(headget, "www.verycd.com")
constr = SocketRead(head, sb)
TextBox1.Text = "setp1:" + constr + chr


head = String.Format(headget, "www.verycd.com")
constr = SocketRead(head, sb)
TextBox1.Text += "setp2:" + constr + chr
End Function


使用的时候,调用test(),麻烦帮我看看了,谢谢
sunlamb 2008-09-13
  • 打赏
  • 举报
回复
多谢提醒
难道说这样的方法无法实现,一次Connect可多次使用 Send 和 Receive(下载网页)
是否只能考虑异步调用?
ZengHD 2008-09-13
  • 打赏
  • 举报
回复
Function SocketRead(ByVal url As String, ByVal con As Socket) As String
Dim ascii As Encoding = Encoding.ASCII
Dim request As String = url
Dim bytesSent As [Byte]() = ascii.GetBytes(request)
Dim bytesReceived(255) As [Byte]

Dim s As Socket = con
If s Is Nothing Then
Return "Connection failed"
End If
s.Send(bytesSent)
Dim bytes As Int32
Dim page As [String] = ""
bytes = s.Receive(bytesReceived)
page = Encoding.Unicode.UTF8.GetString(bytesReceived)

While s.Available > 0
System.Array.Clear(bytesReceived, 0, bytesReceived.Length)
bytes = s.Receive(bytesReceived)
page = page + Encoding.Unicode.UTF8.GetString(bytesReceived)
'根据VB6的经验,这样的情况是不是应该加上DoEvents()呢???
Application.DoEvents();
End While
Return page
End Function
sunlamb 2008-09-13
  • 打赏
  • 举报
回复
有点急,请问是什么地方有问题
加载更多回复(15)

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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