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]