none
求助 網路檔案傳輸 RRS feed

  • 問題

  • 諸位先進

    小弟想讓兩台電腦

    藉由tcp/ip連線來傳輸檔案

    首先分別上兩台電腦中的tcplistener級tcpclient連線

    但是 後面的傳輸檔案部分 我就苦惱了很久了

    以下是我的範例 煩請諸位先進指教

    Imports System.Threading
    Imports System.IO
    Public Class Form1
        Dim intPort As Integer
        Dim myTcpListener As TcpListener
        Dim myNetworkStream As NetworkStream
        Dim isconnect As Boolean = False

        Private Sub server_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles server_btn.Click
            intPort = 3600
            myTcpListener = New TcpListener(System.Net.IPAddress.Any, intPort)
            Dim blnConection As Boolean = False
            Try
                myTcpListener.Start()
                lblMessage.Text = "等待連線中 .............."
                Dim accept_client As TcpClient = myTcpListener.AcceptTcpClient
                Do
                    If accept_client.Connected = True Then
                        lblMessage.Text = "通訊埠 " + "3600" + " 連線成功"
                        isconnect = True
                    End If
                Loop While isconnect = False

            Catch ex As SocketException
                MessageBox.Show _
                (ex.Message, "開啟連接錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End Try
            '=================傳輸檔案部份
            Dim UPFile As New FileStream("c:\a.doc", FileMode.Open)
            Dim bytUPFile() As Byte
            Dim lngFileLength As Long
            myNetworkStream = myTcpListener.AcceptTcpClient.GetStream
            '========
            lngFileLength = UPFile.Length
            ReDim bytUPFile(lngFileLength)
            UPFile.Read(bytUPFile, 0, lngFileLength)
            myNetworkStream.Write(bytUPFile, 0, lngFileLength)
            myNetworkStream.Close()
            UPFile.Close()
            '========
        End Sub
    End Class

    '============================

    Imports System.Net.Sockets
    Imports System.IO
    Imports System.net
    Public Class Form1
        Dim myTcpClient As New TcpClient()
        Dim myNetworkStream As NetworkStream
        Private Sub connect_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect_btn.Click
            Dim strHostName As String = txtHostName.Text
            Dim intPort As Integer = Integer.Parse(txtPort.Text)

            Try
                myTcpClient.Connect(strHostName, intPort)
                myNetworkStream = myTcpClient.GetStream()
                lblMessage.Text = "主機 " + strHostName + "  通訊埠 " + intPort.ToString + " 連接成功 !!"

            Catch ex As ArgumentOutOfRangeException
                MessageBox.Show("ArgumentOutOfRangeException 例外 :" + vbCrLf + ex.Message, _
                "通訊埠號碼不正確", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Catch ex As SocketException
                MessageBox.Show("SocketException 例外 :" + vbCrLf + ex.Message, _
                "連接錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End Try
            '==========     '接收檔案
               Dim UPFile As New FileStream("c:\bb.doc", FileMode.Create)
            Dim bytUPFile() As Byte
            Dim lngFileLength As Long
            myNetworkStream = myTcpClient.GetStream
            ReDim bytUPFile(1024)
            Do
                lngFileLength = myNetworkStream.Read(bytUPFile, 0, 1024)
                UPFile.Write(bytUPFile, 0, lngFileLength)
            Loop While lngFileLength > 0

            UPFile.Close()
            myNetworkStream.Close()
            '===================
        End Sub

     

     

    2006年9月18日 下午 02:00

解答

所有回覆