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
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
Könnt ihr mir weiterhelfen?
mfg
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...
VB.NET-Quellcode
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Data
- Imports System.Drawing
- Imports System.Text
- Imports System.Windows.Forms
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.IO
- Partial Public Class Form1
- Inherits Form
- Public Sub New()
- InitializeComponent()
- End Sub
- Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim fDg As FileDialog = New OpenFileDialog()
- If fDg.ShowDialog() = DialogResult.OK Then
- FTClientCode.SendFile(fDg.FileName)
- End If
- End Sub
- Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Label3.Text = FTClientCode.curMsg
- End Sub
- End Class
- 'FILE TRANSFER USING C#.NET SOCKET - CLIENT
- Class FTClientCode
- Public Shared curMsg As String = "Idle"
- Public Shared Sub SendFile(ByVal fileName As String)
- ' Try
- Dim ipAddress As IPAddress() = Dns.GetHostAddresses("localhost")
- Dim ipEnd As New IPEndPoint(ipAddress(0), 5656)
- Dim clientSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
- Dim filePath As String = ""
- fileName = fileName.Replace("\", "/")
- While fileName.IndexOf("/") > -1
- filePath += fileName.Substring(0, fileName.IndexOf("/") + 1)
- fileName = fileName.Substring(fileName.IndexOf("/") + 1)
- End While
- Dim fileNameByte As Byte() = Encoding.ASCII.GetBytes(fileName)
- If fileNameByte.Length > 850 * 1024 Then
- curMsg = "File size is more than 850kb, please try with small file."
- Return
- End If
- curMsg = "Buffering ..."
- Dim fileData As Byte() = File.ReadAllBytes(filePath & fileName)
- Dim clientData As Byte() = New Byte(4 + fileNameByte.Length + (fileData.Length - 1)) {}
- Dim fileNameLen As Byte() = BitConverter.GetBytes(fileNameByte.Length)
- fileNameLen.CopyTo(clientData, 0)
- fileNameByte.CopyTo(clientData, 4)
- fileData.CopyTo(clientData, 4 + fileNameByte.Length)
- curMsg = "Connection to server ..."
- clientSock.Connect(ipEnd)
- curMsg = "File sending..."
- clientSock.Send(clientData)
- curMsg = "Disconnecting..."
- clientSock.Close()
- curMsg = "File transferred."
- 'Catch ex As Exception
- 'If ex.Message = "No connection could be made because the target machine actively refused it" Then
- 'curMsg = "File Sending fail. Because server not running."
- 'Else
- 'curMsg = "File Sending fail." & ex.Message
- 'End If
- ' End Try
- End Sub
- 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:
VB.NET-Quellcode
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Data
- Imports System.Drawing
- Imports System.Text
- Imports System.Windows.Forms
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.IO
- 'That code is written by Suman Biswas, Calcutta, India (Email: sumanbiswas@aol.in,Website: sumanbiswas.xm.com).
- 'That code is running to transfer small file to client to server. by using and after doing modification any one
- 'can able to make a large file transfer application in C#.Net. This is Server code.
- Partial Public Class Form1
- Inherits Form
- Public Sub New()
- InitializeComponent()
- FTServerCode.receivedPath = ""
- End Sub
- Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
- label5.Text = FTServerCode.receivedPath
- label3.Text = FTServerCode.curMsg
- End Sub
- Private obj As New FTServerCode()
- Private Sub button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
- Dim fd As New FolderBrowserDialog()
- If fd.ShowDialog() = DialogResult.OK Then
- FTServerCode.receivedPath = fd.SelectedPath
- End If
- End Sub
- Private Sub button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
- If FTServerCode.receivedPath.Length > 0 Then
- BackgroundWorker1.RunWorkerAsync()
- Else
- MessageBox.Show("Please select file receiving path")
- End If
- End Sub
- Private Sub BackgroundWorker1_DoWork1(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
- obj.StartServer()
- End Sub
- Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- label5.Text = FTServerCode.receivedPath
- label3.Text = FTServerCode.curMsg
- End Sub
- End Class
- 'FILE TRANSFER USING C#.NET SOCKET - SERVER
- Class FTServerCode
- Private ipEnd As IPEndPoint
- Private sock As Socket
- Public Sub New()
- ipEnd = New IPEndPoint(IPAddress.Any, 5656)
- sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
- sock.Bind(ipEnd)
- End Sub
- Public Shared receivedPath As String
- Public Shared curMsg As String = "Stopped"
- Public Sub StartServer()
- Try
- curMsg = "Starting..."
- sock.Listen(100)
- curMsg = "Running and waiting to receive file."
- Dim clientSock As Socket = sock.Accept()
- Dim clientData As Byte() = New Byte(1024 * 5000 - 1) {}
- Dim receivedBytesLen As Integer = clientSock.Receive(clientData)
- curMsg = "Receiving data..."
- Dim fileNameLen As Integer = BitConverter.ToInt32(clientData, 0)
- Dim fileName As String = Encoding.ASCII.GetString(clientData, 4, fileNameLen)
- Dim bWrite As New BinaryWriter(File.Open(receivedPath & "/" & fileName, FileMode.Append))
- bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen)
- curMsg = "Saving file..."
- bWrite.Close()
- clientSock.Close()
- curMsg = "Reeived & Saved file; Server Stopped."
- Catch ex As Exception
- curMsg = "File Receving error."
- End Try
- End Sub
- End Class
Könnt ihr mir weiterhelfen?
mfg