资源描述:
《利用winsock控件创建的局域网聊天程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.服务器端往服务器窗体(命名为frmServer)添加三个控件,分别为LIST1(存放在线好友名单),text1(留言内容)和text2(聊天记录),程序如下:OptionExplicitConstBusyAsBoolean=FalseConstFreeAsBoolean=TrueDimConnectState()AsBooleanDimSIndexDimUsrs(0To32)'在线人名PrivateSubForm_QueryUnload(CancelAsInteger,UnloadModeAsInteger)EndEndSubPrivateSubForm_Load()IfApp
2、.PrevInstance=TrueThenMsgBox"程序已在运行",vbCriticalEndEndIfReDimPreserveConnectState(0)OnErrorResumeNextConnectState(0)=FreeListener.LocalPort=1001'端口号Listener.Listen'开始侦听EndSubPrivateSubListener_ConnectionRequest(ByValrequestIDAsLong)DimSockIndexAsIntegerDimSockNumAsIntegerOnErrorResumeNextSockNu
3、m=UBound(ConnectState)IfSockNum>32ThenExitSub'查找空闲的SckServerSockIndex=FindFreeSocket'如果已有的sock都忙,而且sock数不超过32个,动态添加sockIfSockIndex>SockNumThenLoadSckServer(SockIndex)ConnectState(SockIndex)=BusySckServer(SockIndex).Tag=SockIndex'接受请求SckServer(SockIndex).Accept(requestID)EndSubPrivateSubSckServ
4、er_DataArrival(IndexAsInteger,ByValbytesTotalAsLong)DimdxAsStringSIndex=IndexSckServer(Index).GetDatadx,vbStringIfLen(Text2.Text)>=512ThenText2.Text=""IfRight(dx,2)="
5、
6、"ThenList1.AddItemReplace(dx,"
7、","")Usrs(SIndex)=Replace(dx,"
8、","")Timer1.Enabled=TrueText2.Text=Format(Now(),"YY-MM-DDhh:mm:s
9、s")&"“"&Usrs(Index)&"”上线。"&vbCrLf&Text2.TextOpenApp.Path&"record.txt"ForAppendAs#1Write#1,Format(Now(),"YY-MM-DDhh:mm:ss")&"“"&Usrs(Index)&"”上线。"Close#1ElseText1.Text=Left(dx,InStr(dx,"
10、"))&Format(Now(),"YY-MM-DDhh:mm:ss")&vbCrLf&Usrs(Index)&"☆说:"&Right(dx,Len(dx)-InStr(dx,"
11、"))Text2.Text=For
12、mat(Now(),"YY-MM-DDhh:mm:ss")&"“"&Usrs(Index)&"”对“"&Replace(dx,"
13、","”说:"&vbCrLf)&vbCrLf&Text2.TextOpenApp.Path&"record.txt"ForAppendAs#1Write#1,Format(Now(),"YY-MM-DDhh:mm:ss")&"“"&Usrs(Index)&"”对“"&Replace(dx,"
14、","”说:"&vbCrLf)Close#1EndIfEndSubPrivateSubSckServer_Close(IndexAsInteger)Dimi%On
15、ErrorResumeNextIfSckServer(Index).State<>sckClosedThenSckServer(Index).CloseConnectState(Index)=FreeText2.Text=Format(Now(),"YY-MM-DDhh:mm:ss")&"“"&Usrs(Index)&"”下线。"&vbCrLf&Text2.TextOpenApp.Path&"record.txt"ForAppendAs#1Write#1,Forma