PDA

Click to See Complete Forum and Search --> : socket connection


Hui
Nov 14th, 1999, 04:08 PM
Hi ,

Can anyone help me with the code for a TCP/IP connection to a server?
I know the host and port, but how do I do it in VB??Please advice ...

Serge
Nov 14th, 1999, 06:13 PM
What do you mean by Connection to a Server. Is it a Database Server, File Server, Printer Server...?



------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

JorgeLedo
Nov 14th, 1999, 06:45 PM
Try using this (In MSDN):

To create a TCP server

Create a new Standard EXE project.


Change the name of the default form to frmServer.


Change the caption of the form to "TCP Server."


Draw a Winsock control on the form and change its name to tcpServer.


Add two TextBox controls to the form. Name the first txtSendData, and the second txtOutput.


Add the code below to the form.
Private Sub Form_Load()
' Set the LocalPort property to an integer.
' Then invoke the Listen method.
tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.Show ' Show the client form.
End Sub

Private Sub tcpServer_ConnectionRequest _
(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.
If tcpServer.State <> sckClosed Then _
tcpServer.Close
' Accept the request with the requestID
' parameter.
tcpServer.Accept requestID
End Sub

Private Sub txtSendData_Change()
' The TextBox control named txtSendData
' contains the data to be sent. Whenever the user
' types into the textbox, the string is sent
' using the SendData method.
tcpServer.SendData txtSendData.Text
End Sub

Private Sub tcpServer_DataArrival _
(ByVal bytesTotal As Long)
' Declare a variable for the incoming data.
' Invoke the GetData method and set the Text
' property of a TextBox named txtOutput to
' the data.
Dim strData As String
tcpServer.GetData strData
txtOutput.Text = strData
End Sub

The procedures above create a simple server application. However, to complete the scenario, you must also create a client application.

To create a TCP client

Add a new form to the project, and name it frmClient.


Change the caption of the form to TCP Client.


Add a Winsock control to the form and name it tcpClient.


Add two TextBox controls to frmClient. Name the first txtSend, and the second txtOutput.


Draw a CommandButton control on the form and name it cmdConnect.


Change the caption of the CommandButton control to Connect.


Add the code below to the form.
Important Be sure to change the value of the RemoteHost property to the friendly name of your computer.

Private Sub Form_Load()
' The name of the Winsock control is tcpClient.
' Note: to specify a remote host, you can use
' either the IP address (ex: "121.111.1.1") or
' the computer's "friendly" name, as shown here.
tcpClient.RemoteHost = "RemoteComputerName"
tcpClient.RemotePort = 1001
End Sub

Private Sub cmdConnect_Click()
' Invoke the Connect method to initiate a
' connection.
tcpClient.Connect
End Sub

Private Sub txtSendData_Change()
tcpClient.SendData txtSend.Text
End Sub

Private Sub tcpClient_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
txtOutput.Text = strData
End Sub

The code above creates a simple client-server application. To try the two together, run the project, and click Connect. Then type text into the txtSendData TextBox on either form, and the same text will appear in the txtOutput TextBox on the other form.



------------------
Jorge Ledo
j_ledo@hotmail.com
Portugal

May 3rd, 2000, 02:51 AM
Hi, dunno how I've stumbled across this thread, but I was wondering, if anyone knows how to deal with sending and accepting file using Winsock..