找回密码
 加入
搜索
查看: 2577|回复: 2

[网络通信] TCP传送多个文件问题

[复制链接]
发表于 2011-4-4 21:31:21 | 显示全部楼层 |阅读模式
我想对客户端发送两个文件,用循环后,客户端收到文件后会把数据叠加一次。
久久试验还是不行,下面的是传送文件单个文件,请教怎么写成客户端接收两个文件

服务端
$iPort = IniRead("ip.ini", "服务器地址", "端口", "")
$g_IP = IniRead("ip.ini", "服务器地址", "ip", "")
Local $sendfile = @ScriptDir & "\avltool.exe";文件路径
Global $MainSocket
TCPStartup();开始TCP服务
$MainSocket = TCPListen($g_IP, $iPort, 100);创建监听
If $MainSocket = -1 Then
MsgBox(0, '错误', '创建监听失败!')
EndIf

While 1 
$ConnectedSocket = TCPAccept($MainSocket);尝试接受一个套接字(socket)连接.
If $ConnectedSocket <> -1 Then ;如果不为-1代表有客户端连接
  $file = FileOpen($sendfile, 16);以二进制方式打开文件
  While 1
   $fdata = FileRead($file);读取文件
   If @error = -1  Then ;文件读取完毕
    TCPCloseSocket($ConnectedSocket)
    ExitLoop
   EndIf
   TCPSend($ConnectedSocket, $fdata);发送数据
  WEnd
EndIf
WEnd
客户端
$iPort = IniRead("ip.ini", "服务器地址", "端口", "")
$g_IP = IniRead("ip.ini", "服务器地址", "ip", "")

TCPStartup();开始TCP服务

_recvFiles()

Func _recvFiles()

Local $downfile = @ScriptDir & "\avltool2a.exe";接收文件路径

$Socket = TCPConnect($g_IP, $iPort);连接服务端
If $Socket = -1 Then
MsgBox(0, '错误', '连接到服务端出错!')
Exit
EndIf

$file = FileOpen($downfile, 2 + 8 + 16)
If $file = -1 Then 
MsgBox(0,'错误','打开目标文件出错!')
EndIf

While 1
$ConnectedSocket = TCPRecv($Socket, 1024 * 1000, 1);接收数据
If @error Then
ExitLoop
Else
;写文件
FileWrite($file, $ConnectedSocket)
EndIf
WEnd
MsgBox(0,'文件接收完成!','文件接收完成!')
EndFunc ;==>_recvFiles
配置文件 ip.ini
[服务器地址]
IP=
端口=
发表于 2011-4-5 10:41:05 | 显示全部楼层
沙发,顶起
发表于 2011-4-5 18:19:20 | 显示全部楼层
不太懂,学习一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-19 08:36 , Processed in 0.076677 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表