通过UDP发送图像 [英] Send Image through UDP

查看:87
本文介绍了通过UDP发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试制作一个远程访问工具,可以从服务器计算机监视客户端计算机.
为了获得客户的计算机桌面视图,我的程序拍摄了屏幕快照,保存了文件并发送
文件到远程服务器计算机.这是客户端的主要代码-

Hi guys,
I am trying to make a remote access tool where a client computer can be monitored from a server computer.
For getting the client''s computer desktop view, my programme take screen shots, save the file and send the
file to remote server computer. Here is the main code for the client-

<br />
<pre lang="vb"><br />
Private Sub Live_pc()<br />
        Dim cmnd As String<br />
        Dim fs As FileStream<br />
        Dim flag As Boolean = True<br />
        Dim b As Bitmap = New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, Imaging.PixelFormat.Format32bppArgb)<br />
        Dim gfx As Graphics = Graphics.FromImage(b)<br />
        Dim filesize As Int64<br />
        Dim buffer() As Byte<br />
<br />
        Do<br />
            cmnd = bReader.ReadString()<br />
<br />
            Select Case cmnd<br />
                Case "Auto_Capture"<br />
                    While flag<br />
                        gfx.CopyFromScreen(My.Computer.Screen.Bounds.X, My.Computer.Screen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)<br />
                        b.Save("temp.xxx", Imaging.ImageFormat.Jpeg)<br />
                        fs = File.OpenRead("temp.xxx")<br />
                        ReDim buffer(fs.Length - 1)<br />
                        filesize = fs.Length<br />
                        fs.Read(buffer, 0, fs.Length)<br />
                        bWriter.Write(filesize)<br />
                        bWriter.Write(buffer, 0, fs.Length)<br />
                        fs.Close()<br />
                        flag = bReader.ReadBoolean<br />
                        Threading.Thread.Sleep(30)<br />
                    End While<br />
                Case "Stop_Auto_Capture"<br />
                Case "exit"<br />
                    b.Dispose()<br />
                    fs.Dispose()<br />
                    gfx.Dispose()<br />
                    Array.Clear(buffer, 0, buffer.Length)<br />
                    Exit Do<br />
<br />
            End Select<br />
        Loop<br />
    End Sub<br />
</pre><br />
and the code for server which receive files-<br />
<pre lang="vb"><br />
<br />
Imports System.IO<br />
Imports System.Net<br />
Imports System.Text<br />
Imports System.Drawing<br />
Imports System.Threading<br />
Imports System.Net.Sockets<br />
<br />
<br />
Public Class frmLivePC<br />
    Private tcpclient As New TcpClient<br />
    Private BR As BinaryReader<br />
    Private BW As BinaryWriter<br />
    Private CaptureFlag As Boolean<br />
<br />
    Private Sub frmLivePC_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing<br />
        BW.Write("exit")<br />
    End Sub<br />
<br />
        Public Sub New(ByVal client As TcpClient)<br />
<br />
        '' This call is required by the Windows Form Designer.<br />
        InitializeComponent()<br />
        tcpclient = client<br />
        BR = New BinaryReader(tcpclient.GetStream())<br />
        BW = New BinaryWriter(tcpclient.GetStream())<br />
        '' Add any initialization after the InitializeComponent() call.<br />
<br />
    End Sub<br />
<br />
      Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click<br />
        BW.Write("Auto_Capture")<br />
        CaptureFlag = True<br />
        btnStart.Enabled = False<br />
        btnStop.Enabled = True<br />
<br />
        Dim AutoCapturingThread As New Thread(AddressOf AutoCapture)<br />
        AutoCapturingThread.Start()<br />
    End Sub<br />
<br />
    Private Sub AutoCapture()<br />
        While CaptureFlag<br />
            Dim fs As FileStream<br />
            Dim filesize As Int64<br />
            filesize = BR.ReadInt64<br />
            Dim BUFFER_SIZE As Int32 = 4096<br />
            Dim BUFFER(BUFFER_SIZE - 1) As Byte<br />
            Dim byteread As Integer<br />
<br />
            fs = File.Create("temp.jpg")<br />
<br />
            While filesize<br />
                If filesize < BUFFER_SIZE Then BUFFER_SIZE = filesize<br />
                byteread = BR.Read(BUFFER, 0, BUFFER_SIZE)<br />
                fs.Write(BUFFER, 0, byteread)<br />
                filesize -= byteread<br />
            End While<br />
            fs.Close()<br />
            PictureBox.ImageLocation = "temp.jpg"<br />
            BW.Write(CaptureFlag)<br />
        End While<br />
    End Sub<br />
<br />
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click<br />
        CaptureFlag = False<br />
        BW.Write(CaptureFlag)<br />
    End Sub<br />
End Class<br />
<br />
</pre><br />
<br />
I''m only providing the main part of the code. An active TCP connection is already made between server and client, <br />
and the client starts the Live_pc() sub as the server starts frmLivePc Form. Now, my programme woks fine but having <br />
some performance issue. Most of the times it hangs or the frames <br />
are shown at a slow rate. I know i can get better performance if<br />
i use UDP instead of TCP. But the problem is that the jpg files are around 110-140kb and i cant send these through<br />
UDP. Using "UDPClient.send" method gives me a error telling that tthe datagram is too big to send over network.<br />
Now i would like you to help me here. How can i get better performance? Is there any modification required or<br />
do i have to use UDP and if so how would i do that? And i have a second question- the thing i''m trying to do here,<br />
can it be done by forwarding the video memory of client''s computer in the network? Some code about this would<br />
be very helpfull. Thanks in advance :)

推荐答案

可以使用UDP通过块来发送大量数据.然后,您需要对其进行哈希处理,然后将其发送.但是,它们无法跟踪是否有任何块遗漏等.所有这些都是内置TCP,您可以免费获得它.因此,我不建议修改UDP传输并最终到达TCP.

您可以使用一些网络服务器.

可能会有帮助:
使用C#.NET的远程桌面 [ http://kishordgupta.wordpress.com /2011/02/03/controlling-pc-and-remote-access-via-lan-in-c/ [
Sending huge data can be done by chunks using UDP. Then you need to hash it, send it. But ther''s no way to track if any chunk misses etc. All these are built-in TCP and you get it free. So I would not suggest modifying UDP transmission and ultimately reaching TCP.

You could use some webserver.

May be of some help:
Remote Desktop using C#.NET[^]

http://kishordgupta.wordpress.com/2011/02/03/controlling-pc-and-remote-access-via-lan-in-c/[^]

Thanks,

Kuthuparakkal


这篇关于通过UDP发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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