简体   繁体   English

通过套接字C ++服务器和C#客户端发送图像

[英]Image sending through socket C++ server and C# client

I'm developing a windows store application, using C#. 我正在使用C#开发Windows商店应用程序。 I would like to make TCP connection to receive images (for now) from a desktop server. 我想建立TCP连接以从台式机服务器接收图像(目前)。 the server is in C++ . 服务器是C ++。

I have a client C++ to test the function and it is working perfectly. 我有一个客户端C ++来测试该功能,并且运行良好。 Now what i want is a similar client but in C# . 现在我想要的是一个类似的客户端,但是使用C#。 I tried converting it but no luck, i tried to use the same logic but i had tons of errors and deleted everything. 我尝试转换它,但是没有运气,我尝试使用相同的逻辑,但是我有很多错误并删除了所有内容。

Help is appreciated,thanks. 感谢您的帮助,谢谢。

C++ Server C ++服务器

int size = 8192;     //image size
char* bufferCMP;
bufferCMP = (char*)malloc(sizeof(char)* size);
FILE *p_file;
p_file = fopen("C:\\Program Files\\img1.png", "rb");
fread(bufferCMP, 1, size, p_file);
fclose(p_file);


int chunkcount = size / DEFAULT_BUFLEN;
int lastchunksize = size - (chunkcount * DEFAULT_BUFLEN);
int fileoffset = 0;

printf("Sending actual Chunk");
while (chunkcount > 0)
{
    iResult = send(ClientSocket, bufferCMP + (fileoffset * DEFAULT_BUFLEN), DEFAULT_BUFLEN, 0);
    fileoffset++;
    chunkcount--;

    if (iResult != DEFAULT_BUFLEN)
    {
        printf("Sending Buffer size <> Default buffer length  ::: %d\n");
    }
    else
    {
        printf("Sending Buffer size = %d \n", iResult, fileoffset);
    }
}

printf("Sending last Chunk", lastchunksize);
iResult = send(ClientSocket, bufferCMP + (fileoffset * DEFAULT_BUFLEN), lastchunksize, 0);

` `

C++ Client (to be converted into C#) C ++客户端(将转换为C#)

int size = 8192;


int FileCounter = 0;
bool flg = true;
char * fileComplete;
char * filesizeBuffer;

FILE *temp;

int receiveBuffer = 0;
int desiredRecBuffer = size;
//int desiredRecBuffer = DEFAULT_BUFLEN ; 
fileComplete = (char*)malloc(sizeof(char)* size);

while (desiredRecBuffer > 0)
{
    iResult = recv(ConnectSocket, fileComplete + receiveBuffer, desiredRecBuffer, 0);
    //iResult = recv( ClientSocket, fileComplete + receiveBuffer , fileSize , 0 );

    if (iResult < 1)
    {
        printf("Reveive Buffer Error  %d \n", WSAGetLastError());
    }
    else
    {
        receiveBuffer += iResult;
        desiredRecBuffer = size - receiveBuffer;
        printf("Reveived Data size :  %d \n", desiredRecBuffer);

    }
}

FILE *File = fopen("C:\\Users\\amirk_000\\Pictures\\img1b.png", "wb");
fwrite(fileComplete, 1, size, File);
//flg = true;
free(fileComplete);
fclose(File);

Full example of C# client socket is available at MSDN MSDN上提供了C#客户端套接字的完整示例

Modify the given SocketSendReceive method to write the received buffer ( bytesReceived array) to a file stream. 修改给定的SocketSendReceive方法,将接收到的缓冲区( bytesReceived数组)写入文件流。

Something like the following should do it: 应该执行以下操作:

    using (var file = File.OpenWrite("myimage.png"))
    {
        do
        {
            bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
            file.Write(bytesReceived, 0, bytes);
        }
        while (bytes > 0);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM