Code Problem

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von trumpetboy.

    Code Problem

    Hi ich hab hier n code der es meiner meinung nach über eine Ip möglich macht zu chatten.
    jetzt hab ich hier aber ein Problem.
    meiner meinung nach brauche ich da zwei Formen kann dass sein ?
    Ich bekomme auch immer Probleme mit dem Formdesinger woran kann das liegen ??

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Threading
    5. Imports System.Text
    6. Imports System.Windows.Forms
    7. public class ChatTool
    8. public Shared Sub Main
    9. Application.Run(New Form1)
    10. End Sub
    11. End class
    12. Public Class Form1
    13. Inherits System.Windows.Forms.Form
    14. Public Sub New()
    15. MyBase.New()
    16. InitializeComponent()
    17. End Sub
    18. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    19. If disposing Then
    20. If Not (components Is Nothing) Then
    21. components.Dispose()
    22. End If
    23. End If
    24. MyBase.Dispose(disposing)
    25. End Sub
    26. Private components As System.ComponentModel.IContainer
    27. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    28. Friend WithEvents Label1 As System.Windows.Forms.Label
    29. Friend WithEvents Label2 As System.Windows.Forms.Label
    30. Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    31. Friend WithEvents Label3 As System.Windows.Forms.Label
    32. Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    33. Friend WithEvents Button1 As System.Windows.Forms.Button
    34. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    35. Me.TextBox1 = New System.Windows.Forms.TextBox
    36. Me.Label1 = New System.Windows.Forms.Label
    37. Me.Label2 = New System.Windows.Forms.Label
    38. Me.TextBox2 = New System.Windows.Forms.TextBox
    39. Me.Label3 = New System.Windows.Forms.Label
    40. Me.TextBox3 = New System.Windows.Forms.TextBox
    41. Me.Button1 = New System.Windows.Forms.Button
    42. Me.SuspendLayout()
    43. '
    44. 'TextBox1
    45. '
    46. Me.TextBox1.Location = New System.Drawing.Point(88, 16)
    47. Me.TextBox1.Name = "TextBox1"
    48. Me.TextBox1.Size = New System.Drawing.Size(136, 20)
    49. Me.TextBox1.TabIndex = 0
    50. Me.TextBox1.Text = ""
    51. '
    52. 'Label1
    53. '
    54. Me.Label1.Location = New System.Drawing.Point(32, 16)
    55. Me.Label1.Name = "Label1"
    56. Me.Label1.Size = New System.Drawing.Size(48, 24)
    57. Me.Label1.TabIndex = 1
    58. Me.Label1.Text = "Message"
    59. '
    60. 'Label2
    61. '
    62. Me.Label2.Location = New System.Drawing.Point(0, 64)
    63. Me.Label2.Name = "Label2"
    64. Me.Label2.Size = New System.Drawing.Size(80, 24)
    65. Me.Label2.TabIndex = 2
    66. Me.Label2.Text = "Target IP"
    67. '
    68. 'TextBox2
    69. '
    70. Me.TextBox2.Location = New System.Drawing.Point(88, 64)
    71. Me.TextBox2.Name = "TextBox2"
    72. Me.TextBox2.Size = New System.Drawing.Size(136, 20)
    73. Me.TextBox2.TabIndex = 3
    74. Me.TextBox2.Text = ""
    75. '
    76. 'Label3
    77. '
    78. Me.Label3.Location = New System.Drawing.Point(8, 104)
    79. Me.Label3.Name = "Label3"
    80. Me.Label3.Size = New System.Drawing.Size(72, 24)
    81. Me.Label3.TabIndex = 4
    82. Me.Label3.Text = "Record"
    83. '
    84. 'TextBox3
    85. '
    86. Me.TextBox3.AutoSize = False
    87. Me.TextBox3.Location = New System.Drawing.Point(80, 104)
    88. Me.TextBox3.Name = "TextBox3"
    89. Me.TextBox3.Size = New System.Drawing.Size(184, 136)
    90. Me.TextBox3.TabIndex = 5
    91. Me.TextBox3.Text = ""
    92. '
    93. 'Button1
    94. '
    95. Me.Button1.Location = New System.Drawing.Point(232, 16)
    96. Me.Button1.Name = "Button1"
    97. Me.Button1.Size = New System.Drawing.Size(56, 24)
    98. Me.Button1.TabIndex = 6
    99. Me.Button1.Text = "Send"
    100. '
    101. 'Form1
    102. '
    103. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    104. Me.ClientSize = New System.Drawing.Size(292, 266)
    105. Me.Controls.Add(Me.Button1)
    106. Me.Controls.Add(Me.TextBox3)
    107. Me.Controls.Add(Me.Label3)
    108. Me.Controls.Add(Me.TextBox2)
    109. Me.Controls.Add(Me.Label2)
    110. Me.Controls.Add(Me.Label1)
    111. Me.Controls.Add(Me.TextBox1)
    112. Me.ResumeLayout(False)
    113. End Sub
    114. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    115. Dim bytes(200) As Byte
    116. Dim MyIP As IPAddress
    117. Dim RemoteIP As IPAddress
    118. Dim str As String
    119. RemoteIP = IPAddress.Parse(TextBox2.Text)
    120. MyIP = IPAddress.Parse("912.168.1.100")
    121. Dim Port1 As Integer = 8080
    122. Dim port2 As Integer = 8081
    123. Dim myLocal As New IPEndPoint(MyIP, Port1)
    124. Dim myRemote As New IPEndPoint(RemoteIP, port2)
    125. Dim receive1 As New UdpClient(myRemote)
    126. Dim send1 As New UdpClient(myLocal)
    127. Try
    128. bytes = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text.ToUpper)
    129. send1.Send(bytes, bytes.Length, myRemote)
    130. str = System.Text.Encoding.ASCII.GetString(bytes)
    131. TextBox3.Text = TextBox3.Text & vbCrLf & str
    132. bytes = receive1.Receive(myLocal)
    133. str = System.Text.Encoding.ASCII.GetString(bytes)
    134. TextBox3.Text = TextBox2.Text & vbCrLf & str
    135. Catch ex As Exception
    136. Console.WriteLine("")
    137. End Try
    138. send1.Close()
    139. receive1.Close()
    140. End Sub
    141. End Class
    Kann es einfach sein, das dort designer code zwischen gekommen ist ?(

    und dann ist es fragwürdig dieses Programm "chat" zu nennen

    Das programm baut eine verbindung auf
    sendet daten
    wartet so lange bis daten zurückkommen
    schließ dann die verbindung gibt aber den reservierten speicher nicht direkt frei, und ich bin mir net mal sicher ob ein einfach .close reicht.

    Und ein zweites Formular brauchst du nicht.