Source: http://www.fenbi360.net/content.aspx? Id = 1100 & t = CT
: Download from the address above.
This is a simple chatProgram(VB6.0), it can achieve online chat between two hosts in the LAN, the program is very simple, only a few dozen lines, but "Sparrow is small, dirty ", it has a general framework of chat programs. We can make some improvements on the basis of it to make good chat software.
Main Control WinsockResolution:
As the main control of this chat program, Winsock. Let's take a look at some of its common attributes, events, and methods. The Winsock control can be used to establish a connection with a remote computer and exchange data through user data reporting protocol (UDP) or Transmission Control Protocol (TCP. Both protocols can be used to create client and server applications. Similar to the timer control, the Winsock Control is invisible during runtime.
Its main purposes include:
1. Create a client application that collects user information and send the collected information to a central server.
2. Create a server application as the data import point of multiple users.
3. Create a chat application.
Winsock has the following attributes:
●In this example, we set the Protocol attribute to sckudpprotocol. This Protocol is generally used for simple data exchange. If we want to write Internet applications, we usually use TCP protocol.
●The remotehost attribute is the address of the remote host. localport and remoteport are the ports of the local host and the remote host respectively. For the customer, this attribute specifies the local port for sending data. For the server, this is the local port used for listening. When setting these two attributes, we must ensure that the port values of the two hosts comply with the following rules, that is, the localport of host 1 is equal to the remoteport value of host 2, similarly, the remoteport of host 1 is equal to the localport value of host 2, so as to ensure normal communication between the two machines.
●The localip attribute is used to return the IP address of the local machine. The format is a string (XXX. XXX) that is added to the IP address ).
In this example, we use the following Winsock methods:
●The bind method is called when a UDP application is created. This is required. The bind method "retains" a local port for the control. For example, if you bind a control to port 1001, other applications cannot use this port to "listen ". This method prevents other applications from using the same port.
●The senddata method is used to send one piece of data to another host. the syntax of this method is: Winsock. senddata [data to be sent]
●Getdata method. When the dataarrival event appears,CodeCall the getdata method to obtain data and store the data in string variables. The syntax is: Winsock. getdata [variable for receiving data]
●Dataarrival event: In this example, we use the dataarrival event. The syntax used by the dataarrival event when new data arrives is object_dataarrival (bytestotal as long) the syntax of the dataarrival event includes the following parts: Object object expression. The value is an object in the "apply to" list. Bytestotal long type, total number of data available.
Note: If you do not obtain all the data in a getdata call, the event does not appear. The event is activated only when new data exists. You can use the bytesreceived attribute to check the available data volume at any time.
This is the simplest chat program, you can improve it and make more practical chat software.