| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 362 人关注过本帖
标题:socketapi 中recv()函数的返回值=-1
只看楼主 加入收藏
疯子疯了
Rank: 2
等 级:论坛游民
帖 子:6
专家分:10
注 册:2012-11-15
收藏
 问题点数:0 回复次数:1 
socketapi 中recv()函数的返回值=-1
本人刚接触socketAPI  以前也用过winsock  但感脚着这控件写的程序到别的电脑上还要安装,不方便  所以改用API了,求哪位大神长期教授啊
代码如下:
程序代码:
Private newsocket, cen
Private Function addbug(obj As Long, info As Variant) As Long
Select Case obj
       Case 1
       objec = "WSAStartup"
       Case 2
       objec = "censocket"
       Case 3
       objec = "sevsocket"
       Case 4
       objec = "bind"
       Case 5
       objec = "listen"
       Case 6
       objec = "connect"
       Case 7
       objec = "accept"
       Case 8
       objec = "send"
       Case 9
       objec = "recv"
       Case 10
       objec = "WSAGetLastError"
       Case 11
       objec = "ioctlsocket"
End Select
a = debug1.Text
debug1.Text = a & objec & "=" & info & Chr(13) + Chr(10)
End Function

Private Sub Command1_Click()
Dim msg As String
msg = Text1.Text
errin = send(cen, msg, Len(msg), 0)
Call addbug(8, errin)
Call addbug(10, WSAGetLastError)
End Sub

Private Sub Command2_Click()
closesocket (cen)
closesocket (sev)
closesocket (newsocket)
WSACleanup
End Sub

Private Sub Command3_Click()
Dim msg As String
errin = recv(newsocket, msg, 1, 0)
Call addbug(9, errin)
Call addbug(10, WSAGetLastError)
Text2.Text = msg
End Sub

Private Sub Command4_Click()
Call Form_Load
End Sub

Private Sub Form_Load()
Dim sevid As sockaddr
Dim cenid As sockaddr
cenid.sin_port = htons(5106)
cenid.sin_family = AF_INET
cenid.sin_addr = inet_addr("127.0.0.1")
sevid.sin_family = AF_INET
sevid.sin_port = htons(5106)
sevid.sin_addr = "0"
Dim udttype As WSADataType
errin = WSAStartup(&H202, udttype)
Call addbug(1, errin)
Call addbug(10, WSAGetLastError)
cen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
Call addbug(2, cen)
Call addbug(10, WSAGetLastError)
sev = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
Call addbug(3, sev)
Call addbug(10, WSAGetLastError)
errin = ioctlsocket(sev, FIONBIO, 1)
Call addbug(11, errin)
Call addbug(10, WSAGetLastError)
errin = bind(sev, sevid, Len(sevid))
Call addbug(4, errin)
Call addbug(10, WSAGetLastError)
errin = listen(sev, 1)
Call addbug(5, errin)
Call addbug(10, WSAGetLastError)
errin = connect(cen, cenid, Len(cenid))
Call addbug(6, errin)
Call addbug(10, WSAGetLastError)
newsocket = accept(sev, sevid, Len(sevid))
Call addbug(7, newsocket)
Call addbug(10, WSAGetLastError)
End Sub

Private Sub Form_Unload(Cancel As Integer)
closesocket (cen)
closesocket (sev)
closesocket (newsocket)
WSACleanup
End Sub

Private Sub Timer1_Timer()
Call Command3_Click
End Sub


[local]1[/local]


未命名.jpg (47.44 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: color 安装 
2012-11-15 21:55
疯子疯了
Rank: 2
等 级:论坛游民
帖 子:6
专家分:10
注 册:2012-11-15
收藏
得分:0 
大家都不在么?
2012-11-16 20:42
快速回复:socketapi 中recv()函数的返回值=-1
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017808 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved