Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Talk Application Problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    Resolved [RESOLVED] [2005] Talk Application Problems

    I've got a talk application that runs from within VB, and there's a substantial problem with it: I can only receive one message, then no more. No exceptions, no errors thrown, nothing. I can't get to the bottom of it and would appreciate some help. Code is below

    vb.net Code:
    1. Imports System.Net
    2. Imports System.Net.Sockets
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.Threading
    6.  
    7. Public Class frmTalk
    8.  
    9. Dim RemoteIP As String = "127.0.0.1"
    10. Dim Connected As Boolean = False
    11. Dim Client As TcpClient
    12. Dim Listener As TcpListener
    13. Dim t As Thread
    14. Dim objStreamWriter As StreamWriter
    15.  
    16. Private Delegate Sub Invoker(ByVal str As String)
    17.  
    18. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    19.     Me.Close()
    20. End Sub
    21.  
    22. Private Sub DoListen()
    23.     Dim strTemp As String = ""
    24.     Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 65535)
    25.     Listener.Start()
    26.     Do
    27.         If Listener.Pending = True Then Listener.AcceptSocket()
    28.         Dim objClient As TcpClient = Listener.AcceptTcpClient
    29.         Dim objStreamReader As New StreamReader(objClient.GetStream)
    30.         While objStreamReader.Peek <> -1
    31.             strTemp &= Convert.ToChar(objStreamReader.Read).ToString
    32.         End While
    33.         Dim params() As Object = {strTemp}
    34.         Me.Invoke(New Invoker(AddressOf WriteText), params)
    35.     Loop
    36. End Sub
    37.  
    38. Private Sub frmTalk_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    39.     Connected = False
    40.     prgRecieved.Value = 0
    41.     btnSendFile.Enabled = False
    42.     Try
    43.         t.Abort()
    44.     Catch tae As Exception
    45.         'Ignore error to prevent thread looping
    46.     End Try
    47. End Sub
    48.  
    49. Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
    50.     RemoteIP = InputBox("Please enter the IP address to connect to", , RemoteIP)
    51.     If My.Computer.Network.Ping(RemoteIP) Then
    52.         t = New Thread(AddressOf DoListen)
    53.         t.Start()
    54.         Try
    55.             Client = New TcpClient(RemoteIP, 65535)
    56.         Catch ex As Exception
    57.             MessageBox.Show(ex.Message)
    58.             Clipboard.SetText(ex.Message)
    59.             Exit Sub
    60.         End Try
    61.         DisconnectToolStripMenuItem.Enabled = True
    62.         ConnectToolStripMenuItem.Enabled = False
    63.         txtMessage.Enabled = True
    64.         txtRecieved.Enabled = True
    65.         btnSend.Enabled = True
    66.         prgRecieved.Enabled = True
    67.         prgRecieved.Value = 0
    68.         btnSendFile.Enabled = True
    69.         Connected = True
    70.         MessageBox.Show("You have successfully connected to " & RemoteIP.ToString)
    71.     Else
    72.         MessageBox.Show("The IP address you entered is invalid or is being blocked by a firewall")
    73.     End If
    74. End Sub
    75.  
    76. Private Sub DisconnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectToolStripMenuItem.Click
    77.     Client.Close()
    78.     ConnectToolStripMenuItem.Enabled = True
    79.     DisconnectToolStripMenuItem.Enabled = False
    80.     Connected = False
    81.     tmrRecieve.Enabled = False
    82.     txtMessage.Enabled = False
    83.     txtRecieved.Enabled = False
    84.     btnSend.Enabled = False
    85.     prgRecieved.Enabled = False
    86.     btnSendFile.Enabled = False
    87.     prgRecieved.Value = 0
    88. End Sub
    89.  
    90.     Private Sub btnSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFile.Click
    91.         Dim ofd As New OpenFileDialog
    92.  
    93.         ofd.Filter = "All files|*.*"
    94.         ofd.Multiselect = False
    95.         ofd.CheckFileExists = True
    96.         If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    97.             'Code to send file
    98.         End If
    99.     End Sub
    100.  
    101. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    102.     objStreamWriter = New StreamWriter(Client.GetStream)
    103.     objStreamWriter.Write(txtMessage.Text)
    104.     objStreamWriter.Flush()
    105.     objStreamWriter.Close()
    106. End Sub
    107.  
    108. Private Sub WriteText(ByVal str As String)
    109.     txtRecieved.Text &= RemoteIP & " says: " & vbCrLf & str & vbCrLf & vbCrLf
    110. End Sub
    111. End Class

  2. #2
    Lively Member
    Join Date
    Jan 2007
    Posts
    95

    Re: [2005] Talk Application Problems

    i got an error on this line in sendbtn_click:
    objStreamWriter = New StreamWriter(Client.GetStream)

    i forgot the description and cannot get it again... i dunt know what happened to my computer and it is not running the application again...

    but still i think it said that cannot get the disconnect client or something...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    Re: [2005] Talk Application Problems

    That was the problem I ran into. How can I get past it?

  4. #4
    Lively Member
    Join Date
    Jan 2007
    Posts
    95

    Re: [2005] Talk Application Problems


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    Re: [2005] Talk Application Problems

    I already did. That brought up quite substantial problems across the rest of my program. Thanks for the reply though

  6. #6
    Lively Member
    Join Date
    Jan 2007
    Posts
    95

    Re: [2005] Talk Application Problems

    but Winsock2007 is too easy... you are using the built-in socket and it is very very complicated and difficult... i converted my single player game into multiplayer game (can play over internet) in just three days!!! today i finished that work

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Post Re: [2005] Talk Application Problems

    its not necessary to go using extra components for something that already exists in the .Net framework.
    Im going to make some changes in your code, try it out and see if it works better.


    VB Code:
    1. Imports System.Net
    2. Imports System.Net.Sockets
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.Threading
    6.  
    7. Public Class frmTalk
    8.  
    9. Dim RemoteIP As String = "127.0.0.1"
    10. Dim Connected As Boolean = False
    11. Dim Client As TcpClient
    12. Dim Listener As TcpListener
    13. Dim t As Thread
    14. Dim objStreamWriter As StreamWriter
    15.  
    16. Private Delegate Sub Invoker(ByVal str As String)
    17.  
    18. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    19.     Me.Close()
    20. End Sub
    21.  
    22. Private Sub DoListen()
    23.     Dim strTemp As String = ""
    24.     Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 65535)
    25.     Listener.Start()
    26.     Dim objStreamReader As StreamReader
    27.     Do
    28.         Try
    29.         Dim objClient As TcpClient = Listener.AcceptTcpClient
    30.         objStreamReader = New StreamReader(objClient.GetStream)
    31.        
    32.         Dim params() As Object = {objStreamReader.ReadToEnd}
    33.         Me.Invoke(New Invoker(AddressOf WriteText), params)
    34.        Catch
    35.        End Try
    36.     Loop
    37. End Sub
    38.  
    39. Private Sub frmTalk_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    40.     Connected = False
    41.     prgRecieved.Value = 0
    42.     btnSendFile.Enabled = False
    43.     Try
    44.         t.Abort()
    45.     Catch tae As Exception
    46.         'Ignore error to prevent thread looping
    47.     End Try
    48. End Sub
    49.  
    50. Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
    51.     RemoteIP = InputBox("Please enter the IP address to connect to", , RemoteIP)
    52.     If My.Computer.Network.Ping(RemoteIP) Then
    53.         t = New Thread(AddressOf DoListen)
    54.         t.IsBackGround = True
    55.         t.Start()
    56.         Try
    57.             Client = New TcpClient(RemoteIP, 65535)
    58.         Catch ex As Exception
    59.             MessageBox.Show(ex.Message)
    60.             Clipboard.SetText(ex.Message)
    61.             Exit Sub
    62.         End Try
    63.         DisconnectToolStripMenuItem.Enabled = True
    64.         ConnectToolStripMenuItem.Enabled = False
    65.         txtMessage.Enabled = True
    66.         txtRecieved.Enabled = True
    67.         btnSend.Enabled = True
    68.         prgRecieved.Enabled = True
    69.         prgRecieved.Value = 0
    70.         btnSendFile.Enabled = True
    71.         Connected = True
    72.         MessageBox.Show("You have successfully connected to " & RemoteIP.ToString)
    73.     Else
    74.         MessageBox.Show("The IP address you entered is invalid or is being blocked by a firewall")
    75.     End If
    76. End Sub
    77.  
    78. Private Sub DisconnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectToolStripMenuItem.Click
    79.     Client.Close()
    80.     ConnectToolStripMenuItem.Enabled = True
    81.     DisconnectToolStripMenuItem.Enabled = False
    82.     Connected = False
    83.     tmrRecieve.Enabled = False
    84.     txtMessage.Enabled = False
    85.     txtRecieved.Enabled = False
    86.     btnSend.Enabled = False
    87.     prgRecieved.Enabled = False
    88.     btnSendFile.Enabled = False
    89.     prgRecieved.Value = 0
    90. End Sub
    91.  
    92.     Private Sub btnSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFile.Click
    93.         Dim ofd As New OpenFileDialog
    94.  
    95.         ofd.Filter = "All files|*.*"
    96.         ofd.Multiselect = False
    97.         ofd.CheckFileExists = True
    98.         If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    99.             'Code to send file
    100.         End If
    101.     End Sub
    102.  
    103. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    104.     objStreamWriter = New StreamWriter(Client.GetStream)
    105.     objStreamWriter.Write(txtMessage.Text)
    106.     objStreamWriter.Flush()
    107. End Sub
    108.  
    109. Private Sub WriteText(ByVal str As String)
    110.     txtRecieved.Text &= RemoteIP & " says: " & Environment.NewLine & str & Environment.NewLine & Environment.NewLine
    111. End Sub
    112. End Class
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    88

    Re: [RESOLVED] [2005] Talk Application Problems

    That worked great thanks

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