使用socket传输文件 FormatException?那应该和send没什么关系,你的代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考:http://tech.ccidnet.com/pub/disp/Article?columnID=322&articleID=56279&pageNO=1 就是啊,应该没什么关系的。.NET我没用过sleep,用wait可能好点。 client:clientSocket.Connect(endPoint);byte []point1 = new byte [18];point1 = Encoding.ASCII.GetBytes(client_IP.ToString()+":"+txt_clientPort.Text.ToString()+":");clientSocket.Send(point1);Thread.Sleep(200);byte[] b_flength = Encoding.ASCII.GetBytes(file_length.ToString());clientSocket.Send(b_flength);Thread.Sleep(200); byte[] b_type = new byte[1];b_type = Encoding.ASCII.GetBytes("1");clientSocket.Send(b_type);Thread.Sleep(200);while(bytes_num < file_length){ bytes = one.Read(in_read,0,1024); clientSocket.Send(in_read,0,bytes,SocketFlags.None ); bytes_num += bytes;}one.Close ();clientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);clientSocket.Close();server:client = serverSocket.Accept();byte []endPoint = new byte[18];client.Receive(endPoint);string point2 = Encoding.ASCII.GetString (endPoint);byte []temp = new byte[50];client.Receive(temp);long file_length = Int64.Parse(Encoding.ASCII.GetString (temp));byte []type = new byte[1];client.Receive(type);if (file_length>0){ if (type[0] == 49) { string savePath = ""; SaveFileDialog saveLog = new SaveFileDialog(); // Initialize the SaveFileDialog to specify the Log extension for the file. saveLog.DefaultExt = "*.bin"; saveLog.Filter = "Files|*.*"; // Determine if the user selected a file name from the saveFileDialog. if(saveLog.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveLog.FileName.Length > 0) { savePath = saveLog.FileName; } if (savePath.Equals("")) { client.Shutdown(System.Net.Sockets.SocketShutdown.Receive); client.Close(); return; } FileStream two = File.Create (@savePath); byte[] to_write = new byte [1024]; int bytes = 0; int bytes_num = 0; while(bytes_num < file_length) { bytes = client.Receive (to_write,1024,0); two.Write (to_write,0,bytes); bytes_num+=bytes; } two.Close (); }}这是大概的几行,应该可以看懂意思吧,有点乱。 「玩一玩」绘制投影直方图(我希望各位需要的能自己动手写下) 求详解.我菜鸟 怎理解IM软件创建数据库连接对象SqlConnection 不能定义为static? 怎么方便操作datatable 各路大侠来帮忙 我装了3G多的vs2005,里面没有C#语言规范??? 请问这个XML怎么读取,感觉好奇怪 100分求c#短信收发源代码 owc.chartspace.datasource C#怎样读取数据库中的OLE对象??? 无法访问名为“Note”的已处置对象???? C#中的datagrid绑定后标题改成自己要的中文怎么改和列怎么隐藏起来?
http://tech.ccidnet.com/pub/disp/Article?columnID=322&articleID=56279&pageNO=1
clientSocket.Connect(endPoint);
byte []point1 = new byte [18];
point1 = Encoding.ASCII.GetBytes(client_IP.ToString()+":"+txt_clientPort.Text.ToString()+":");
clientSocket.Send(point1);
Thread.Sleep(200);byte[] b_flength = Encoding.ASCII.GetBytes(file_length.ToString());clientSocket.Send(b_flength);
Thread.Sleep(200);
byte[] b_type = new byte[1];
b_type = Encoding.ASCII.GetBytes("1");
clientSocket.Send(b_type);
Thread.Sleep(200);while(bytes_num < file_length)
{
bytes = one.Read(in_read,0,1024);
clientSocket.Send(in_read,0,bytes,SocketFlags.None );
bytes_num += bytes;
}
one.Close ();
clientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
clientSocket.Close();server:
client = serverSocket.Accept();byte []endPoint = new byte[18];client.Receive(endPoint);
string point2 = Encoding.ASCII.GetString (endPoint);byte []temp = new byte[50];client.Receive(temp);
long file_length = Int64.Parse(Encoding.ASCII.GetString (temp));byte []type = new byte[1];
client.Receive(type);if (file_length>0)
{
if (type[0] == 49)
{
string savePath = "";
SaveFileDialog saveLog = new SaveFileDialog(); // Initialize the SaveFileDialog to specify the Log extension for the file.
saveLog.DefaultExt = "*.bin";
saveLog.Filter = "Files|*.*"; // Determine if the user selected a file name from the saveFileDialog.
if(saveLog.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveLog.FileName.Length > 0)
{
savePath = saveLog.FileName;
}
if (savePath.Equals(""))
{
client.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
client.Close();
return;
}
FileStream two = File.Create (@savePath); byte[] to_write = new byte [1024];
int bytes = 0;
int bytes_num = 0; while(bytes_num < file_length)
{
bytes = client.Receive (to_write,1024,0);
two.Write (to_write,0,bytes);
bytes_num+=bytes;
} two.Close ();
}
}这是大概的几行,应该可以看懂意思吧,有点乱。