VB Socket Programming Introduction __ Programming

Source: Internet
Author: User
Using Winsock control to realize LAN communication
For the network users of the programming enthusiasts, if you can make up a local area network communication program, then this will be how wonderful. However, if you want to start from scratch entirely by writing a program for communication, it is not easy to have a deeper understanding of the relevant network protocols and other lower-level technologies. And now with the Winsock control, everything is different, it has encapsulated all the cumbersome technical details for you, and provides a convenient way to access TCP and UDP network services. You can easily connect to a remote computer by setting the control's properties and calling its methods, and you'll also be able to exchange data in both directions without requiring you to know the details of TCP or to invoke low-level Winsock APIs.
The Winsock control can be used by developers in Microsoft Acess, visual basic,visual C + +, or Visual FoxPro. This article takes the Visual Basic 6 Enterprise Edition as the development environment to introduce to you the preliminary application of the Winsock control.
The Winsock control can use two protocols: the TCP protocol and the UDP protocol, which are described separately below.
The TCP protocol, the data Transfer Protocol, allows you to create and maintain a connection to a remote computer so that it can transmit data to each other. Using TCP protocol communication, you must establish client applications and server applications separately.
When you create a client application, you must know the server computer name or its IP address (stored in the RemoteHost property), and the port on which the server computer listens (in the RemotePort attribute), and then call the Connect method.
When creating a server application, you should set up a listening port (LocalPort property) and call the Listen method accordingly. The Connectionrequest event occurs when the client needs to connect (connect). In order to complete the connection, you can invoke the Accept method in the Connectionrequest event. When a connection is established, either computer can send or receive data from the other side. If you want to send the data, you need to call the SendData method. When the data is received, the DataArrival event occurs, and the GetData method in the DataArrival event is invoked to obtain the data transmitted by the other.
Said so much, we may still do not understand, let me use the program to elaborate.
If there are only two computers, it is very easy. If the machine is a client, B machine is a server, and its IP is 192.192.192.1, the receiving port is 1200 (optionally an unused port). First in a machine client program to add a Winsock control, named Sckconnect, and set its properties: remotehost= "192.192.192.1", (that is, a machine IP address), remoteport=1200 (that is, a machine listening port) , and then in the B-Machine server program if a Winsock control named Sckserver (0), its localport=1200,
In the B-Machine server program Form_Load () join
Sckserver (0). Bind sckserver (0). LocalPort ' Bind with local port
Sckserver (0). Listern ' Listening
If you want to transfer data, the two machines must first establish a connection. The procedure for establishing the connection is as follows:
A machine client must first request a connection
Sckconnect.connect Sckconnect. RemoteHost, Sckconnect. RemotePort
Executing this sentence triggers the Connectrequest event in the server program, in which you decide whether to establish a connection with the following code:
Private Sub Sckserver_connectionrequest (index as Integer,byval RequestID as Long)
If Sckserver.count=1 Then
Load Sckserver (1)
Sckserver (1). Accept RequestID
End If
End Sub
When the connection is established, the SendData method can be used to transmit the data in a machine or a B machine. For example, if a confidential string is sent by a secret, simply add it to the code:
Sckconnect. SendData string
A machine transmits the data, will trigger the DataArrival event of the B machine, in its process can receive the transmitted data by the GetData method:
Private Sub Sckserver_dataarrival (Index as Integer,byval bytestotal as Long)
Dim sdata As String
Sckserver (1). GetData sdata,vbstring
End Sub
Finally, don't forget to close the Winsock control before closing the program
Privat Sub Form_Unload (Cancel As Integer)
If Sckconnect.state <> sckclosed Then
Sckconnect.close
End If
End Sub

