100分悬赏,局域网内文件传输。

trite 2003-11-13 09:27:53
我现在开发数据库管理系统,单机版已调试成功,其中有些字段是存储某些文件(主要是word文档)的路径,直接连接打开文件。现在有两个问题想请教大家:
一、计算机间如何实现文件传输?所有文件存储在一台服务器上,文件传输只在局域网中实现。有什么命令吗?作为服务器端需要怎么设置?
二、这些文件如何打开?直接打开服务器上的文件,还是先下载到临时文件夹中再打开?
...全文
33 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
cngxylyh 2003-11-16
  • 打赏
  • 举报
回复
登录机器的时候直接登录win2000域,以后访问服务器就不用输密码了。
trite 2003-11-14
  • 打赏
  • 举报
回复
谢谢各位,我基本上看明白了。我先试一下,有错误再向大家请教。
snowolf_ren 2003-11-13
  • 打赏
  • 举报
回复
gz
intocsdn 2003-11-13
  • 打赏
  • 举报
回复
不用那么复杂,把服务器的目录共享出来映射成客户机的一个驱动器,就可以直接从客户机考文件过去了考过去了。
---------------------------------------------------------------------
请教一下,WIN2000下共享目录时会要求输入密码,怎么才能不输入密码?
golden24kcn 2003-11-13
  • 打赏
  • 举报
回复
服务器端
Option Explicit
Dim fs
Dim SaveFile
Dim LngFilePack As Long
Dim LngNowPack As Long

Private Sub cmdCloseService_Click()
Unload Me
End Sub

Private Sub Form_Load()
Socket(0).LocalPort = 1259
Socket(0).Listen
lbl.Caption = "正在监听1259端口...."
StrFileName = "E:\program\数据交换模型\Server\File\基本数据库概念.zip"
StrFileLen = FileLen(StrFileName)
Call DividePacks(StrFileName, 3000)
DoEvents
Pause (2000)
'LngPacketSize = 7000
LngFilePack = LngChunkPacks
End Sub

Private Sub Socket_Close(Index As Integer)
Socket(Index).Close
Erase StrDataChunk
Erase LngPosition
Pause (2000)
End Sub

Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
Load Socket(Socket.Count)
Socket(Socket.Count - 1).LocalPort = 0
Socket(Socket.Count - 1).Accept requestID
End If
End Sub

Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'On Error GoTo Errorhandler
Dim VtData
Dim VarData
Dim SelectionPart As String
Dim I As Integer


If Socket(Index).State = sckConnected Then Socket(Index).GetData VtData, vbString
Pause (50)
SelectionPart = Left(VtData, 5)
lbl.Caption = "From Client: " & SelectionPart

