Write online chat programs with Winsock control in VB5

xiaoxiao2021-03-06  64

[Return] Software World 1999 No. 2

Write online chat programs with Winsock control in VB5

Li Yining 红

Sockets is proposed on the UNIX system, which is mainly used for local communication, but it is quickly applied to the C / S system. Microsoft Corporation created a Winsock control for Windows interfaces, which is fully compatible with Sockets. The Winsock control is invisible to the user, which provides a convenient way to access TCP and UDP network services. Microsoft Access, Visual Basic, Visual C or Visual FoxPro developers can use it. To write customer or server applications, you don't have to know the details of TCP or call low Winsock APIs. By setting the properties of the control and calling its method to easily connect to a remote machine and can also exchange data two-way. Next, you will write an online chat program using the Winsock control in VB5. 1. Basics and Selection of Network Communication Protocol 1.1 TCP (Data Transfer Protocol) Basic Data Transfer Protocol Allows to create and maintain connections to remote computers. Connecting two computers can perform data transmission with each other. If you create a client application, you must know the server computer name or IP address (RemoteHost property), and know the port of "listening" (RemotePort property), then call the Connect method. If you create a server application, you should set a listening port (LocalPort property) and call the listen method. ConnectionRequest events occur when the client computer needs to connect. In order to complete the connection, you can call the AcceptRequest method within the ConnectionRequest event. After establishing a connection, any one of the computers can send and receive data. In order to send data, the SendData method can be called. DataArrival events occur when receiving data. Call the getData method within the DataArrival event to get the data. 1.2 UDP (User Data Library Protocol) Basic User Data Data Protocol (UDP) is a connectionless protocol. Unlike TCP, the computer does not establish a connection. In addition, the UDP application can be a client, or a server. In order to transfer data, first set the localPort property of the client computer. The server computer then sets the RemoteHost to the client computer's Internet address and set the RemotePort property to the same port as the client computer's localport property, and invoke the SendData method to start sending information. Thus, the client computer uses the getData method in the DataArrival event to obtain information that has been sent. 1.3 Selecting the Communication Protocol When you use the Winsock control, you first need to consider what protocol used. The protocols that can be used include TCP and UDP. An important difference between the two protocols is their connection status: The TCP protocol is a connection protocol that can be compared to the telephone system. The user must establish a connection before starting the data transfer. The UDP protocol is a connectionless protocol, and the transfer between the two computers is similar to whether the message is sent from a computer to another, but there is no clear connection between the two. In addition, the maximum amount of data of single transmission depends on the specific network. Which protocol to choose is usually determined by the application you need to create. The following issues will help choose the appropriate protocol: 1. Does the application need to get a confirmation information of the client or server when transmitting and receiving data? If you need, use the TCP protocol to establish a clear connection before sending the data.

2. Is the amount of data specifically large (such as image and sound files)? After the connection is established, the TCP protocol will maintain the connection and ensure the integrity of the data. However, this connection requires more computing resources, thus comparing "expensive". 3. Data transmission is intermittent or within a session? For example, if an application needs to notify a computer while a task is completed, the UDP protocol is more appropriate. The UDP protocol is suitable for a small amount of data. * The selection of the Multimedia Information Bureau of Wuhan City Telecommunications Communication Agreement is achieved by setting the Winsock's Protocol property. The following selection TCP Communication Protocols are written online chat programs, and you must know an extremely important parameter - the IP address or computer name of the server side. 2) Determine the name of the computer 1. On the computer's desktop, right-click "Online Neighbors". 2. Select "Properties". 3. Click the "Identification" tab. 4. You can find the name of your computer in the Computer Name box. Determine the IP address of your computer 1. Click Start on the Task Bob. 2. Select "Run". 3. If the server-side operating system is filled in "WiniPCFG" in "Open", if the server-side operating system is "IPConfig" in "Open". 4. Press the "OK" button. The computer name or IP address found above can be used as the value of the REMOTEHOST attribute of Winsock. 3) The State property of the Winsock control. The set value of the State property is: the constant value describes SCKClosed0 default. Turn off Sckopen1 Open Scklistening2 Listening SCKNECTIONPENDING3 Connection SchResolvingHost4 Recognizes Host SckhostResolved5 recognizes host SCKNECTING6 is connected SCKCONNECTED7 The connection SCKCONNECTED 7 connected SCKCLOSING8 Single staff is closing connection SCKERROR9 error

