I am developing an application for viewing client screen over TCP socket. my server application is in C# and the client application is in C++. I could send the image to the server without any error. At the server end, while converting buffer length I am getting a big number and this causes 'Overflow exception' while making image buffer with that size. Following are the code I have tried.
 
Server Code
 
- public static void StartListening()  
- {  
-       
-     byte[] bytes = new Byte[1024];  
-   
-       
-       
-       
-     IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");  
-     IPAddress ipAddress = ipHostInfo.AddressList[0];  
-     IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);  
-   
-       
-     Socket listener = new Socket(AddressFamily.InterNetwork,  
-         SocketType.Stream, ProtocolType.Tcp);  
-   
-       
-     try  
-     {  
-         listener.Bind(localEndPoint);  
-         listener.Listen(100);  
-   
-         while (true)  
-         {  
-               
-             allDone.Reset();  
-   
-               
-             Console.WriteLine("Waiting for a connection...");  
-             listener.BeginAccept(  
-                 new AsyncCallback(AcceptCallback),  
-                 listener);  
-   
-               
-             allDone.WaitOne();  
-         }  
-   
-     }  
-     catch (Exception e)  
-     {  
-         Console.WriteLine(e.ToString());  
-     }  
-   
-     Console.WriteLine("\nPress ENTER to continue...");  
-     Console.Read();  
-   
- }  
-   
- public static void AcceptCallback(IAsyncResult ar)  
- {  
-       
-     allDone.Set();  
-   
-       
-     Socket listener = (Socket)ar.AsyncState;  
-     Socket handler = listener.EndAccept(ar);  
-   
-       
-     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)  
- {  
-       
-       
-     StateObject state = (StateObject)ar.AsyncState;  
-     Socket handler = state.workSocket;  
-   
-       
-     int bytesRead = handler.EndReceive(ar);  
-   
-       
-     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;  
-   
-       
-       
-     StateObject state = (StateObject)ar.AsyncState;  
-     Socket handler = state.workSocket;  
-   
-       
-     int bytesRead = handler.EndReceive(ar);  
-   
-   
-     if (bytesRead > 0)  
-     {  
-           
-         Buffer.BlockCopy(state.buffer, 0, _imageBuff, _totBytesRead, bytesRead);  
-   
-         _totBytesRead += bytesRead;  
-   
-         if (_totBytesRead < state.ImageSize)  
-         {  
-               
-             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();  
-   
-   
-   
- hdcScreen = GetDC(NULL);  
- hdcWindow = GetDC(hWnd);  
-   
-   
- hdcMemDC = CreateCompatibleDC(hdcWindow);   
-   
- if(!hdcMemDC)  
- {  
-       
-     goto done;  
- }  
-   
-   
- RECT rcClient;  
- GetClientRect(hWnd,&rcClient);  
-   
-   
- SetStretchBltMode(hdcWindow,HALFTONE);  
-   
-   
- 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;  
- }  
-   
-   
- hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right-rcClient.left,   
- rcClient.bottom-rcClient.top);  
-   
- if(!hbmScreen)  
- {  
-     MessageBox(hWnd, "CreateCompatibleBitmap Failed","Failed", MB_OK);  
-     goto done;  
- }  
-   
-   
- SelectObject(hdcMemDC,hbmScreen);  
-   
-   
- 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;  
-    }  
-   
-   
- 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);     
-   
-   
- DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
-   
-   
- bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);   
-   
-   
- bmfHeader.bfSize = dwSizeofDIB;   
-   
-   
- bmfHeader.bfType = 0x4D42;   
-   
-   
- 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);  
-   
-   
-   
- GlobalUnlock(hDIB);      
- GlobalFree(hDIB);  
-   
-   
- CloseHandle(hFile);  
-   
-   
- 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();  
- }