Select Case SelectionPart
Case "QFILE"
Pause (200)
If Socket(Index).State <> sckConnected Then GoTo ErrorConn Else Socket(Index).SendData ("AGREE")
Pause (200)
Case "AGREE"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QNAME")
Pause (200)
Case "QNAME"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FNAME|" & StrFileName)
Pause (200)
Case "FNAME"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QLENG")
Pause (200)
Case "QLENG"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FLENG" & StrFileLen)
Pause (200)
Case "FLENG"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QPACK")
Pause (200)
Case "QPACK"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FPACK|" & CStr(LngFilePack))
Pause (200)
Case "FPACK"
Pause (200)
StrFilePack = Trim(Mid(VtData, 7, Len(VtData) - 6))
LngFilePack = Val(StrFilePack)
ReDim StrDataChunk(LngFilePack)
ReDim LngPosition(LngFilePack)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QPCKL")
Pause (200)
Case "QPCKL"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FPCKL|" & LngPacketSize)
Pause (200)
Case "FPCKL"
Pause (200)
LngPacketSize = Val(Trim(Mid(VtData, 7, Len(VtData) - 6)))
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("START")
Pause (200)
Case "START", "SNDNX"
Pause (200)
If LngFilePack > LngNowPack Then
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("DPACK|" & StrDataChunk(LngNowPack))
List1.AddItem Len(StrDataChunk(LngNowPack))
LngNowPack = LngNowPack + 1
Pause (200)
Else
Pause (400)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("CLOSE")
End If
Case "SNDPR"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("DPACK|" & StrDataChunk(LngNowPack))
Pause (200)
Case "DPACK"
Pause (200)
If LngFilePack < UBound(StrDataChunk) Then
VarData = Mid(VtData, 7, Len(VtData) - 6)
If Len(VarData) = LngPacketSize Then
StrDataChunk(LngNowPack) = VarData
LngNowPack = LngNowPack + 1
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("SNDNX")
Pause (200)
Else
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("SNDPR")
Pause (200)
End If
Else
Pause (400)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("CLOSE")
End If
Case "CLOSE"
Pause (200)
Socket(Index).Close
Set fs = CreateObject("Scripting.FileSystemObject")
Set SaveFile = fs.CreateTextFile(App.Path & "\file\" & StrFileName, True)
For I = 0 To LngFilePack - 1
SaveFile.Write (StrDataChunk(I))
Next
SaveFile.Close
Set fs = Nothing
Set SaveFile = Nothing
End Select

Set VtData = Nothing
Set VarData = Nothing
Exit Sub

Errorhandler:

lbl.Caption = "[" & Err.Number & "]" & "[" & Err.Description & "]"
Err.Clear
Socket(Index).Close
Exit Sub

ErrorConn:
lbl.Caption = "SOCKET连接错误!"

End Sub

Private Sub Socket_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Socket(Index).Close
MsgBox "Err: [10.3.0.151] " & "[ " & Number & "] [" & Description & "]"
End Sub

以上协议是我自己定的,客户端与服务器端可以双向传输,你可以根据你的需求改一下吧
golden24kcn 2003-11-13
  • 打赏
  • 举报
回复
客户端

Option Explicit
Dim fs
Dim SaveFile
Dim LngFilePack As Long
Dim LngNowPack As Long

Private Sub cmdSend_Click()
If Socket.State = sckConnected Then
MsgBox "连接建立成功!"
Socket.SendData ("QFILE")
Exit Sub
End If
Socket.Protocol = sckTCPProtocol
Socket.RemoteHost = "10.3.0.151"
Socket.RemotePort = 1259
Socket.Connect
If Socket.State = sckConnected Then MsgBox "连接建立成功!"

End Sub

Private Sub Form_Load()
'strfilename = "E:\Downloads\HappyEO3sc_setup_305.exe"
End Sub

Private Sub Socket_Close()
Socket.Close
Erase StrDataChunk
Erase LngPosition
Pause (2000)
End Sub

Private Sub Socket_DataArrival(ByVal bytesTotal As Long)

'On Error GoTo Errorhandler
Dim VtData
Dim VarData
Dim SelectionPart As String
Dim StrFilePack As String
Dim I As Integer


If Socket.State = sckConnected Then Socket.GetData VtData, vbString
Pause (50)
SelectionPart = Left(VtData, 5)
lbl.Caption = "From Server:" & vbCrLf & SelectionPart

Select Case SelectionPart
Case "QFILE"
Pause (200)
If Socket.State <> sckConnected Then GoTo ErrorConn Else Socket.SendData ("AGREE")
Pause (200)
Case "AGREE"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("QNAME")
Pause (200)
Case "QNAME"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("FNAME|" & StrFileName)
Pause (200)
Case "FNAME"
Pause (200)
StrFileName = Mid(VtData, 7)
If Socket.State = sckConnected Then Socket.SendData ("QLENG")
Pause (200)
Case "QLENG"
Pause (200)
'If Socket.State = sckConnected Then Socket.SendData ("FLENG" & StrFileLen)
Pause (200)
Case "FLENG"
Pause (200)
StrFileLen = Mid(VtData, 7)
If Socket.State = sckConnected Then Socket.SendData ("QPACK")
Pause (200)
Case "QPACK"
Pause (200)
'If Socket.State = sckConnected Then Socket.SendData ("FPACK|" & StrFilePack)
Pause (200)
Case "FPACK"
Pause (200)
StrFilePack = Trim(Mid(VtData, 7, Len(VtData) - 6))
LngFilePack = Val(StrFilePack)
ReDim StrDataChunk(LngFilePack)
ReDim LngPosition(LngFilePack)
If Socket.State = sckConnected Then Socket.SendData ("QPCKL")
Pause (200)
Case "QPCKL"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("FPACK|" & LngPacketSize)
Pause (200)
Case "FPCKL"
Pause (200)
LngPacketSize = Val(Trim(Mid(VtData, 7, Len(VtData) - 6)))
If Socket.State = sckConnected Then Socket.SendData ("START")
Pause (200)
Case "START", "SNDNX"
Pause (200)
If LngFilePack > LngNowPack Then
If Socket.State = sckConnected Then Socket.SendData ("DPACK|" & StrDataChunk(LngNowPack))
LngNowPack = LngNowPack + 1
Pause (200)
Else
Pause (400)
If Socket.State = sckConnected Then Socket.SendData ("CLOSE")
End If
Case "SNDPR"
If Socket.State = sckConnected Then Socket.SendData ("DPACK|" & StrDataChunk(LngNowPack))
Pause (200)
Case "DPACK"
Pause (200)
If LngFilePack > LngNowPack Then

VarData = Mid(VtData, 7, Len(VtData) - 6)
MsgBox Len(VarData) & " " & LngNowPack
If Len(VarData) = LngPacketSize Then
StrDataChunk(LngNowPack) = VarData
LngNowPack = LngNowPack + 1
If Socket.State = sckConnected Then Socket.SendData ("SNDNX")
lbl.Caption = Str(LngNowPack)
Pause (200)
Else
If Socket.State = sckConnected Then Socket.SendData ("SNDPR")
Pause (200)
End If
Else
Pause (400)
If Socket.State = sckConnected Then Socket.SendData ("CLOSE")
End If
Case "CLOSE"
Pause (200)
Socket.Close

Set fs = CreateObject("Scripting.FileSystemObject")
'Set SaveFile = fs.CreateTextFile(App.Path & "\file\" & StrFileName, True)
Set SaveFile = fs.CreateTextFile(App.Path & "\file\01.zip", True)
For I = 0 To LngFilePack - 1
SaveFile.Write (StrDataChunk(I))
Next
SaveFile.Close
Set fs = Nothing
Set SaveFile = Nothing
End Select

Set VtData = Nothing
Set VarData = Nothing
Exit Sub

Errorhandler:

lbl.Caption = "[" & Err.Number & "]" & "[" & Err.Description & "]"
Err.Clear
Socket.Close
Exit Sub

ErrorConn:
lbl.Caption = "SOCKET连接错误!"

End Sub

Private Sub Socket_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Socket.Close
MsgBox "Err: [" & txtHost.Text & "] " & "[ " & Number & "] [" & Description & "]"
End Sub
golden24kcn 2003-11-13
  • 打赏
  • 举报
回复
我的一个,给你代码吧!

这个是模块的,客户/服务器都要有这个
Option Explicit

Declare Function GetTickCount Lib "kernel32" () As Long
Public LngChunkPackSize As Long '
Public LngPosition() As Long
Public StrDataChunk() As String
Public Authorizationstring As String
Public TaskIDToCheck As String
Public TimeToCheck As String
Public TaskTimeToCheck As String
Public StrKey As String
Public ControlString1 As String
Public BrokenFileName As String
Public ResumeFileName As String
Public BrokenFileLen As Long
Public ResumeFileLen As Long
Public ResumeFilePacket As String * 512
Public ResumeFilePacketPos As Long
Public BrokenFileNum As Long
Public ResRetval As Long

Public StrFileName As String
Public StrFileLen As String
Public StrFilePack As String

Public OptionsIP As String
Public OptionsTop As Long
Public OptionsLeft As Long
Public LngNextPack As Long
Public LngTotalPacks As Long
Public StrDistributedFileName As String
Public LngDistributedFileLength As Long
Public LngDistributedFileNum As Long
Public LngDistributedFileChunkPos As Long
Public StrDistributedFileDataChunk As String
Public StrDistributedFileBackUpName As String
Public StrDistributedFileSavingLocation As String
Public StrDistributedFileTargetLocation As String
Public StrDistributedFileSavingName As String
Public ScreenFileNum As Long
Public SendScreen As Boolean
Public ResumeTargetLocation As String
Public ResumeTargetFolder As String
Public ResumeFileName2 As String
Public KioskAppHwnd As Long
Public SckStatusRemoteIP As String
Public SckStatusRemotePort As String
Public GelenDosya As String
Public MAX_CHUNK As Long
Public bReplied As Boolean
Public lTIme As Long
Public LastRemoteIP As String

Public LngPacketSize As Long '每包的长度
Public LngChunkPacks As Long '拆分的包数量
Public LngLastChunkSize As Long '拆分后的余数


Sub Pause(HowLong As Long)
Dim u%, tick As Long
tick = GetTickCount()
Do
u% = DoEvents
Loop Until tick + HowLong < GetTickCount
End Sub

Public Function DividePacks(ByVal FileToDivide As String, ByVal PacketSize As Long)
Dim Position As Long
Dim TestRetval As Long
Dim LngSendFileNum As Long, LngFileSize As Long
'Dim StrChunkPacks
Dim lngIndex As Long

'On Error GoTo Errorhandler

If PacketSize = 0 Then PacketSize = 1024
StrFileName = FileToDivide
frmServer.lbl.Caption = StrFileName
LngSendFileNum = FreeFile() '得到空的文件号

Erase StrDataChunk '重载
Erase LngPosition '重载
'==================================================================
'取得文件大小,以指定大小拆分成包
LngFileSize = (FileLen(StrFileName))
LngPacketSize = PacketSize
If LngFileSize < LngPacketSize Then
LngChunkPacks = 1
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
Else
LngLastChunkSize = LngFileSize Mod LngPacketSize
If LngLastChunkSize = 0 Then
LngChunkPacks = LngFileSize / LngPacketSize
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
Else
'StrChunkPacks = LngFileSize / LngPacketSize
'StrChunkPacks = Left(StrChunkPacks, (InStr(1, StrChunkPacks, ",")) - 1)
LngChunkPacks = (LngFileSize \ LngPacketSize) + 1
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
End If
End If
'===================================================================
'拆分过程
Open StrFileName For Binary As LngSendFileNum

Position = 1
For lngIndex = 1 To LngChunkPacks

StrDataChunk(lngIndex) = Input(LngPacketSize, LngSendFileNum)
LngPosition(lngIndex) = Position
Position = Position + LngPacketSize
' LngLocPos = Loc(LngSendFileNum)
' LngSeekPos = Seek(LngSendFileNum)
' Set ItmX = Form1.ListView1.ListItems.Add(Form1.ListView1.ListItems.Count + 1, , ("Packet [" & lngIndex & "]"), 2, 2)
' ItmX.SubItems(1) = Len(StrDataChunk(lngIndex))
' ItmX.SubItems(2) = LngPosition(lngIndex)

Next lngIndex

Close
'====================================================================
'校验包的完整性
If LngFileSize < LngPacketSize Then
TestRetval = Len(StrDataChunk(1))
If TestRetval = LngFileSize Then
Pause (200)
Else
Pause (200)
Exit Function
End If
Else
TestRetval = ((LngChunkPacks - 1) * LngPacketSize) + LngLastChunkSize
If TestRetval = LngFileSize Then
Pause (200)
Else
Pause (200)
Exit Function
End If
End If

Pause (200)

Exit Function

Errorhandler:
MsgBox " [1] Error :[" & Err.Number & "] Desc:[" & Err.Description & "]"
Err.Clear
Exit Function

End Function
螺丝刀2000 2003-11-13
  • 打赏
  • 举报
回复
up
cngxylyh 2003-11-13
  • 打赏
  • 举报
回复
不用那么复杂,把服务器的目录共享出来映射成客户机的一个驱动器,就可以直接从客户机考文件过去了考过去了。
planetike 2003-11-13
  • 打赏
  • 举报
回复
楼上的,我也正在想试一下这一方面的东西,能不能也发一个给我,到时候发帖给分啊。
planetike@hotmail.com
null1027 2003-11-13
  • 打赏
  • 举报
回复
如果楼主需要的话,邮箱给我,我可以把我做的一个简单的局域网内发送文件的小程序和一个简单的向数据库读取和写入文件的程序发给你。
null1027 2003-11-13
  • 打赏
  • 举报
回复
1,文件传输怎么实现这个问题很笼统啊,用winsock控件可以做到,研究一下吧。

2,文件打开嘛,有专门的语句可以打开和关闭以及读取文件内容的语句。

你这个程序,要是想给远程的数据库存储文件的话,我觉得也可以在客户端连接到远程服务器之后直接向数据库写入数据。当然,象你这样把文件传过去再由服务器存储也可以。
shazifeng 2003-11-13
  • 打赏
  • 举报
回复
呵呵,俺也长知识了!以前碰见时就不会,绕过去了
luoxi1124 2003-11-13
  • 打赏
  • 举报
回复
呵呵!学东西!
woookaooo 2003-11-13
  • 打赏
  • 举报
回复
用WINSOCK传送二进制数据进行传送文件。
kmzs 2003-11-13
  • 打赏
  • 举报
回复
WINSOCK太小了
egxsun 2003-11-13
  • 打赏
  • 举报
回复
to:golden24kcn(不甜)
你的传图片(比如gif或jpg文件)行不行?
yupingping 2003-11-13
  • 打赏
  • 举报
回复
up
trite 2003-11-13
  • 打赏
  • 举报
回复
谢谢各位,我现在是初级水平。还要一段时间才能看懂这段代码。谁有更简单的我更欢迎,我先试一试这个代码,试明白了马上给分。

7,771

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