I. Delphi and Socket computer networks are composed of a series of network communication protocols, where the core protocol is the TCP / IP and UDP protocol of the transport layer. TCP is a connection, communication between communication, is like the current telephone line, using Telnet to log in to BBS, using TCP protocol; UDP is not connected, communication between the communication, the browser accesss the Internet The HTTP protocol is based on the UDP protocol. TCP and UDP protocols are very complicated, especially TCP protocols, in order to ensure the correctness and effectiveness of network transmission, a series of complex error correction and sorting must be performed. Socket is a socket specification built on the transport layer protocol (mainly TCP and UDP), which was originally proposed by the University of California, which defines the norms of communication between two computers (also a programming specification). If two computers are communicating using a "channel", then the ends of this "channel" are two sockets. Socket shields the difference between the underlying communication software and the specific operating system, so that any two communication between the TCP protocol software and the computer that implements the socket specification is possible. Microsoft's Windows Socket Specification (WINSOCK) extends to Berkley's socket specification, using standard socket methods, can communicate with Socket on any platform; use its expansion, can be more efficiently implemented on a Windows platform Communication between the computer. In Delphi, its underlying Socket should also be Windows Socket. Socket reduces the difficulty of writing computer communication software, but always is quite complicated (this will be told behind); Inprise has a valid package for Windows Socket in Delphi, so that users can be very convenient Write a network communication program. Below us of instance interpretation how to write a communication program using Socket in Delphi. Second, write the Socket communication program with Delphi. Below is a simple Socket communication program, where the client and the server are the same program. When the client (server) enters a paragraph in a MEMO1 and then knocks enter the carriage return, this paragraph can be displayed in the server (client In Memo2, it was also established. The specific steps are as follows: 1. Create a new form, any name, not set to Chatform; put on a MainMenu (in the Standard Bar), establish the ListenItem, ConnectItem, Disconnect, and EXIT menu items; select TSERVERSOCKET, TclientSocket Add to CHATFORM, where the name of the TclientSocket is set to ClientSocket, the port is set to 1025, the default Active is false; set the TSERVERSOCKET name to Serversocket, Port is set to 1025, the default Active is false, the other unchanged; Put two MEMO, one named Memo1, and another named MEMO2, where Memo2's color is set to gray because it is mainly used to display the other party's input. Let's explain the reason below. 2, double click on ListemItem.
Writing the following code: procedure TChatForm.ListenItemClick (Sender: TObject); beginListenItem.Checked: = not ListenItem.Checked; if ListenItem.Checked thenbeginClientSocket.Active: = False; ServerSocket.Active: = True; endelsebeginif ServerSocket.Active thenServerSocket.Active : = False; end; END; This block is described as follows: When the user selects ListemItem, the ListenItem is reversed, if selected, the reader is in the Listen state, the reader wants to understand: Listen is Socket as a server Method, if in Listen, Serversocket is set to active; otherwise, cancel Listen, turn off ServerSocket. In fact, only the user selects the menu item, indicating that the program is used as a Server. Conversely, if the user selects ConnectItem, it is inevitably used as a client. 3, double-click ConnectItem, type the following code. procedure TChatForm.ConnectItemClick (Sender: TObject); beginif ClientSocket.Active then ClientSocket.Active: = False; if InputQuery ( 'Computer to connect to', 'Address Name:', Server) thenif Length (Server)> 0 thenwith ClientSocket dobeginHost : = Server; active: = true; listenItem.checked: = false; end; end; The main function of this program is to set the application for the client when the user selects the ConnectItem menu item, and pops up the INPUT box to allow the user to enter the server. the address of. This is why we don't start to fix the Host of ClientSocket, so that users can dynamically connect different servers. The reader needs to understand that the host address is just a attribute when Socket has a client. Socket is "general" as a server, because it is bound to this unit. 4, the method memo1 keydown written in the following code: procedure TChatForm.Memo1KeyDown (Sender: TObject; var Key: Word; Shift: TShiftState); beginif Key = VK_Return thenif IsServer thenServerSocket.Socket.Connections [0] .SendText (Memo1 .LINES [Memo1.Lines.count - 1]) ElseclientSocket.socket.sendtext (Memo1.Lines [Memo1.Lines.Count - 1]); END; The role of this section is obvious, that is, start message. If it is Server, it only sent a message to the first client. Since a server can connect multiple clients, each connection with the client is maintained by a socket, so serversocket.socket.connnections arrays Stored is a Socket that is connected to the client.
In the standard socket, the server side's Socket acquires the Socket connected to the client through the ACCEPT () method, and the method of sending, accepting the message is Send (Sendto) and Recvfrom), Delphi. Package. 5. Brief introduction to the rest of the code. Procedure tchatform.serversocketAccept (SENDER: TOBJECT; SOCKET: TCUSTOMWINSOCKET); BeginisServer: = true; end; serverSocket ACCEPT method, when the client is connected, through its parameters, it can be considered, it is after the standard accept method Executed because there is a TCUSTomWinsocket this parameter type, it should be the return value of the standard Server Socket. Procedure tchatform.clientsocketread (Sender: Tobject; socket: tcustomwinsocket); beginMemo2.Lines.Add (socket.ReceirtExT);
Procedure tchatform.ServersocketClientRead (Snder: Tobject; socket: tcustomwinsocket); beginMemo2.Lines.add (socket.ReceirtExT); END; these two codes are triggered by server side and clients when receiving the other party, by Delphi triggered The role is to display the received message in MEMO2. Among them, the socket in ClientSocketRead is actually Socket itself, and Socket in ServersocketClientRead is actually a Socket in Serversocket.Socket.Connection []. However, in Delphi, the SOCKET of the server is effective in encapsulation. procedure TChatForm.ServerSocketClientConnect (Sender: TObject; Socket: TCustomWinSocket); beginMemo2.Lines.Clear; end; procedure TChatForm.ClientSocketDisconnect (Sender: TObject; Socket: TCustomWinSocket); beginListenItemClick (nil); end; these two relatively simple. Where ServersocketClientConnect is triggered when Serversocket receives a new connection. ClientSocketDisconnect triggers when ClientSocket issues disconncet.
procedure TChatForm.Exit1Click (Sender: TObject); beginServerSocket.Close; ClientSocket.Close; Close; end; procedure TChatForm.Disconnect1Click (Sender: TObject); beginClientSocket.Active: = False; ServerSocket.Active: = True; end; a first The segment is to close the app. In the standard socket, each socket is turned off, you must call the ClosSocket () method, otherwise the system does not release resources. In Serversockt.Close and ClientSocket.Close, the system is sure to call the CloseSocket () method. Third, the standard socket and the Socket in Delphi. The standard socket application framework is as follows: Server: socket () [New a socket] - bind () [with server address Bond] - Listen () --Accept () - block wait - read ( ) [Accept the message, in the Windows platform, the method is Send (TCP), or Sendto (UDP)] - Processing Service Request - Write () [Send Message, in the Windows Platform, the method is Send (TCP), Or for Sendto (UDP). The Client party is relatively simple: socket () - Connect () [Connect a specific server through a certain port, which is connected to the server] - Write () - read (). Socket can be TCP based on UDP, and Socket is even built in other protocols, such as IPX / SPX, DECNET, etc. When you create a new socket, you can specify the newly built Socket. Bind () is used to confirm the address of the server. If a host has only one IP address, the actually the role of the bond is relatively. Listen () Start listening to the network, accept () is used to accept the connection, and its return value is the Socket that keeps the same with the client. In Delphi, there is a valid package for Socket in Windows.