Results 1 to 4 of 4

Thread: socket connection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 1999
    Posts
    19

    Post

    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 ...

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    What do you mean by Connection to a Server. Is it a Database Server, File Server, Printer Server...?



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

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  3. #3
    Addicted Member
    Join Date
    Oct 1999
    Location
    Oporto, Portugal
    Posts
    134

    Post

    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
    [email protected]
    Portugal

  4. #4
    Guest
    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width