c# - Image processing from the client through TCP socket failed -
i developing application viewing client screen on tcp socket. server application in c# , client application in c++. send image server without error. @ server end, while converting buffer length getting big number , causes 'overflow exception' while making image buffer size. following code have tried.
server code
==========
public static void startlistening() { // data buffer incoming data. byte[] bytes = new byte[1024]; // establish local endpoint socket. // dns name of computer // running listener "host.contoso.com". iphostentry iphostinfo = dns.gethostentry("127.0.0.1"); ipaddress ipaddress = iphostinfo.addresslist[0]; ipendpoint localendpoint = new ipendpoint(ipaddress.parse("127.0.0.1"), 11000); // create tcp/ip socket. socket listener = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); // bind socket local endpoint , listen incoming connections. try { listener.bind(localendpoint); listener.listen(100); while (true) { // set event nonsignaled state. alldone.reset(); // start asynchronous socket listen connections. console.writeline("waiting connection..."); listener.beginaccept( new asynccallback(acceptcallback), listener); // wait until connection made before continuing. alldone.waitone(); } } catch (exception e) { console.writeline(e.tostring()); } console.writeline("\npress enter continue..."); console.read(); } public static void acceptcallback(iasyncresult ar) { // signal main thread continue. alldone.set(); // socket handles client request. socket listener = (socket)ar.asyncstate; socket handler = listener.endaccept(ar); // create state object. stateobject state = new stateobject(); state.worksocket = handler; handler.beginreceive(state.buffer, 0, stateobject.buffersize, 0, new asynccallback(readheadercallback), state); } public static void readheadercallback(iasyncresult ar) { // retrieve state object , handler socket // asynchronous state object. stateobject state = (stateobject)ar.asyncstate; socket handler = state.worksocket; // read data client socket. int bytesread = handler.endreceive(ar); //we need add error handler here...but later state.imagesize = bitconverter.toint32(state.buffer, 0); _imagebuff = new byte[state.imagesize]; _totbytesread = 0; handler.beginreceive(state.buffer, 0, stateobject.buffersize, 0, new asynccallback(readcallback), state); } public static void readcallback(iasyncresult ar) { string content = string.empty; // retrieve state object , handler socket // asynchronous state object. stateobject state = (stateobject)ar.asyncstate; socket handler = state.worksocket; // read data client socket. int bytesread = handler.endreceive(ar); if (bytesread > 0) { // there might more data, store data received far. buffer.blockcopy(state.buffer, 0, _imagebuff, _totbytesread, bytesread); _totbytesread += bytesread; if (_totbytesread < state.imagesize) { // not data received. more. handler.beginreceive(state.buffer, 0, stateobject.buffersize, 0, new asynccallback(readcallback), state); } else { file.writeallbytes(@"c:\temp\incoming\untitled.bmp", _imagebuff); } } } public static int main(string[] args) { startlistening(); return 0; }
client code
void sendscreen() { hdc hdcscreen; hdc hdcwindow; hdc hdcmemdc = null; hbitmap hbmscreen = null; bitmap bmpscreen; hwnd hwnd = getdesktopwindow(); // retrieve handle display device context client // area of window. hdcscreen = getdc(null); hdcwindow = getdc(hwnd); // create compatible dc used in bitblt window dc hdcmemdc = createcompatibledc(hdcwindow); if(!hdcmemdc) { //messagebox(hwnd, l"createcompatibledc has failed",l"failed", mb_ok); goto done; } // client area size calculation rect rcclient; getclientrect(hwnd,&rcclient); //this best stretch mode setstretchbltmode(hdcwindow,halftone); //the source dc entire screen , destination dc current window (hwnd) if(!stretchblt(hdcwindow, 0,0, rcclient.right, rcclient.bottom, hdcscreen, 0,0, getsystemmetrics (sm_cxscreen), getsystemmetrics (sm_cyscreen), srccopy)) { messagebox(hwnd, "stretchblt has failed","failed", mb_ok); goto done; } // create compatible bitmap window dc hbmscreen = createcompatiblebitmap(hdcwindow, rcclient.right-rcclient.left, rcclient.bottom-rcclient.top); if(!hbmscreen) { messagebox(hwnd, "createcompatiblebitmap failed","failed", mb_ok); goto done; } // select compatible bitmap compatible memory dc. selectobject(hdcmemdc,hbmscreen); // bit block transfer our compatible memory dc. if(!bitblt(hdcmemdc, 0,0, rcclient.right-rcclient.left, rcclient.bottom-rcclient.top, hdcwindow, 0,0, srccopy)) { messagebox(hwnd, "bitblt has failed", "failed", mb_ok); goto done; } // bitmap hbitmap getobject(hbmscreen,sizeof(bitmap),&bmpscreen); bitmapfileheader bmfheader; bitmapinfoheader bi; bi.bisize = sizeof(bitmapinfoheader); bi.biwidth = bmpscreen.bmwidth; bi.biheight = bmpscreen.bmheight; bi.biplanes = 1; bi.bibitcount = 32; bi.bicompression = bi_rgb; bi.bisizeimage = 0; bi.bixpelspermeter = 0; bi.biypelspermeter = 0; bi.biclrused = 0; bi.biclrimportant = 0; dword dwbmpsize = ((bmpscreen.bmwidth bi.bibitcount + 31) / 32) 4 * bmpscreen.bmheight; handle hdib = globalalloc(ghnd,dwbmpsize); char lpbitmap = (char )globallock(hdib); getdibits(hdcwindow, hbmscreen, 0, (uint)bmpscreen.bmheight, lpbitmap, (bitmapinfo *)&bi, dib_rgb_colors); handle hfile = createfile("capture.bmp", generic_write, 0, null, create_always, file_attribute_normal, null); // add size of headers size of bitmap total file size dword dwsizeofdib = dwbmpsize + sizeof(bitmapfileheader) + sizeof(bitmapinfoheader); //offset actual bitmap bits start. bmfheader.bfoffbits = (dword)sizeof(bitmapfileheader) + (dword)sizeof(bitmapinfoheader); //size of file bmfheader.bfsize = dwsizeofdib; //bftype must bm bitmaps bmfheader.bftype = 0x4d42; //bm dword dwbyteswritten = 0; writefile(hfile, (lpstr)&bmfheader, sizeof(bitmapfileheader), &dwbyteswritten, null); writefile(hfile, (lpstr)&bi, sizeof(bitmapinfoheader), &dwbyteswritten, null); writefile(hfile, (lpstr)lpbitmap, dwbmpsize, &dwbyteswritten, null); sendingtosocketbfh(bmfheader); sendingtosocketbih(bi); sendingtosocket(lpbitmap,dwbmpsize); //unlock , free dib heap globalunlock(hdib); globalfree(hdib); //close handle file created closehandle(hfile); //clean done: deleteobject(hbmscreen); deleteobject(hdcmemdc); releasedc(null,hdcscreen); releasedc(hwnd,hdcwindow); } void sendingtosocketbfh(bitmapfileheader bfinfo) { dword dwerr=0; int sent=0; sent = send((char*)&bfinfo,sizeof(bfinfo),0); dwerr = getlasterror(); } void sendingtosocketbih(bitmapinfoheader binfo) { dword dwerr=0; int sent=0; sent = send((char*)&binfo,sizeof(binfo),0); dwerr = getlasterror(); } void sendingtosocket(char* cpbuff,int ilen) { dword dwerr=0; int sent=0; sent = send(cpbuff,ilen,0); dwerr = getlasterror(); }
Comments
Post a Comment