A "server, N client" communication program based on Winsock control (complete original procedure)

zhaozj2021-02-16  57

TCP / IP Group Communication Demo Procedure, in Win98 debugging, please download your own learning test, program size 4K

Download address: http://www.lshdic.com/download/lshdic/vb_winsock.zip

Code Browse:

PRIVATE SUB CHECK3_CLICK () client two open and interrupts the server connection if check3.value = 1 ThenON Error ResMe nextw3.remoteHost = text9.text: w3.remoteport = text10.text: w3.connectif err.Number <> 0 "Connected host address or connection port number error", vbcritical, "can't find the server": check3.value = 0: EXIT SUBELSEIF W3.STATE = 7 THEN W3.SENDDATA "Staff 2 [" & w3.Remotehostip & "Termination Connection, Exit System": Doevents: Text11.Text = "" "w3.closeEnd IFEND SUB

Private subs command1_click () server send data str0 = 0for i = 0 to w1.count - 1DOEventsif W1 (i) .State = 7 THEN W1 (i) .senddata "Enterprise Administrator Announcement:" & Text4.Text: STR0 = STR0 1NEXTIF STR0 = 0 THEN MSGBOX "Unable to Connect Server, Unable to Send Data", Vbcritical, "No User" end SUB

Private submmand2_click () 'Client One Send Data IF W2.State <> 7 THEN MSGBOX "Unconnected Host or Connection Host Work is OK, Unable to Send Data", Vbcritical, "Connection Not Normal": Exit Subw2.senddata "staff 1: "& Text8.Textend Sub

Private Sub Command3_Click () if w3.State <> 7 damGBox "Unconnected host or connection host is working, unable to send data", vbcritical, "connection is not normal": exit subw3.senddata "staff 2:" & text12. Textend Sub

Private Sub Form_Load () 'Turn on the server to listen when the server monitor text1.text = w1 (0) .localip: text5.text = w1 (0) .localip: text9.text = W1 (0) .localipw1 (0) .localport = text2 .Text: w1 (0) .listenend Sub

PRIVATE SUB CHECK1_CLICK () 'On and Off Server IF Check1.Value = 1 Thenw1 (0) .localport = Text2.Text: W1 (0) .listenelsefor I = 0 to w1.count - 1IF W1 (i) .State = 7 THEN W1 (i) .senddata "server to shut down, stop receiving user information": doeventsw1 (i) .closeif i <> 0 THEN UNLOAD W1 (i) NextText3.text = "": text7.text = ": Text11 .Text = "" End IFEND SUBPRIVATE SUB CHECK2_CLICK () 'The client is turned on and interrupts and the server connection if check2.value = 1 kilror resume nextw2.remotehost = text5.text: w2.remoteport = text6.text: w2. Connectif Err.Number <> 0 THEN MSGBOX "Connected Host Address or Connection Port Number Error", vbcritical, "Can't find the server": check2.value = 0: EXIT SUBELSEIF W2.STATE = 7 Then W2.sendData "staff 1 ["& w2.remotehostip &"] Termination connection, exit system ": doevents: text7.text =" "w2.closend ingnd sub

Private Sub Form_Queryunload (Cancel As Integer, UnloadMode As Integer) W3.Close: w2.closefor I = 0 to w1.count - 1DOEventsw1 (i) .closenextendend Sub

Private sub timer1_timer () users = 0for i = 0 to w1.count - 1STR1 = STR1 & W1 (i) .State & "," IF W1 (i) .State = 7 THEN users = users 1NextMe.caption = "host Status: "& Left (str1, len (str1) - 1) &", client one state: "& w2.state &", client two status: "& w3.StateLabel3.caption =" User connection number: " & users & ", TCP / IP communications Group presentation of the original program" & vbCrLf & "author: Storm dance (http://www.lshdic.com)" Text3.SelStart = Len (Text3.Text): Text7.SelStart = Len (text7.text): Text11.selstart = len (text11.text) End Sub

Private sub w1_close (index as integer) if check1.value = 0 THEN 'If the server is shut down, it is turned off for i = 0 to w1.count - 1W1 (i) .closeif i <> 0 THEN UNLOAD W1 (i) NEXTELSEW1 (INDEX) .closeEnd IFEND SubPrivate Sub W1_ConnectionRequest (INDEX AS INTEGER, BYVAL Requestid AS Long "server connected to connection application if w1.count = 111 (w1.count) W1 (w1.count - 1) .closew1 ( W1.count - 1) .accept requestideXit Subend iflen1 = 0for i = 1 to w1.count - 1IF W1 (i) .State = 0 THEN W1 (i) .accept requestid: EXIT SUBNEXTLOAD W1 (W1.count): W1 ( W1.count - 1) .accept RequestidEnd Sub

Private Sub w1_DataArrival (index As Integer, ByVal bytesTotal As Long) 'to the data server Dim w1str As Stringw1 (index) .GetData w1strText3.Text = Text3.Text & w1str & vbCrLfFor i = 0 To w1.Count - 1DoEventsIf w1 (i ) .State = 7 the W1 (i) .senddata w1strnextend sub

Private sub w2_close () client one is about to close the connection w2.closeif check2.value = 1 Ten check2.value = 0nd Sub

Private sub w2_connect () w2.senddata system message: staff 1 successfully launched the Group Communication System "End Sub

Private Sub W2_DataArrival (Byval Bytestotal As Long "client receives data DIM W2STR AS STRINGW2.GETDATA W2STRTEXT7.TEXT = TEXT7.TEXT & W2STR & VBCRLFEND SUB

Private Sub w2_Error (ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) MsgBox "An error occurred, connect to the server fails, it may server is not open Or connect port and address error ", Vbcritical," Error ": check2.value = 0w2.closend Sub

Private sub w3_close () 'Client Second is about to close Connection W3.Close: if check3.value = 1 Then Check3.Value = 0END SUB

Private sub w3_connect () w3.senddata system message: staff 2 successfully launched the Group Communication System "End Sub

Private Sub w3_DataArrival (ByVal bytesTotal As Long) 'client receives two data Dim w3str As Stringw3.GetData w3strText11.Text = Text11.Text & w3str & vbCrLfEnd SubPrivate Sub w3_Error (ByVal Number As Integer, Description As String, ByVal Scode As Long Byval Source AS String, Byval HelpFile As String, Byval HelpContext As Long, CancelDisplay As Boolean Msgbox "Errors, Connection Server Fails, Possible Server Not Opening or Connection Port and Address Error", Vbcritical, "Error": Check3.Value = 0w3.closend Sub

转载请注明原文地址:https://www.9cbs.com/read-24545.html

New Post(0)