初探socket,如何接收文件。
Client:
protected void Page_Load(object sender, EventArgs e)
{
Socket SendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse( "192.168.1.221 "), 2000);
FileStream fileSteam = new FileStream(@ "D:\My Documents\MyPictures\779ud9z23svz.jpg ", FileMode.Open);
Byte[] MyFile = new Byte[fileSteam.Length];
fileSteam.Read(MyFile, 0, MyFile.Length);
fileSteam.Flush();
fileSteam.Close();
fileSteam.Dispose();
SendSocket.Connect(endPoint);
SendSocket.Send(MyFile);
SendSocket.Shutdown(SocketShutdown.Send);
SendSocket.Close();
}
=========================================================================
Server:
protected void Page_Load(object sender, EventArgs e)
{
Socket ReceiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint HostPoint = new IPEndPoint(IPAddress.Any, 2000);
ReceiveSocket.Bind(HostPoint);
ReceiveSocket.Listen(100);
Socket HostSocket = ReceiveSocket.Accept();
Byte[] RecData = new Byte[1024];
HostSocket.Receive(RecData);
FileStream fileStream = new FileStream(@ "E:\myNewFile.jpg ", FileMode.OpenOrCreate);
fileStream.Write(RecData, 0, RecData.Length);
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
HostSocket.Shutdown(SocketShutdown.Receive);
HostSocket.Close();
}
==================================================================
现在的问题是1Mb多的图片,这边只能接受1Kb,如何连续接受流呢? 望各位指教,先些了。
------解决方案--------------------沙发,帮忙顶
下班了 ,明天再来看看
------解决方案--------------------基本传送原理就是这样,先转成二进制流,再传。
你在传时可以把缓存设大点,比如64K,然后Client端分成多次传.可以这样来做,
传大文件前,先传一个标志位到server,标志位中包括要传的文件的大小,server收到后,就开始缓冲一个接收池,准备接收。
Client开始连续传送,每次传64K,server把收到的数据依次放入缓冲池中,传完成后,再组合起来。
大概方法就这样,你可以灵活设计自己的程序。^_^