c# - Why does this message appear when I run this code? -


i wrote code sends image between sender , receiver.

first must run receiver run sender.

when test code on images between 1kb , 1.5kb, works fine, when try send larger image, message appears

cannot evaluate expression because native ..........

my code below, can help?

 using system;     using system.collections.generic;     using system.linq;     using system.text;      using system.net;     using system.net.sockets;     using system.io;     using system.drawing;     using system.drawing.imaging;     using system.windows.forms;     using system.threading;      namespace updtester     {         public class udpclass         {              public image merge(queue<byte[]> mylist)             {                 int imgheight = bitconverter.toint32(mylist.dequeue(), 0);                 int imgwidth = bitconverter.toint32(mylist.dequeue(), 0);                  bitmap bmp = new bitmap(imgwidth, imgheight);                 graphics g = graphics.fromimage(bmp);                  int x, y = 0;                 while (mylist.count > 0)                 {                     x = bitconverter.toint32(mylist.dequeue(), 0);                     y = bitconverter.toint32(mylist.dequeue(), 0);                     g.drawimage(bytetobitmapconverter(mylist.dequeue()), x, y);                 }                 return bmp;             }              /// <summary>             /// image segmentatoin.             /// img: image divided.             /// </summary>             public queue<byte[]> segmentation(bitmap img)             {                 queue<byte[]> bytearray = new queue<byte[]>();                  bytearray.enqueue(bitconverter.getbytes(img.width));                 bytearray.enqueue(bitconverter.getbytes(img.height));                                      image temimg;                  (ushort x = 0; x < img.width - 5; x += 5)                 {                     (ushort y = 0; y < img.height - 5; y += 5)                     {                         //temimg = null;                         temimg = img.clone(new rectangle(x, y,  5,  5), pixelformat.format32bppargb);                         bytearray.enqueue(bitconverter.getbytes(x));                         bytearray.enqueue(bitconverter.getbytes(y));                                             bytearray.enqueue(imagetobyteconverter(temimg));                     }                 }                 return bytearray;             }              //sender             public void senderudp(bitmap img)             {                 byte[] data = new byte[1024];                             ipendpoint ipep = new ipendpoint(ipaddress.parse("127.0.0.1"), 9050);                  socket server = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);                  string welcome = "hello, there?";                 data = encoding.ascii.getbytes(welcome);                  server.sendto(data, data.length, socketflags.none, ipep);                  ipendpoint sender = new ipendpoint(ipaddress.any, 0);                 endpoint remote = (endpoint)sender;                  data = new byte[1024];                 int recv = server.receivefrom(data, ref remote);                  messagebox.show("message received from");                 messagebox.show(encoding.ascii.getstring(data, 0, recv));                  queue<byte[]> temlist = segmentation(img);                 data = new byte[1024];                 data =temlist.dequeue();                 //send width of image.                 server.sendto(data, data.length, socketflags.none, ipep);                  data = new byte[1024];                 data = temlist.dequeue();                 //send height of image.                 server.sendto(data, data.length, socketflags.none, ipep);                  data = bitconverter.getbytes(temlist.count);                 //send count of list.                 server.sendto(data, data.length, socketflags.none, ipep);                 messagebox.show(temlist.count.tostring() + " iam sender");                  while (temlist.count > 0)                 {                     server.sendto(temlist.dequeue(), remote);                     //messagebox.show(temlist.count.tostring() + "s");                 }                 //server.close();             }              //receiver..(ip, portnum)             public image receiverudp()             //public void receiverudp(ref picturebox pic)             {                 int recv;                 byte[] data = new byte[1024];                 ipendpoint ipep = new ipendpoint(ipaddress.any, 9050);                  socket newsock = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);                  newsock.bind(ipep);                 messagebox.show("waiting client....");                 //console.writeline("waiting client....");                  ipendpoint sender = new ipendpoint(ipaddress.any, 0);                 endpoint remote = (endpoint)(sender);                  recv = newsock.receivefrom(data, ref remote);                  messagebox.show("message received ", remote.tostring());                 messagebox.show(encoding.ascii.getstring(data, 0, recv));                 //console.writeline("message received {0}:", remote.tostring());                 //console.writeline(encoding.ascii.getstring(data, 0, recv));                  string welcome = "welcome test server";                 data = encoding.ascii.getbytes(welcome);                 newsock.sendto(data, data.length, socketflags.none, remote);                  queue<byte[]> templist = new queue<byte[]>();                  //receive width of image.                 newsock.receivefrom(data, ref remote);                 templist.enqueue(data);                  //receive height of image.                 newsock.receivefrom(data, ref remote);                 templist.enqueue(data);                  //reccive count of list.                 newsock.receivefrom(data, ref remote);                 int count = bitconverter.toint32(data, 0);                 messagebox.show(count.tostring() + " iam receiver");                  data = new byte[1024];                 while (count > 0)                 {                                     data = new byte[1024];                      newsock.receivefrom(data, ref remote);                     templist.enqueue(data);                     data = new byte[1024];                      newsock.receivefrom(data, ref remote);                     templist.enqueue(data);                     data = new byte[1024];                      newsock.receivefrom(data, ref remote);                     templist.enqueue(data);                      messagebox.show(count.tostring());                         count -= 3;                 }                   return merge(templist);             }              private byte[] imagetobyteconverter(image img)             {                 memorystream ms = new memorystream();                 img.save(ms, imageformat.png);                 return ms.toarray();             }              private image bytetobitmapconverter(byte[] buffer)             {                 memorystream ms = new memorystream(buffer);                 image img = image.fromstream(ms);                 return img;             }         }     } 

most reason locks because udp doesn't guarantee delivery. packets can dropped. if happens, receive loop going sit there waiting forever packet won't ever come.


Comments