WSAAsyncSelect简单问题! ( 积分: 100 )

  • 主题发起人 主题发起人 6713827
  • 开始时间 开始时间
6

6713827

Unregistered / Unconfirmed
GUEST, unregistred user!
const<br>&nbsp;&nbsp;WM_SOCKET&nbsp;=&nbsp;WM_USER&nbsp;+&nbsp;$1000;<br>type&nbsp;&nbsp;&nbsp;<br>TNet=class<br>private<br>&nbsp;&nbsp;{SOCKET消息钩子}<br>&nbsp;&nbsp;procedure&nbsp;SockProssMsg(var&nbsp;Msg&nbsp;:&nbsp;TMessage);&nbsp;Message&nbsp;WM_SOCKET;<br>public<br>&nbsp;&nbsp;{初始化SOCKET}<br>&nbsp;&nbsp;function&nbsp;SocketInitialize:&nbsp;Boolean;<br>&nbsp;&nbsp;{关闭SOCKET}<br>&nbsp;&nbsp;function&nbsp;SocketFinalize:&nbsp;Boolean;<br>&nbsp;&nbsp;{SOCKET连接}<br>&nbsp;&nbsp;function&nbsp;SocketConnect:&nbsp;Boolean;<br>&nbsp;&nbsp;{SOCKET消息发送}<br>&nbsp;&nbsp;function&nbsp;SocketSend(S:String):&nbsp;Boolean;<br>end;<br>function&nbsp;TNet.SocketInitialize:&nbsp;Boolean;<br>begin<br>Result:=True;<br>try<br>if&nbsp;not&nbsp;InitializeSocket&nbsp;then&nbsp;begin<br>writeln('初始化Socket错误'+'&nbsp;错误代码为:&nbsp;'+IntToStr(WSAGetLastError()));<br>end&nbsp;Else<br>Begin<br>writeln('初始化Socket成功');<br>SocketIni:=True;<br>end;<br>except<br>Result:=False;<br>end;<br>end;<br><br>function&nbsp;TNet.SocketFinalize:&nbsp;Boolean;<br>begin<br>Result:=True;<br>try<br>if&nbsp;not&nbsp;cleanupSocket&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;writeln('关闭Socket错误'+'&nbsp;错误代码为:&nbsp;'+&nbsp;IntToStr(WSAGetLastError()));<br>end&nbsp;Else<br>Begin<br>writeln('关闭Socket成功');<br>SocketIni:=True;<br>SocketConn:=True;<br>end;<br>except<br>Result:=False;<br>end;<br>end;<br><br>function&nbsp;TNet.SocketConnect:&nbsp;Boolean;<br>begin<br>Result:=True;<br>try<br>CliSock:=&nbsp;Socket(AF_INET,Sock_Stream,IPPROTO_TCP);<br>if&nbsp;Not&nbsp;SocketIni&nbsp;then&nbsp;SocketInitialize;<br>{初始化连接}<br>if&nbsp;CliSock&nbsp;=&nbsp;INVALID_SOCKET&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;writeln('初始化Sock套接字失败'+&nbsp;'&nbsp;错误代码为:'+&nbsp;IntToStr(WSAGetLastError()));<br>&nbsp;&nbsp;CloseSocket(Clisock);<br>end&nbsp;Else&nbsp;writeln('初始化Sock套接字成功');<br>{开始连接}<br>if&nbsp;not&nbsp;ConnectIP(CliSock,Ip,Port)&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;writeln('连接服务器错误.'+'错误代码:&nbsp;'+&nbsp;IntToStr(WSAGetLastError()));<br>end<br>else&nbsp;begin<br>&nbsp;&nbsp;WSAAsyncSelect(CliSock,Handle,WM_SOCKET,FD_READ&nbsp;or&nbsp;FD_CLOSE&nbsp;Or&nbsp;SOCKET_ERROR&nbsp;Or&nbsp;FD_WRITE&nbsp;Or&nbsp;FD_CONNECT);<br>&nbsp;&nbsp;SocketConn:=True;<br>&nbsp;&nbsp;writeln('连接到服务器成功');<br>&nbsp;&nbsp;SocketSend('收到次消息表示连接成功');<br>end;<br>except<br>Result:=False;<br>end;<br>end;<br><br>procedure&nbsp;TNet.SockProssMsg(var&nbsp;Msg:&nbsp;TMessage);<br>Var<br>&nbsp;RecvMsg:&nbsp;Integer;<br>&nbsp;RecvLen:&nbsp;longint;<br>&nbsp;RecvData:&nbsp;Buff;<br>&nbsp;RecvStr:&nbsp;string;<br>Begin<br>&nbsp;&nbsp;writeln('消息处理事件');&nbsp;<br>&nbsp;&nbsp;RecvMsg:=&nbsp;WSAGetSelectError(Msg.LParam);<br>&nbsp;&nbsp;case&nbsp;RecvMsg&nbsp;of<br>&nbsp;&nbsp;&nbsp;&nbsp;SOCKET_ERROR:writeln('连接错误');<br>&nbsp;&nbsp;&nbsp;&nbsp;FD_CLOSE:writeln('与服务器断开连接');<br>&nbsp;&nbsp;&nbsp;&nbsp;FD_READ:<br>&nbsp;&nbsp;&nbsp;&nbsp;Begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeln('接收数据错误');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RecvLen:=&nbsp;Recv(Msg.WParam,RecvData,Sizeof(Recvdata),0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;RecvLen&nbsp;=&nbsp;SOCKET_ERROR&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeln('接收数据错误!');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RecvStr:=&nbsp;Trim(Copy(RecvData,0,Length(RecvData)));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeln(RecvStr);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;&nbsp;<br>end;<br><br>function&nbsp;TNet.SocketSend(S:String):&nbsp;Boolean;<br>Begin<br>Result:=True;<br>Try<br>&nbsp;&nbsp;writeln('开始发送消息!');<br>&nbsp;&nbsp;if&nbsp;Send(CliSock,Pointer(pchar(s))^,length(s),0)&nbsp;=&nbsp;SOCKET_ERROR&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;writeln('发送消息到服务器错误!请重试!!');<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseSocket(CliSock);<br>&nbsp;&nbsp;&nbsp;&nbsp;Result:=False;<br>&nbsp;&nbsp;end&nbsp;Else&nbsp;writeln('消息发送成功!');<br>except<br>Result:=False;<br>end;<br>end;<br><br>程序部分代码!<br>运行情况:<br>程序运行时,SOCKET没问题,消息能正确发送,服务端能正确接受;<br>问题:<br>当服务器向客户端发送消息时,客户端没有触发消息事件(SockProssMsg);<br>报告完毕。<br>请大侠指点!
 
if&nbsp;WSAGETSELECTERROR(Msg.lParam)&nbsp;&lt;&gt;&nbsp;0&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;writeln('连接错误');<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;case&nbsp;WSAGetSelectEvent(Msg.lParam)&nbsp;of<br>&nbsp;&nbsp;&nbsp;&nbsp;FD_CONNECT:&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;...
 
不应该吧?<br>他开始这段都没有执行。<br>procedure&nbsp;TNet.SockProssMsg(var&nbsp;Msg:&nbsp;TMessage);<br>Var<br>&nbsp;RecvMsg:&nbsp;Integer;<br>&nbsp;RecvLen:&nbsp;longint;<br>&nbsp;RecvData:&nbsp;Buff;<br>&nbsp;RecvStr:&nbsp;string;<br>Begin<br>&nbsp;&nbsp;writeln('消息处理事件');&nbsp;<br>这里该输出消息的控制台,但是没有。
 
不对,效果是一样的!<br>没有收到消息!
 
请参考:&nbsp;http://www.delphibbs.com/delphibbs/dispq.asp?lid=3627212
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
775
import
I
I
回复
0
查看
720
import
I
后退
顶部