tcp file transfer questions
hello all
i have a few questions about tcp file transfer
i have a two programs one sends a file the other one receives
the source for the receiving program:
Code:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private alSockets As ArrayList
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lbConnections As System.Windows.Forms.ListBox
Friend WithEvents lblStatus As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblStatus = New System.Windows.Forms.Label
Me.lbConnections = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'lblStatus
'
Me.lblStatus.Location = New System.Drawing.Point(24, 16)
Me.lblStatus.Name = "lblStatus"
Me.lblStatus.Size = New System.Drawing.Size(136, 32)
Me.lblStatus.TabIndex = 0
Me.lblStatus.Text = "My IP address:"
'
'lbConnections
'
Me.lbConnections.Location = New System.Drawing.Point(16, 56)
Me.lbConnections.Name = "lbConnections"
Me.lbConnections.Size = New System.Drawing.Size(144, 173)
Me.lbConnections.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(176, 266)
Me.Controls.Add(Me.lbConnections)
Me.Controls.Add(Me.lblStatus)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Public Sub listenerThread()
Dim tcpListener As New TcpListener(8080)
Dim handlerSocket As Socket
Dim thdstHandler As ThreadStart
Dim thdHandler As Thread
tcpListener.Start()
Do
handlerSocket = tcpListener.AcceptSocket()
If handlerSocket.Connected Then
lbConnections.Items.Add( _
handlerSocket.RemoteEndPoint.ToString() + " connected.")
SyncLock (Me)
alSockets.Add(handlerSocket)
End SyncLock
thdstHandler = New ThreadStart(AddressOf handlerThread)
thdHandler = New Thread(thdstHandler)
thdHandler.Start()
End If
Loop
End Sub
Public Sub handlerThread()
Dim handlerSocket As Socket
handlerSocket = alSockets(alSockets.Count - 1)
Dim networkStream As NetworkStream = New NetworkStream(handlerSocket)
Dim blockSize As Int16 = 1024
Dim thisRead As Int16
Dim dataByte(blockSize) As Byte
SyncLock Me
' Only one process can access the
' same file at any given time
Dim fileStream As Stream
fileStream = File.OpenWrite("c:\upload.txt")
While (True)
thisRead = networkStream.Read(dataByte, 0, blockSize)
fileStream.Write(dataByte, 0, dataByte.Length)
If thisRead = 0 Then Exit While
End While
fileStream.Close()
End SyncLock
lbConnections.Items.Add("File Written")
handlerSocket = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim IPHost As IPHostEntry
IPHost = Dns.GetHostByName(Dns.GetHostName())
lblStatus.Text = "My IP address is " + IPHost.AddressList(0).ToString()
alSockets = New ArrayList
Dim thdListener As New Thread(New ThreadStart(AddressOf listenerThread))
thdListener.Start()
End Sub
End Class
i can receive text files without any problems
1. how do i receive binary files?????????
i only need to change receiving programs code, sender program has everythinng configured for text/binary
my second question:
2. how can i determine sent file information such as name and size?
thanks
Re: tcp file transfer questions
Quote:
Originally Posted by kaos_frack
1. how do i receive binary files?
If you're receiving a stream of bytes, it doesn't matter what the bytes are.
Quote:
2. how can i determine sent file information such as name and size?
That depends on how you're sending them. Set up a protocol - maybe %%%<filename>%<size>%<date>%%%the-actual-file-gets-sent-here. Then you just look for the second set of %%% from the beginning of the stream, save the rest as the file and split the first block into filename, size, date, etc.