The following is mainly used to use the value of the two state properties of SckClosed.sckconnected. 4) The program of the online chat program 4.1 The role of the server side in the program. There is no direct communication between the two WINSOCK controls of the server side, while SckServer1 and Sckclient2 and SckServer2 and Sckclient1 cannot communicate directly. This is to say that if SckClient1 issues information to SCKCLIENT2, the information is first accepted by SckServer1, and SckServer1 then transmits the information processing section of the information, and the information processing portion will pass the handled information to SCKSERVER2, and then transmit it to SCKCLIENT2. vice versa. Then what work is on the server-side information processing section? 1. Do some limits the number of channels. 2. Passages that have been closed after use, must be reused to save resources. 3. Must make different processing on the transmitted packet information to make different processing. Talking about the problem of online information. The information on the Internet is very different, how do you open different information? Different information can be distinguished by unlocked the header of the data. There are two ways to chat online: the first, in a broadcast method; second, a point-to-point manner. Broadcasting methods, all customers can receive information from a customer. Point-to-point's way, I want to say "whisper", a pair of customers specializes in a conversation "small house", other customers can't "listen" to their conversation. In the following program, it will see how to use the data to distinguish whether the user is to talk in a broadcast method or a point-to-point manner (PT ", the header" PT ", and the broadcast mode has no header). 4.2 Client Programs 1. Create a new project in the client name "ClientPRJ" 2. Name the default form to frmclient. 3. Change the title of the form to "Client". 4. Add a WINSOCK control in the form and name it TCPClient. 5. Add a ListBox control to frmClient. Name it LSTRECEIVE. 6. Add a TextBox control in FRMCLIENT. Name it TXTSEND. 7. Put two CommandButton controls on the form and name it CmdConnect and cmdsent. 8. Change the title of the CMDConnect control to Connect, change the title of the CMDSent control to SENT. 9. Add the following code to the form. Private Sub cmdConnect_Click () On Error GoTo ErrorPro sckClient.Connect Exit Sub ErrorPro: MsgBox "! Server is not on or network error" End End Sub Private Sub cmdSent_Click () sckClient.SendData txtSent.Text End Sub Private Sub Form_Load () 'RemoteComputerName to The server name or IP address of the server.

sckClient.RemoteHost = "RemoteComputerName" sckClient.RemotePort = 1000 End Sub Private Sub sckClient_Close () MsgBox "server channel is closed!" End End Sub Private Sub sckClient_Connect () MsgBox "connection success!" cmdConnect.Enabled = False End Sub Private Sub sckClient_DataArrival (ByVal bytesTotal As Long) Dim s As String sckClient.GetData s lstReceive.AddItem s End Sub Private Sub sckClient_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 Sckclient.close CMDConnect.Enabled = True End Sub 4.3 Server-side programs 1. Create a new project in the server side name "ServerPRJ". 2. Name the default form "FRMSERVER". 3. Add a ListBox control in the form to name "LSTRECEIVE". 4. Add three WINSOCK controls to the form, named "Scklisten", Sckbusy, and SckServer, and set "SCKSERVER" to 0. 5. Add the following code in the form..

'The maximum number of channels Private MaxChan As Integer Private Sub Form_Load () Dim i As Integer MaxChan = 10 For i = 1 To MaxChan - 1 Load sckServer (i) Next i sckListen.LocalPort = 1000 sckListen.Listen End Sub Private Sub sckBusy_Close () sckBusy.Close End Sub Private Sub sckBusy_DataArrival (ByVal bytesTotal As Long) sckBusy.SendData "server is busy, please reattach it later!" DoEvents End Sub Private Sub sckListen_ConnectionRequest (ByVal requestID As Long) Dim i As Integer 'which is determined by the Winsock Accept request for i = 0 to maxchan - 1 if Sckserver (i) .State = 0 THEN EXIT for end if Next i if Sckserver (i) .State = 0 Then Sckserver (i) .accept requestid exit subside if f ' Winsock is running, is accepted by special "busy" Winsock, so as not to get the user's request to get the response Sckbusy.Close Sckbusy.Accept Requestid End Sub Private Sub Scklisten_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 scklisten.close scklisten.localport = 1000 Scklisten.li sten End Sub Private Sub sckServer_Close (Index As Integer) sckServer (Index) .Close End Sub Private Sub sckServer_DataArrival (Index As Integer, ByVal bytesTotal As Long) Dim s As String Dim i As Integer sckServer (Index) .GetData s If UCase ( Left (Trim (s), 2)) = "Pt" THEN 'Judging whether it is a private message, point-to-point method if ISNUMERIC (MID (Trim (S), 3, 1)) THEN I = MID (Trim (S), 3, 1) Sckserver (i) .senddata "Channel" & index & "& right (trim (s)) - 3) DOEVENTS END IF ELSE 'Broadcasting method for i = 0 To MaxChan - 1' Use the Winsock's State property to all clients on the server IF Sckserver (i) .State = 7 Then Sckserver (i) .senddata "

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

New Post(0)