Winsock2客户端/服务器 [英] Winsock2 Client/Server

查看:93
本文介绍了Winsock2客户端/服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第2版问题

我正在尝试使用C ++和WinApi编写自己的远程桌面程序.我编写了一个使用"BmpTools.h"捕获屏幕,设置屏幕,获取像素数据数组,压缩像素数据数组等的多类项目.它使用"nSocket.h"创建服务器或客户端并通过套接字发送/接收数据.我现在的问题是,在发送过程中,我的程序似乎毫无原因地挂起.如果有人能告诉我为什么它无缘无故挂起,我将不胜感激.这是我所有的代码.

我从来没有使用过这样的留言板来找到问题的答案,我通常自己找到答案.如果我输入的信息过多和/或难以回答我的问题,请原谅.

我的程序似乎将Client中cout << "Sent Data Info!\nSending Main Data...\n";之后的行和Server中cout << "Recieved Data Info!\n";之后的行挂起.

这是与我的问题相关的代码.

Client.cpp

Question Revision #2

I am trying to write my own remote desktop program using C++ and WinApi. I have written a multi class project that uses "BmpTools.h" to capture the screen, set the screen, get arrays of pixel data, compress arrays of pixel data, etc. It uses "nSocket.h" for creating a server or client and sending/recieving data through sockets. My problem right now is that my program seems to hang for no reason at all in the middle of sending. I would greatly appreciate if someone could tell me why it hangs for no reason. Here is all of my code.

I have never used a message board like this to find an answer to my questions, I usually find the answers on my own. Please excuse me if I entered too much information and/or made it difficult to answer my question.

My program seems to hang the line after cout << "Sent Data Info!\nSending Main Data...\n"; in my Client and at the line after cout << "Recieved Data Info!\n"; in my Server.

Here is the RELEVANT code to my problem.

Client.cpp

#include <windows.h>
#include <iostream>
#include "BmpTools.h"
#include "nSocket.h"

using std::cout;

int main(int argc, char *argv[])
{
     HBITMAP bmp;
     unsigned char *pixels, *cPixels;
     int arraySize, dataSize, lastPacketSize, compBits, compBit1, compBit2, compBit3, compBit4;
     unsigned char sendData[6144];
     int bufferSize = 6144;
     int run = 148;
     cout << "Connecting...\n";
     nSocket client;
     client.nConnect("nyllshanson.selfip.com", 1000);
     cout << "Connected!\n";
     while(run!=SOCKET_ERROR)
     {
          bmp = getScreen();
          delete [] pixels;
          pixels = charGetPixels(bmp);
          delete [] cPixels;
          cPixels = charCompressPixels(pixels);
          compBit1 = cPixels[0];
          compBit2 = cPixels[1];
          compBit3 = cPixels[2];
          compBit4 = cPixels[3];
          compBits = (compBit1*1000000) + (compBit2*10000) + (compBit3*100) + compBit4;
          arraySize = compBits;
          dataSize = compBits / bufferSize;
          lastPacketSize = compBits % bufferSize;
          cout << "Sending Data Info...\n";
          client.nSend((char*)&arraySize, (int)sizeof(arraySize));
          client.nSend((char*)&dataSize, (int)sizeof(dataSize));
          client.nSend((char*)&lastPacketSize, (int)sizeof(lastPacketSize));
          cout << "Sent Data Info!\nSending Main Data...\n";
          for(int i=0;i<dataSize;i++)
          {
               for(int j=0;j<bufferSize;j++)
               {
                    sendData[j] = cPixels[(j+(i*bufferSize))];
               }
               run = client.nSend((char*)sendData, lastPacketSize);
          }
          cout << "Sent Main Data!\nSending Last Packet...\n";
          if(lastPacketSize!=0)
          {
               for(int i=0;i<lastPacketSize;i++)
               {
                    sendData[i] = cPixels[(i+(dataSize*bufferSize))];
               }
               run = client.nSend((char*)sendData, bufferSize);
          }
          cout << "Sent Last Packet!\n";
     }
     client.nClose();
     client.nCleanup();
}



Server.cpp



Server.cpp

#include <windows.h>
#include <iostream>
#include "BmpTools.h"
#include "nSocket.h"

using std::cout;

int main(int argc, char *argv[])
{
     HBITMAP bmp;
     unsigned char *pixels, *uPixels;
     int arraySize, dataSize, lastPacketSize;
     unsigned char recvData[6144];
     int bufferSize = 6144;
     int run;
     cout << "Starting Server...\n";
     nSocket server;
     server.nStartServer(1000);
     cout << "Server Started!\n";
     while(true)
     {
          cout << "Waiting For Connection...\n";
          server.nAcceptConnection();
          cout << "Accepted Connection!\n";
          run = 148;
          while(run!=SOCKET_ERROR)
          {
               cout << "Recieving Data Info...\n";
               server.nRecv((char*)&arraySize, (int)sizeof(arraySize));
               server.nRecv((char*)&dataSize, (int)sizeof(dataSize));
               server.nRecv((char*)&lastPacketSize, (int)sizeof(lastPacketSize));
               cout << "Recieved Data Info!\n";
               delete [] pixels;
               pixels = new unsigned char[arraySize];
               cout << "Recieving Main Data...\n";
               for(int i=0;i<dataSize;i++)
               {
                    server.nRecv((char*)recvData, bufferSize);
                    for(int j=0;j<bufferSize;j++)
                    {
                         pixels[(j+(i*bufferSize))] = recvData[j];
                    }
               }
               cout << "Recieved Main Data!\nRecieving Last Packet...\n";
               if(lastPacketSize!=0)
               {
                    server.nRecv((char*)recvData, bufferSize);
                    for(int i=0;i<lastPacketSize;i++)
                    {
                         pixels[(i+(dataSize*bufferSize))] = recvData[i];
                    }
               }
               cout << "Recieved Last Packet!\n";
               delete [] uPixels;
               uPixels = charUncompressPixels(pixels);
               bmp = charSetPixels(uPixels);
               setScreen(bmp);
          }
     }
}



如果您需要更多代码以供参考,请告诉我,我将其备份,这一次确保不要在HTML中对其进行双重编码.在此先感谢您的帮助.



If you need any more code for reference just let me know and I will put it back up, this time making sure that I dont double encode it in HTML. Thanks in advance for helping.

推荐答案

这是一堆几乎不相关的代码,已经过双重HTML编码(因此我们甚至无法复制和将其粘贴到IDE中并运行).也许,如果您告诉我们问题的根源,以及为什么此时感觉什么也不做",您离开该设备多长时间以查看问题是否恢复,等等.
That is a TON of mostly irrelevant code, that''s been double HTML encoded ( so we can''t even copy and paste it into an IDE and run it ). Perhaps if you told us the line of the problem, and why you feel it''s doing ''nothing'' at that point, how long you''ve left it to see if it recovers, etc.


这篇关于Winsock2客户端/服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