Realiser une liaison client serveur sous vb

Description

C avec ce code que j'ai commencer a utiliser winsock, il sort tout droit de mon tp de reseaux et doit donc etre comprehensible pour tout le monde puisque j'ai compris ( ou presque).

Voici le tutorial complet, mais petite difficulte, il est en anglais ( ca reste assez simple).

Visual Basic Client/Server Programming Tutorial
· Enter the VB6 environment
· Open New Standard Project
We require an extra control within this form

· Click on the PROJECT on the menu bar
· Components
· Search for Microsoft Winsock Control
· Tick the box
· Click on APPLY
· CLOSE

Create a Client-Server Link making use of TCP protocol
· Single Click on the Winsock control within the toolbox and position anywhere on the form
· Go to the Winsock?s property window and set the protocol to sckTCPProtocol
· Change the Name of the control to tcpServer



· Return to the form and insert two text boxes
· Rename these boxes txtOutput and txtInput
· Insert a label
· Insert a timer
o Single click on the timer control and set the property interval = 1000
· Insert a command button
· Double click anywhere on the form, but not on any of the controls that you have just inserted
· Insert the following code for form load

Private Sub Form_Load()
tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.show
End Sub

· Below this subroutine include the following:
Private Sub tcpServer_ConnectionRequest(byVal requestID As long)
If tcpServer.State <> sckclosed Then tcpServer.Close
tcpServer.Accept requestID
End Sub

Private Sub txtOutput_Change()
tcpServer.SendData txtOutput.Text
End Sub

Private Sub tcpServer_DataArrival (ByVal bytesTotal As Long)
Dim strData As String
tcpServer.GetData strData
txtInput.Text = strData
End Sub

Private Sub Command1_Click()
Tcpserver.close
End sub

Private Sub Timer1_timer()
Label1.caption = tcpserver.state
If tcpserver.state = 0 then tcpserver.listen
End sub

Now save the form and project together in a folder of your choice
We now need to create the client form
· Enter the project menu, and add a new form
· Change the name of this form to frmClient
· Click on the winsock control in the toolbox and insert into this form.
· Rename the control as tcpClient
· Add two text boxes to the form and rename them txtOutput and txtInput
· Insert a command button
· Insert another command button and rename it cmdConnect
o Change the caption property on this button to CONNECT
· Insert a label
· Inset a timer control and again set the interval property to 1000

Before we add the code for the client, we need to know "Name" of the remote server machine. In our example the client and server processes will be running locally. To find the name of our machine we need to look within the control panel network settings where the name of the machine can be found. If we want to, we can insert the dotted decimal IP address of the machine instead
· Now we need to add code to the client form
Private Sub Form_Load()
tcpclient.RemoteHost = "name of remote computer" '<< INSERT NAME OF REMOTE COMPUTER
tcpclient.RemotePort = 1001
End Sub

Private Sub cmdConnect_Click()
tcpclient.Connect
End Sub

Private Sub txtOutput_Change()
tcpClient.SendData txtOutput.text
End Sub

Private Sub tcpClient_DataArrival(ByVal bytestotal As Long)
Dim strData as String
tcpclient.GetData strData
txtInput.Text = strData
End Sub

Private Sub Command1_Click()
Tcpclient.close
End sub

Private Sub timer1_timer()
Label1.caption = tcpclient.state
End sub

· Now save this form
· Now click on the start/play button
· Click on the command button to create the TCP link
· Move the forms so that you can see both of them (this can be done prior to running the application)
· Now type information into the text box for sending data on the client and it should appear in the server receive box
· Now repeat the above process, so that the server data is sent to the client
· Click on the server command button to close the link
· Click on the client button to close the link
· When this has been achieved, move the server application code to another machine, and get the client code to talk to it across the network


Having completed the above:
· Insert an extra button on the client form
· Only send the text to the server when a full string has been inserted on the client (click on the new button to send it)
· On the server when the string of data arrives
· Check if the string is CLOSE ? if yes then close down the server link
· This will remove the need for the close button on the server

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.