2013-02-13 62 views
1

所以,我在这里找到了计算器,通过套接字发送一个二进制文件,图像..一个代码,所以我用它来测试我的项目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等。与原始相同的尺寸,但图像只是没有出现,所以我打开这两个文件与文字板,他们有点不同... ...不知道为什么... ScreenShot

所以任何想法,为什么我面临这个问题?我的意思是...我从套接字中获取每一个数据并将它们保存到文件中?也许一个腐败的文件通过C#读取?

+0

你见过这个问题引用发送一个XML文件作为二进制数据,http://stackoverflow.com/questions/14692181/xml-over-tcp -socket/14719589#14719589?也许一个类似的解决方案替换* .xml的* .jpg将为你工作 – MarcF 2013-02-13 22:10:01

+1

这可能不会回答你的问题 - 但你可以让你的代码更多的资源友好只读取jpg文件数据('File.ReadAllBytes()'),而不是转换为位图,然后返回到jpg – 2013-02-13 22:10:06

+0

所以我试了一下你的方法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

回答

2

图像是不同的,因为你读它,解析成Bitmap并重新编码它。写字板屏幕截图显示,无论是JPEG的,但有不同的元数据(例如,“土坯”失踪“)。

只需使用File.ReadAllBytes或其他无损方法来读取图像。

发送代码看起来声音。不知道为什么你在循环发送从未做部分IOs AFAIK在阻止套接字

+0

我用file.readallbytes和解决了这个问题,但另一个出现..我不知道为什么......但我收到一些额外的字节.. 我mIRC的出现: '[数据]共有大小158116'但我的C#'出现 158112' – Devian 2013-02-13 22:55:21

+3

@Devian - 将接收4个额外的字节是肯定,因为你在的形式发送4个额外的字节在图像数据之前发送的'datasize'(一个4字节的整数)。 – Iridium 2013-02-13 23:00:22

+0

真是该死的,对不起,在4k行代码之后,你有点不好意思。 – Devian 2013-02-13 23:05:38