Socket Programming with VB

zhaozj2021-02-08  248

Socket Programming with VB

We involved Winsock programming in the study of Yantai project, and made several models after a longer study. This article is some experience in my work, please refer to you.

1. Introduction

In various network programming interfaces under Windows, Windows Sockets stands out, because the INDOWS Sockets specification is a set of open, supporting a network programming interface under Windows of multiple protocols. Use the Winsock control to establish a connection with the remote computer and data exchange via the User Data Network Protocol (UDP) or Transmission Control Protocol (TCP). Both protocols can be used to create clients and server applications.

The Windows Sockets specification is built on the Beckley socket model. This model is now the standard of TCP / IP network. The so-called socket is one end of communication. The socket is usually exchanged with sockets in the same domain (data exchange may also pass the boundaries of the domain, but at this time you must perform some explanation). There are currently two sockets, namely junction interfaces, and data sets. The junction interface provides two-way, orderly, no duplicate data stream services. The data setup interface supports the two-way data stream, but it is not guaranteed to be reliable, orderly, and no repetition. We only use the stream socket in programming, unordered, less reliable, reliable, reliability, can not meet the requirements of the system's accurate and efficient data transmission.

2. Client / server model

An example of the most commonly used application when establishing a distributed application is a client / server model. In this scenario, the client application requests the server, and the server program requests the service response to the client's request. The cornerstone of communication is a socket, all requests, and responding work is done by the socket.

3. Set of interface network programming principles

The stream socket defines a reliable-oriented service that implements no error-free repetition sequential data transmission.

Using a connection-oriented socket programming, the process can be represented by the following figure:

Server Socket Client Socket4. Peer- To-Peer Communication Program is now a specific program. This program is written in VB because VB makes sockets into controls, which is very convenient to use. This is a point-to-point communication program, and the function is to communicate both parties duplex transmission strings. This program blocks a fact, and the C / S model, but you must start the server when the program is run.

Service-Terminal:

To create a new project, add the Microsoft Winsock Control control, place the control below, mainly five: Two text boxes are used to display the received data and enter the sending data; a CommandBotton is sent; a small icon display is The join (connected to Visible = true); the most important thing is the Winsock control, all things are dry, and never displayed (unknown silently).

Here is the code, the comment is clearer:

Option expedition

DIM IsconnectionOpen as Boolean

Private submmand1_click ()

If isconnectionopen = false kil

Msgbox "Unconnected!" 'If not established, you will pop up such a dialog.

Else

Winsock1.senddata text2.text

'Socket is like this, it is very simple.

END IF

End Sub

Private sub flow_load ()

Winsock1.localport = 1111

Winsock1.listen 'Socket lands on the program 1111Image1.visible = false

ISconnectionOpen = false 'started to be unpacked

End Sub

Private sub winsock1_close () 'other Socket Turn off this event

Winsock1.close 'Close yourself

End 'That will close the program.

End Sub

Private Sub Winsock1_ConnectionRequest (Byval Requestid As Long "receives the other party join

'Request to trigger this event

IF Winsock1.State <> SCKCLOSED THEN WINSOCK1.CLOSE 'Make Socket in Receive

'Keep the closed state before the join request

Winsock1.accept requestid 'Winsock1 receives the joint request, with the client socket

'Establishing join

IsconnectionOpen = TRUE

Image1.visible = true 'Now is good

End Sub

Private Sub Winsock1_DataArrival (Byval Bytestotal As Long "received data triggering this event

Dim Str As String

Winsock1.GetData str 'When you receive data, Winsock1 writes data to cache Str

TEXT1.TEXT = TEXT1.TEXT STR CHR (13) CHR (10)

TEXT1.SELSTART = LEN (Text1.Text)

'Display the received data in the text box

End Sub

Client:

A new project is also established, and the Winsock control is added. The layout is as follows, it is slightly more than the server.

Miscellaneous, because the client issues a join request, so two text boxes are used to enter the address and port of the server.

Winsock control, a CommandBotton is used to join:

Code:

Option expedition

DIM IsconnectionOpen as Boolean

Private submmand1_click () 'join button

Winsock1.Remotehost = text1.text

Winsock1.remotEport = Clng (Text2.Text) 'Fill in the other party address of the Socket

'And the other party port number

Winsock1.connect 'socket sends a join request, when the connection is successful, Winsock

'Connect event (look down)

End Sub

Private sub command2_click () 'Send button

If isconnectionopen = false kil

Msgbox "Not established joint!"

Else

Winsock1.senddata text3.text

End if 'is the same as the server

End Sub

Private sub flow_load ()

TEXT1.TEXT = "sjn"

Text2.text = "1111" "The two text boxes first display the address of the server when the program starts.

'And port slogans, it doesn't matter, anyway, you can modify it.

Image1.visible = false

IsconnectionOpen = false 'unconnected

End Sub

Private Sub Winsock1_close () Winsock1.close

End 'is the same as the server

End Sub

Private sub winsock1_connect () 'triggers when connecting

IsconnectionOpen = TRUE

Image1.visible = true 'join success

End Sub

Private sub winsock1_dataarrival (byval bytestotal as long)

Dim Str As String

Winsock1.Getdata STR

TEXT4.TEXT = TEXT4.TEXT STR CHR (13) CHR (10)

TEXT4.SELSTART = LEN (Text4.Text) 'Like the server

End Sub

At this point, the server is established with the client.

When running, the server and the client can be placed on both machines, or it can be refunded on one machine. It doesn't matter.

Start the server first, run the client, now you can say hello.

The process of two programs: (in fact, the previous chart has been displayed, but it is necessary to come again)

server:

Number:

K, now we have built a simple C / S communication model, it is really simple. Further, we can also add a lot of functions, for example, through the .accept method, and the Winsock control to make an array, the server can establish a connection with multiple clients to implement a simple chat room.

About Winsock Control:

1. The client's socket must accurately fill in the address and listening ports of the two parties, otherwise, it can not be able to join.

2. Both communications are done by the Socket communication entity, and the protocol generally adopts the TCP protocol to ensure accurate transmission.

3. Specific events and methods See the program and MSDN-> Directory -> Visual Basic Documentation -> Using the VB-> Part Tool Guide -> Using ActiveX Controls -> Using the Winsock Control (quite detailed inside)

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

New Post(0)