This is only the simplest case, if there are more than one computer, it is a little more complex, the client program can not make changes, and the server-side program needs to be slightly changed:
Private Sub Sckserver_connectrequest (Index as Integer,byval RequestID as Long)
Dim sip As String
Dim I As Integer
Sip=sckserver (0). Remotehostip ' Get the IP address of the logged-in person
I=1
Do While I <=sckserver.ubound ' check if there is a record of this address
If Sckserver (I). Remotehostip=sip then ' If any, you don't have to load the new control
Sckserver (I). Accept RequestID
Exit Sub
End If
I=i+1
Loop
Load Sckserver (I) ' Otherwise, load the new control
Scksrver (I). Accept RequestID
End Sub
Note: The above information conversation actually occurs between the client and the server, if you want to make a chat room, everyone's words can be "heard", that is, in the server-side DataArrival event, the received data from the client, forward to all clients.
The code for the circular forwarding of information is as follows:
For I=1 to Sckserver.count
If Sckserver (I). State <> Sckclosed Then
Sckserver (I). SendData sdata
End If
Next I

So we can make our own communication software.

However, I do not know that no, the above procedures need to have a computer as a server, but if our local area network is not a computer can be often open, that is, if the server-side program is not running, other client programs can not be able to communicate. This may be a frequent occurrence! At least, that's how I use the local area network. Can't we enjoy the fun of LAN communication?
Don't worry, remember, our Winsock control has another lead: the UDP protocol.
The UDP protocol, also known as the User Data packet protocol, is a connectionless protocol. What is a connectionless protocol. This means that when connecting with this protocol, you do not have to be like the TCP protocol: Require server-side listening, client-side request connections, and server-side connections to communicate. In addition, a UDP application can be either a client or a server program, rather than having to establish a client program and a server program separately from the TCP application.
The following is a brief description of the UDP protocol communication process: in the UDP protocol, in order to transmit data in both machines, it is necessary to set up the LocalPort properties of the two machines respectively; the RemoteHost property of the machine is set to the IP address of the B machine. The RemotePort property is set to the LocalPort property value of the B machine, at this time the SendData method can transmit the data, B machine also uses the GetData method of the DataArrival event to obtain the information of the machine sent to the B machine. If you want to send the data to Machine B machine, just imitate the above process settings.
UDP protocol to transmit information than the TCP protocol is much simpler, it does not need to listen (LISTEN), no need to request connection (connect), as we usually send letters, as long as we write a good address and the name of the addressee and sent out. We can use this to write a local area network Information transfer program, the following is a brief description of the following program to implement the functions and their basic ideas:
First of all, we would like to have the program's icon appear in system tray and not appear in the taskbar. This is not too easy if you are programmed to implement it, fortunately VB CD in the Common\tools\vb\unsupport\systemtray has a ready-made program, we just compile it into systray.ocx control, You can then add this control when you write your own program. The use of the method is very simple, it has been defined by the mouse click, double-click, and so on, you can only write the appropriate mouse events, here no longer said.
The key of the program is: UDP protocol in the communication to know each other's IP and port, how to achieve this. The simplest way is to create a configuration file, which placed the local area network of each computer's name, IP and port, in the program initialization read out all information, in the program as long as you know who to communicate, read out its corresponding IP and port can.
We know the IP and port of each computer, but how do we know if other computers are online, or else they can't get the message out? We can put this program in the boot menu, let it start automatically, and minimized, in the lower right corner of the window in the system tray. When the program is first run, it automatically sends a message to all the IP it knows from the configuration file: "I'm coming." "If there is a computer online, it will automatically return a message:" Welcome. , so that the two machines communicate successfully, they will add each other's name to their own list of speakers; If a computer shuts down, the program will automatically say goodbye to everyone before exiting: "Goodbye." , the computer that receives this information automatically removes the name of the computer that sent the message from its own list of people who can communicate. In this way, if someone is not on the line, you will not be able to send the message to it, if other than you, other people do not boot, then your list of senders will not be anyone. and other people just a line, will automatically go to your place "registration", other people just offline, will automatically go to you "farewell", you can know whether others are using the computer, you can even use this program to count other people's Daily check-in time, not bad.
OK, the basic model of a LAN communication program is already there, it's not complicated. Let's get this over with and enjoy the fun of chatting with your own program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.