这个是客户端发送的
private static void SendFile()
{
string filePath = @"E:\release\bin\Common.dll";
string directPath = "E:/Common.dll";
int port = 8888;
IPAddress ip = IPAddress.Parse("192.168.10.11");
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(ip, port);
socket.Connect(endPoint);
Console.Write("Connect Successfull!\r\nStart Sending...\r\n"); FileInfo info = new FileInfo(filePath); using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate))
{
//发送文件长度
byte[] lenArr = BitConverter.GetBytes(fs.Length);
socket.Send(lenArr); //发送文件路径
byte[] path = Encoding.ASCII.GetBytes(directPath);
socket.Send(path); //发送文件
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
socket.Send(data);
}
Console.Write("Send Successfull!");
}code][code=C#] IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8888);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10); Socket client = socket.Accept();
IPEndPoint remote = (IPEndPoint)client.RemoteEndPoint;
Console.Write("Accept:" + remote.Address);
byte[] temp = new byte[32]; //接收文件长度
client.Receive(temp);
int length = BitConverter.ToInt32(temp, 0); //接收文件路径
client.Receive(temp);
string path = Encoding.ASCII.GetString(temp); //接收文件
byte[] data = new byte[length];
using (FileStream fs = new FileStream("", FileMode.OpenOrCreate))
{
fs.Write(data, 0, data.Length);
}
Console.Read();问题出现在“接收文件路径那里”,每次接收到的值都不一样的,有时候是:
"E:/Common.dllMZ?\0\0\0\0\0\0\0??\0\0?\0\0"
而有的时候是"\0??\0\0?\0\0\0\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
为什么会出现这种情况呢?
private static void SendFile()
{
string filePath = @"E:\release\bin\Common.dll";
string directPath = "E:/Common.dll";
int port = 8888;
IPAddress ip = IPAddress.Parse("192.168.10.11");
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(ip, port);
socket.Connect(endPoint);
Console.Write("Connect Successfull!\r\nStart Sending...\r\n"); FileInfo info = new FileInfo(filePath); using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate))
{
//发送文件长度
byte[] lenArr = BitConverter.GetBytes(fs.Length);
socket.Send(lenArr); //发送文件路径
byte[] path = Encoding.ASCII.GetBytes(directPath);
socket.Send(path); //发送文件
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
socket.Send(data);
}
Console.Write("Send Successfull!");
}code][code=C#] IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8888);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10); Socket client = socket.Accept();
IPEndPoint remote = (IPEndPoint)client.RemoteEndPoint;
Console.Write("Accept:" + remote.Address);
byte[] temp = new byte[32]; //接收文件长度
client.Receive(temp);
int length = BitConverter.ToInt32(temp, 0); //接收文件路径
client.Receive(temp);
string path = Encoding.ASCII.GetString(temp); //接收文件
byte[] data = new byte[length];
using (FileStream fs = new FileStream("", FileMode.OpenOrCreate))
{
fs.Write(data, 0, data.Length);
}
Console.Read();问题出现在“接收文件路径那里”,每次接收到的值都不一样的,有时候是:
"E:/Common.dllMZ?\0\0\0\0\0\0\0??\0\0?\0\0"
而有的时候是"\0??\0\0?\0\0\0\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
为什么会出现这种情况呢?
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8888);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10); Socket client = socket.Accept();
IPEndPoint remote = (IPEndPoint)client.RemoteEndPoint;
Console.Write("Accept:" + remote.Address);
byte[] temp = new byte[32]; //接收文件长度
client.Receive(temp);
int length = BitConverter.ToInt32(temp, 0); //接收文件路径
client.Receive(temp);
string path = Encoding.ASCII.GetString(temp); //接收文件
byte[] data = new byte[length];
using (FileStream fs = new FileStream("", FileMode.OpenOrCreate))
{
fs.Write(data, 0, data.Length);
}
Console.Read();