高手来看下我的代码(全)
为什么到accept的时候server会“死掉”
////////////////////////////////////
'tcpserver.frm
Dim nResult As Long
Dim msg_sock As Long, accept_sock As Long
Dim tcph As TcpHeader, iph As IPHeader
Private Sub cmdstart_Click()
Dim addr As sockaddr, client_addr As sockaddr, addrlen As Long, pSockAddr As Long
Dim recvbuff(1024) As Integer, k As Long
k = LBound(recvbuff)
cmdstart.Enabled = False
cmdstop.Enabled = True
accept_sock = socket(AF_INET, SOCK_STREAM, 0)
If accept_sock = INVALID_SOCKET Then
MsgBox "Error in socket"
Exit Sub
End If
addr.sin_family = AF_INET
addr.sin_port = htons(CLng(txtdestport.Text))
addr.sin_addr = inet_addr(sckserver.LocalIP)
nResult = bind(accept_sock, addr, Len(addr))
If nResult = SOCKET_ERROR Then
MsgBox "Error in bind"
closesocket (accept_sock)
cmdstart.Enabled = True
cmdstop.Enabled = False
Exit Sub
End If
nResult = listen(accept_sock, 1)
If nResult = SOCKET_ERROR Then
MsgBox "Error in listen"
closesocket (accept_sock)
cmdstart.Enabled = True
cmdstop.Enabled = False
Exit Sub
End If
'Dim InParamBuffer As Long
'Dim BytesRet As Long
'BytesRet = 0
'InParamBuffer = 1
'nResult = WSAIoctl(accept_sock, &H98000001, InParamBuffer, Len(InParamBuffer), 0, 0, BytesRet, 0, 0)
'If nResult <> 0 Then
'MsgBox "ioctlsocket"
'Exit Sub
'End If
msg_sock = accept(accept_sock, client_addr, Len(client_addr))
??????????????????????????就这里
If msg_sock = INVALID_SOCKET Then
MsgBox "Error in accept"
closesocket (accept_sock)
cmdstart.Enabled = True
cmdstop.Enabled = False
Exit Sub
End If
Do Until False 'Len(recvbuff(k)) < 1024
DoEvents
Call Sleep(300)
nResult = recv(msg_sock, ByVal recvbuff(k), 1024, 0)
If nResult = SOCKET_ERROR Then
MsgBox "error in recieve data from remote socket"
closesocket (accept_sock)
closesocket (msg_sock)
cmdstart.Enabled = True
cmdstop.Enabled = False
Exit Sub
End If
CopyMemory tcph, recvbuff(k), Len(tcph)
CopyMemory iph, recvbuff(k), Len(iph)
txtdestaddr.Text = sckserver.LocalIP
txtsourceaddr.Text = iph.sourceIP
txtsourceport.Text = tcph.sport
txtwin.Text = tcph.win
txttcpcksum.Text = tcph.cksum
txtseq.Text = tcph.seq
txtack.Text = tcph.ack
nResult = send(msg_sock, ByVal recvbuff(k), 1024, 0)
If nResult = SOCKET_ERROR Then
MsgBox "error send data to remote socket"
closesocket (accept_sock)
closesocket (msg_sock)
cmdstart.Enabled = True
cmdstop.Enabled = False
Exit Sub
End If
Loop
End Sub
Private Sub cmdstop_Click()
closesocket (accept_sock)
closesocket (msg_sock)
WSACancelBlockingCall
WSACleanup
cmdstart.Enabled = True
cmdstop.Enabled = False
End Sub
Private Sub Form_Load()
Dim mwsaData As WSADataType
cmdstop.Enabled = False
nResult = WSAStartup(&H202, mwsaData)
If nResult <> WSANOERROR Then
MsgBox "Error en WSAStartup"
WSACleanup
Exit Sub
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
closesocket (accept_sock)
closesocket (msg_sock)
WSACancelBlockingCall
WSACleanup
End Sub
//////////////////////////
tcpclient.frm
Dim soc As Long, dwRc As Long
Dim RemoteAddr As sockaddr
Dim tcph As TcpHeader
Dim psdh As psdHeader
Dim iph As IPHeader
Private Sub cmdconnect_Click()
soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If soc = INVALID_SOCKET Then
MsgBox "Error in Create Socket"
Else
RemoteAddr.sin_family = AF_INET
RemoteAddr.sin_port = htons(CLng(txtdestport.Text))
RemoteAddr.sin_addr = GetHostByNameAlias(txtdestaddr.Text)
dwRc = connect(soc, RemoteAddr, sockaddr_size)
If dwRc = SOCKET_ERROR Then
MsgBox "Error in connect to remote Socket"
Else
cmdconnect.Enabled = False
cmddisconnect.Enabled = True
cmdsend.Enabled = True
End If
End If
End Sub
Private Sub cmddisconnect_Click()
txtdata.Text = ""
If soc <> INVALID_SOCKET Then closesocket (soc)
WSACleanup
cmdconnect.Enabled = True
cmdsend.Enabled = False
cmddisconnect.Enabled = False
soc = INVALID_SOCKET
End Sub
Private Sub cmdsend_Click()
'txtdata为设定的数据长度
Dim h As Integer, buff(0 To 65535) As Integer, g As Integer
h = CInt(txtdata.Text)
If h > 65535 Then
MsgBox "too big"
Exit Sub
End If
For g = 0 To 65535
buff(g) = 1
Next g
If soc = INVALID_SOCKET Then
MsgBox "Create Socket First"
Exit Sub
End If
Dim RetMsg(1024) As Integer, j As Long
j = LBound(RetMsg)
Dim sendbuff(128) As Integer
Dim i As Long, k As Long
i = LBound(sendbuff)
'填充包头,计算校验和
With iph
.cksum = 0
.flags = 0
.lenver = (4 \ &HF) Xor Len(iph) / 4
.len = htons(Len(iph) + Len(tcph))
.ident = 0
.ttl = 128
.destIP = txtdestaddr.Text
.sourceIP = sckclient.LocalIP
.proto = sckTCPProtocol
End With
With tcph
.sport = htons(sckclient.LocalPort)
.dport = CInt(txtdestport.Text)
.seq = 0
.ack = 1
.win = htons(16384)
.urp = 0
.cksum = 0
End With
With psdh
.saddr = iph.sourceIP
.daddr = iph.destIP
.mbz = 0
.proto = sckTCPProtocol
.tcplen = htons(Len(tcph))
End With
CopyMemory sendbuff(i), psdh, Len(psdh)
CopyMemory sendbuff(i + Len(psdh)), tcph, Len(tcph)
tcph.cksum = checksum(sendbuff(), Len(psdh) + Len(tcph))
txtsourceport.Text = tcph.sport
txtsourceaddr.Text = iph.sourceIP
txttcpcksum.Text = tcph.cksum
k = Len(iph) + Len(tcph) + h
CopyMemory sendbuff(i), tcph, Len(tcph)
CopyMemory sendbuff(i + Len(tcph)), iph, Len(iph)
Select Case k
Case k < 1024
CopyMemory RetMsg(j), tcph, Len(tcph)
CopyMemory RetMsg(j + Len(tcph)), iph, Len(iph)
CopyMemory RetMsg(j + Len(tcph) + Len(iph)), buff(0), h
dwRc = send(soc, ByVal RetMsg(j), Len(RetMsg(j)), 0)
If dwRc = SOCKET_ERROR Then
MsgBox "Couldn't send data to remote Socket"
Else
dwRc = recv(soc, ByVal RetMsg(j), 1024, 0)
If dwRc = SOCKET_ERROR Then
MsgBox "couldn't recieve data from remote socket"
Else
tcph.ack = tcph.ack + 1
txtack.Text = tcph.ack
End If
End If
tcph.win = tcph.win - 1
tcph.seq = tcph.seq + 1
txtwin.Text = tcph.win
txtseq.Text = tcph.seq
Case k >= 1024
CopyMemory RetMsg(j), sendbuff(i), Len(sendbuff(i))
CopyMemory RetMsg(j + Len(sendbuff(i))), buff(0), 1024 - Len(sendbuff(i))
g = 1024 - Len(sendbuff(i))
Do Until k <= 0
dwRc = send(soc, ByVal RetMsg(j), 1024, 0)
If dwRc = SOCKET_ERROR Then
MsgBox "Couldn't send data to remote Socket"
Else
dwRc = recv(soc, ByVal RetMsg(j), 1024, 0)
If dwRc = SOCKET_ERROR Then
MsgBox "couldn't recieve data from remote socket"
Else
tcph.ack = tcph.ack + 1
txtack.Text = tcph.ack
End If
End If
k = k - 1024
tcph.win = tcph.win - 1
tcph.seq = tcph.seq + 1
txtwin.Text = tcph.win
txtseq.Text = tcph.seq
CopyMemory RetMsg(j), buff(g), 1024
g = g + 1024
Loop
End Select
End Sub
Private Sub Form_Load()
Dim nResult As Long, mwsaData As WSADataType
cmddisconnect.Enabled = False
cmdsend.Enabled = False
nResult = WSAStartup(&H202, mwsaData)
If nResult <> WSANOERROR Then
MsgBox "Error en WSAStartup"
WSACleanup
Exit Sub
End If
End Sub