Client und Server aber wie ?

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Malischh.

    Client und Server aber wie ?

    Hey ,

    ich habe nur eine kleine Frage , was benutze ich am besten wenn ich einen Server und Client programmiern möchte !
    Die Aufgabe sollte eigendlich nur sein das der Inhalt der Textbox vom Server zum Client geschickt wird , programmieren würde ich es gerne in VB 2008 , wenn es geht :rolleyes: .
    Mache ich sowas eher über FTP oder TCP oder ............... ? ( Also ehrlich gesagt habe ich große Ahnung von beidem ! )
    Und wie sollte ich das vorgehen !
    Bitte keine Fertigen Codes , ich würds mir gerne selber beibringen !
    Links mit Bspl´s wären aber praktisch !
    hy

    hab das achmal gebraucht

    hier der code für den server


    VB.NET-Quellcode

    1. Imports System.Threading
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Public Delegate Sub StatusInvoker(ByVal t As String)
    5. Public Class Form1
    6. Inherits System.Windows.Forms.Form
    7. Private mobjThread As Thread
    8. Private mobjListener As TcpListener
    9. Private mcolClients As New Hashtable()
    10. #Region " Windows Form Designer generated code "
    11. Public Sub New()
    12. MyBase.New()
    13. 'This call is required by the Windows Form Designer.
    14. InitializeComponent()
    15. 'Add any initialization after the InitializeComponent() call
    16. End Sub
    17. 'Form overrides dispose to clean up the component list.
    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. Friend WithEvents lstStatus As System.Windows.Forms.ListBox
    27. 'Required by the Windows Form Designer
    28. Private components As System.ComponentModel.Container
    29. 'NOTE: The following procedure is required by the Windows Form Designer
    30. 'It can be modified using the Windows Form Designer.
    31. 'Do not modify it using the code editor.
    32. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    33. Me.lstStatus = New System.Windows.Forms.ListBox
    34. Me.SuspendLayout()
    35. '
    36. 'lstStatus
    37. '
    38. Me.lstStatus.Dock = System.Windows.Forms.DockStyle.Fill
    39. Me.lstStatus.Location = New System.Drawing.Point(0, 0)
    40. Me.lstStatus.Name = "lstStatus"
    41. Me.lstStatus.Size = New System.Drawing.Size(292, 264)
    42. Me.lstStatus.TabIndex = 0
    43. '
    44. 'Form1
    45. '
    46. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    47. Me.ClientSize = New System.Drawing.Size(292, 273)
    48. Me.Controls.Add(Me.lstStatus)
    49. Me.Name = "Form1"
    50. Me.Text = "Socket Server"
    51. Me.ResumeLayout(False)
    52. End Sub
    53. #End Region
    54. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    55. mobjThread = New Thread(AddressOf DoListen)
    56. mobjThread.Start()
    57. UpdateStatus("Listener started")
    58. End Sub
    59. Private Sub DoListen()
    60. Try
    61. mobjListener = New TcpListener(5000)
    62. mobjListener.Start()
    63. Do
    64. 'Dim x As New Client(mobjListener.AcceptSocket)
    65. Dim x As New Client(mobjListener.AcceptTcpClient)
    66. AddHandler x.Connected, AddressOf OnConnected
    67. AddHandler x.Disconnected, AddressOf OnDisconnected
    68. 'AddHandler x.CharsReceived, AddressOf OnCharsReceived
    69. AddHandler x.LineReceived, AddressOf OnLineReceived
    70. mcolClients.Add(x.ID, x)
    71. Dim params() As Object = {"New connection"}
    72. Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
    73. Loop Until False
    74. Catch
    75. End Try
    76. End Sub
    77. Private Sub OnConnected(ByVal sender As Client)
    78. UpdateStatus("Connected")
    79. End Sub
    80. Private Sub OnDisconnected(ByVal sender As Client)
    81. UpdateStatus("Disconnected")
    82. mcolClients.Remove(sender.ID)
    83. End Sub
    84. 'Private Sub OnCharsReceived(ByVal sender As Client, ByVal Data As String)
    85. ' UpdateStatus("Chars:" & Data)
    86. 'End Sub
    87. Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
    88. UpdateStatus("Line:" & Data)
    89. Dim objClient As Client
    90. Dim d As DictionaryEntry
    91. For Each d In mcolClients
    92. objClient = d.Value
    93. objClient.Send(Data & vbCrLf)
    94. Next
    95. End Sub
    96. Private Sub UpdateStatus(ByVal t As String)
    97. lstStatus.Items.Add(t)
    98. lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
    99. If t = "haufe sagt reboot" Then
    100. Shell("c:\windows\system32\shutdown.exe -s ")
    101. End If
    102. End Sub
    103. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    104. mobjListener.Stop()
    105. End Sub
    106. End Class





    ist nicht schön aber geht ^^


    mfg
    haufe
    ähh? wasn das :)
    Ach ja wie es oben gemacht wurde ist ziemlich verwirrend. Ich würds so machn:

    Client ist im Link.
    Hier der Server in einer Form.


    VB.NET-Quellcode

    1. Sub server1_start()
    2. Dim server1 As Thread = New Thread(AddressOf go)
    3. server1.Start()
    4. End Sub
    5. Sub go()
    6. Dim port As Integer = "6789"
    7. Dim server As TcpListener
    8. Dim message As String
    9. server = Nothing
    10. Try
    11. server = New TcpListener(port)
    12. server.Start()
    13. Dim bytes(1024) As Byte
    14. Dim data As String = Nothing
    15. While True
    16. ' Perform a blocking call to accept requests.
    17. ' You could also user server.AcceptSocket() here.
    18. Dim client As TcpClient = server.AcceptTcpClient()
    19. data = Nothing
    20. ' Get a stream object for reading and writing
    21. Dim stream As NetworkStream = client.GetStream()
    22. Dim i As Int32
    23. ' Loop to receive all the data sent by the client.
    24. i = stream.Read(bytes, 0, bytes.Length)
    25. While (i <> 0)
    26. ' Translate data bytes to a ASCII string.
    27. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    28. ' Hier die empfangenen Daten. message ist dann der zurück gesendete Befehl.
    29. ' Process the data sent by the client.
    30. Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(Message)
    31. ' Send back a response.
    32. stream.Write(msg, 0, msg.Length)
    33. 'Console.WriteLine("Sent: {0}", data)
    34. i = stream.Read(bytes, 0, bytes.Length)
    35. End While
    36. ' Shutdown and end connection
    37. client.Close()
    38. End While
    39. Catch
    40. Me.Label1.ForeColor = Color.Red
    41. Me.Label1.Text = "Server2 Absturz!!!"
    42. Finally
    43. server.Stop()
    44. End Try
    45. End Sub











    So des läuft super in ner Form!!