Daten senden Problem

  • VB.NET

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

    Daten senden Problem

    Hi,

    ich habe mir ein Daten senden/empfangen via TCP/IP projekt (das in C# war) heruntergeladen und in vb übersetzt.
    Nun kommt immer ein error, womit ich nix anfangen kann...

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Collections.Generic
    2. Imports System.ComponentModel
    3. Imports System.Data
    4. Imports System.Drawing
    5. Imports System.Text
    6. Imports System.Windows.Forms
    7. Imports System.Net
    8. Imports System.Net.Sockets
    9. Imports System.IO
    10. Partial Public Class Form1
    11. Inherits Form
    12. Public Sub New()
    13. InitializeComponent()
    14. End Sub
    15. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16. Dim fDg As FileDialog = New OpenFileDialog()
    17. If fDg.ShowDialog() = DialogResult.OK Then
    18. FTClientCode.SendFile(fDg.FileName)
    19. End If
    20. End Sub
    21. Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    22. Label3.Text = FTClientCode.curMsg
    23. End Sub
    24. End Class
    25. 'FILE TRANSFER USING C#.NET SOCKET - CLIENT
    26. Class FTClientCode
    27. Public Shared curMsg As String = "Idle"
    28. Public Shared Sub SendFile(ByVal fileName As String)
    29. ' Try
    30. Dim ipAddress As IPAddress() = Dns.GetHostAddresses("localhost")
    31. Dim ipEnd As New IPEndPoint(ipAddress(0), 5656)
    32. Dim clientSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
    33. Dim filePath As String = ""
    34. fileName = fileName.Replace("\", "/")
    35. While fileName.IndexOf("/") > -1
    36. filePath += fileName.Substring(0, fileName.IndexOf("/") + 1)
    37. fileName = fileName.Substring(fileName.IndexOf("/") + 1)
    38. End While
    39. Dim fileNameByte As Byte() = Encoding.ASCII.GetBytes(fileName)
    40. If fileNameByte.Length > 850 * 1024 Then
    41. curMsg = "File size is more than 850kb, please try with small file."
    42. Return
    43. End If
    44. curMsg = "Buffering ..."
    45. Dim fileData As Byte() = File.ReadAllBytes(filePath & fileName)
    46. Dim clientData As Byte() = New Byte(4 + fileNameByte.Length + (fileData.Length - 1)) {}
    47. Dim fileNameLen As Byte() = BitConverter.GetBytes(fileNameByte.Length)
    48. fileNameLen.CopyTo(clientData, 0)
    49. fileNameByte.CopyTo(clientData, 4)
    50. fileData.CopyTo(clientData, 4 + fileNameByte.Length)
    51. curMsg = "Connection to server ..."
    52. clientSock.Connect(ipEnd)
    53. curMsg = "File sending..."
    54. clientSock.Send(clientData)
    55. curMsg = "Disconnecting..."
    56. clientSock.Close()
    57. curMsg = "File transferred."
    58. 'Catch ex As Exception
    59. 'If ex.Message = "No connection could be made because the target machine actively refused it" Then
    60. 'curMsg = "File Sending fail. Because server not running."
    61. 'Else
    62. 'curMsg = "File Sending fail." & ex.Message
    63. 'End If
    64. ' End Try
    65. End Sub
    66. End Class

    Fehler kommt bei "clientSock.connect(ipEnd)
    Es wurde eine Adresse verwendet, die mit dem angeforderten Protokoll nicht kompatibel ist ::1:5656

    Wo bzw. wie kommt dieser Fehler?
    Hier noch der Server:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Collections.Generic
    2. Imports System.ComponentModel
    3. Imports System.Data
    4. Imports System.Drawing
    5. Imports System.Text
    6. Imports System.Windows.Forms
    7. Imports System.Net
    8. Imports System.Net.Sockets
    9. Imports System.IO
    10. 'That code is written by Suman Biswas, Calcutta, India (Email: sumanbiswas@aol.in,Website: sumanbiswas.xm.com).
    11. 'That code is running to transfer small file to client to server. by using and after doing modification any one
    12. 'can able to make a large file transfer application in C#.Net. This is Server code.
    13. Partial Public Class Form1
    14. Inherits Form
    15. Public Sub New()
    16. InitializeComponent()
    17. FTServerCode.receivedPath = ""
    18. End Sub
    19. Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    20. label5.Text = FTServerCode.receivedPath
    21. label3.Text = FTServerCode.curMsg
    22. End Sub
    23. Private obj As New FTServerCode()
    24. Private Sub button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
    25. Dim fd As New FolderBrowserDialog()
    26. If fd.ShowDialog() = DialogResult.OK Then
    27. FTServerCode.receivedPath = fd.SelectedPath
    28. End If
    29. End Sub
    30. Private Sub button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    31. If FTServerCode.receivedPath.Length > 0 Then
    32. BackgroundWorker1.RunWorkerAsync()
    33. Else
    34. MessageBox.Show("Please select file receiving path")
    35. End If
    36. End Sub
    37. Private Sub BackgroundWorker1_DoWork1(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    38. obj.StartServer()
    39. End Sub
    40. Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    41. label5.Text = FTServerCode.receivedPath
    42. label3.Text = FTServerCode.curMsg
    43. End Sub
    44. End Class
    45. 'FILE TRANSFER USING C#.NET SOCKET - SERVER
    46. Class FTServerCode
    47. Private ipEnd As IPEndPoint
    48. Private sock As Socket
    49. Public Sub New()
    50. ipEnd = New IPEndPoint(IPAddress.Any, 5656)
    51. sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
    52. sock.Bind(ipEnd)
    53. End Sub
    54. Public Shared receivedPath As String
    55. Public Shared curMsg As String = "Stopped"
    56. Public Sub StartServer()
    57. Try
    58. curMsg = "Starting..."
    59. sock.Listen(100)
    60. curMsg = "Running and waiting to receive file."
    61. Dim clientSock As Socket = sock.Accept()
    62. Dim clientData As Byte() = New Byte(1024 * 5000 - 1) {}
    63. Dim receivedBytesLen As Integer = clientSock.Receive(clientData)
    64. curMsg = "Receiving data..."
    65. Dim fileNameLen As Integer = BitConverter.ToInt32(clientData, 0)
    66. Dim fileName As String = Encoding.ASCII.GetString(clientData, 4, fileNameLen)
    67. Dim bWrite As New BinaryWriter(File.Open(receivedPath & "/" & fileName, FileMode.Append))
    68. bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen)
    69. curMsg = "Saving file..."
    70. bWrite.Close()
    71. clientSock.Close()
    72. curMsg = "Reeived & Saved file; Server Stopped."
    73. Catch ex As Exception
    74. curMsg = "File Receving error."
    75. End Try
    76. End Sub
    77. End Class


    Könnt ihr mir weiterhelfen?

    mfg
    öhm...

    Kann ich nicht einfach "127.0.0.1" eintippen?

    Ich kann noch" uff, da gibt alles":
    .tcp, .upd, .ipv4, ipv6...

    echt viel, nun die frage: was ist davon brauchbar...

    gfcwfzkm schrieb:

    Dim ipAddress As IPAddress() = Dns.GetHostAddresses("localhost")
    Dim ipEnd As New IPEndPoint(ipAddress(0), 5656)
    GetHostAddresses liefert ja ein Array von Addressen. Und du nimmst einfach die erstbeste im Vertrauen, die wird schon das richtige Format haben.

    Vertrauen ist gut...

    VB.NET-Quellcode

    1. Dim ipAddress=Dns.GetHostAddresses(My.Computer.Name).FirstOrDefault(
    2. Function(p) p.AddressFamily = Sockets.AddressFamily.InterNetwork)
    3. Dim ipEnd As New IPEndPoint(ipAddress, 5656)