vb winsock chat
Results 1 to 2 of 2

Thread: vb winsock chat

  1. #1
    Member
    Join Date
    May 2002
    Posts
    63

    vb winsock chat

    This is simple Vb client and chat Program tutorial.
    First go to project the components and select microsoft winsock control under controls, Then add the winsock component to the form. Im going to make the server first of all.
    Add Winsock1.LocalPort = 4746 and Winsock1.Listen under Private Sub Form_Load() this sets the port that winsock should be listening to connetions from and tells winsock to start listing. You should try and pick a port that noone else uses.
    The first bit should look like this:

    Private Sub Form_Load()

    Winsock1.LocalPort = 4746
    Winsock1.Listen

    End Sub

    Next under Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) place "Winsock1.Close" and "Winsock1.Accept requestID" this closes the winsock and then accepts the connection.
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    Winsock1.Close
    Winsock1.Accept requestID

    End Sub
    To receive text you use the code below which gets the data then places it in a text box.
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim TempData As String

    Winsock1.GetData TempData

    Text1.Text = Trim(Text1.Text) & vbCrLf & TempData
    End Sub

    To send text to the server add a command button and a text box and put this "Winsock1.SendData text2.Text" under the command button, which send the text that isin the text box.

    Here is all of the code:

    Private Sub Command1_Click()
    Winsock1.SendData text2.Text
    End Sub

    Private Sub Form_Load()

    Winsock1.LocalPort = 4746
    Winsock1.Listen

    End Sub

    Private Sub Winsock1_Close()

    Winsock1.Close
    Winsock1.Listen
    End Sub

    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    Winsock1.Close
    Winsock1.Accept requestID


    End Sub

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim TempData As String

    Winsock1.GetData TempData

    Text1.Text = Trim(Text1.Text) & vbCrLf & TempData


    End Sub

    The client,
    First add the componets like in the server, go to project the components and select microsoft winsock control under controls, Then add the winsock component to the form.
    Now add 2 command buttons and 3 text boxes. Under form load place "Winsock1.RemotePort = 4746" and "Winsock1.RemoteHost = text1.text" this sets the port that winsock connects to and tells it that it has to connecct to the ip address that should be placed in text1 .

    Under command1 place "Winsock1.Connect" which tells winsock to connect to the server.
    Now under command2 place "Winsock1.SendData text2.text" which sends the text that is in the text2 to the server.
    Then add:
    "Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim TempData As String

    Winsock1.GetData TempData
    Text3.Text = Trim(Text3.Text) & vbCrLf & TempData

    End Sub
    "
    this retrives the data that the server has sent and places it in text3.
    and that is all there is to it.
    Here is the full code:

    Private Sub Command2_Click()

    Winsock1.SendData text2.text
    End Sub

    Private Sub Command1_Click()
    Winsock1.Connect
    End Sub

    Private Sub Form_Load()
    Winsock1.RemotePort = 4746
    Winsock1.RemoteHost = text1.text

    End Sub


    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim TempData As String

    Winsock1.GetData TempData
    Text3.Text = Trim(Text3.Text) & vbCrLf & TempData

    End Sub

    Please tell me what you think and any thing i could add. thanx trials
    If its not broken it can still be inproved.

  2. #2
    name your controls logically; ie text1 becomes txtInput, command1 become cmdQuit etc.

Posting Permissions

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