所以,我在这里找到了计算器,通过套接字发送一个二进制文件,图像..一个代码,所以我用它来测试我的项目C#套接字发送图片JPEG
private void send_ss()
{
byte[] data = new byte[1024];
int sent;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 306);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
}
catch (SocketException e)
{
//Console.WriteLine("Unable to connect to server.");
//Console.WriteLine(e.ToString());
//Console.ReadLine();
}
Bitmap bmp = new Bitmap("C:\\Windows\\Web\\Wallpaper\\Theme2\\img7.jpg");
MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// read to end
byte[] bmpBytes = ms.ToArray();
bmp.Dispose();
ms.Close();
sent = SendVarData(server, bmpBytes);
//Console.WriteLine("Disconnecting from server...");
server.Shutdown(SocketShutdown.Both);
server.Close();
}
private static int SendVarData(Socket s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
byte[] datasize = new byte[4];
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);
while (total < size)
{
sent = s.Send(data, total, dataleft, SocketFlags.None);
total += sent;
dataleft -= sent;
}
return total;
}
,所以我试图发送这张照片上的端口306我的监听套接字的一个(具有m IRC听)
on *:socklisten:ac_img:{
var %p = $ticks $+ $time(hhnnss) $+ $ctime
sockaccept ac_img_ $+ %p
echo -s [] Image Connection Established On -> ac_img_ $+ %p
}
on *:sockread:ac_img_*:{
sockread &picture
bwrite $qt($mIRCdir $+ $sockname $+ .jpg) -1 -1 &picture
}
所以我得到文件,如ac_img_2920385501147471360792067.jpg等。与原始相同的尺寸,但图像只是没有出现,所以我打开这两个文件与文字板,他们有点不同... ...不知道为什么...
所以任何想法,为什么我面临这个问题?我的意思是...我从套接字中获取每一个数据并将它们保存到文件中?也许一个腐败的文件通过C#读取?
你见过这个问题引用发送一个XML文件作为二进制数据,http://stackoverflow.com/questions/14692181/xml-over-tcp -socket/14719589#14719589?也许一个类似的解决方案替换* .xml的* .jpg将为你工作 – MarcF 2013-02-13 22:10:01
这可能不会回答你的问题 - 但你可以让你的代码更多的资源友好只读取jpg文件数据('File.ReadAllBytes()'),而不是转换为位图,然后返回到jpg – 2013-02-13 22:10:06
所以我试了一下你的方法C.Evenhuis 'sent = SendVarData(server,ReadContentFromFile(“C:\\的Windows \\ \\网站\\壁纸\\ THEME2 img7.jpg“)); public static byte [] ReadContentFromFile(String filePath) return {File.ReadAllBytes(filePath); } 仍然相同..所以问题必须在Sendvardata – Devian 2013-02-13 22:21:21