Question TCP Client/Server problem

Daybreak

New member
Joined
Sep 26, 2008
Messages
1
Programming Experience
1-3
I have a Tcp Client which gets a screenshot, writes it to a memorystream, and then sends it to the server.When i have both the client and Server on one computer i recieve the whole image.When i use my computer as the Client and another as the Server, I can't seem to send the whole image-just a small portion of it.The other way around-when my computer is the server,i recieve the whole image from the other computers' desktop,but then the program stops working.I've tried this with two diffrent computers and i got the same result.

Any assistance would be greatly apreciated

Here is the code for the CLient:

Public Class Client
Dim Stream As NetworkStream
Dim MS As New MemoryStream


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim Client As New TcpClient

Client.Connect(IPAddress.Parse(TextBox1.Text), 13000)
MsgBox("Connection Established")
Stream = Client.GetStream
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
Private Function PrintScreen() As Bitmap

Dim bounds As Rectangle
Dim ScreenShot As System.Drawing.Bitmap
Dim graph As Graphics

bounds = Screen.PrimaryScreen.Bounds
ScreenShot = New System.Drawing.Bitmap(bounds.Width, bounds.Height)
graph = Graphics.FromImage(ScreenShot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)

Return ScreenShot

End Function

Public Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


Dim ScreenShot As Bitmap = PrintScreen()
ScreenShot.Save(MS, Imaging.ImageFormat.Jpeg)

Dim ByteArray(MS.Length) As Byte
ByteArray = MS.ToArray

Dim Reader As New BinaryReader(MS)
Dim Writer As New BinaryWriter(Stream)
Try

Writer.Write(CInt(MS.Length))
Writer.Write(ByteArray, 0, MS.Length)
Writer.Flush()

Catch ex As Exception
MsgBox(ex.Message)
End Try
MS.Seek(0, SeekOrigin.Begin)

End Sub
End Class

And Here is the Server Code:

Dim Server As New TcpListener(IPAddress.Any, 13000)
Dim Client As New TcpClient
Dim Reader As BinaryReader
Dim DataLenght As UInt32

Server.Start()
Client = Server.AcceptTcpClient
Reader = New BinaryReader(Client.GetStream)

Try

DataLenght = Reader.ReadUInt32
Dim ReadBuffer(DataLenght) As Byte
Reader.Read(ReadBuffer, 0, ReadBuffer.Length)

Dim Stream As New MemoryStream(ReadBuffer)
Dim Screenshot As Bitmap = Bitmap.FromStream(Stream)
Stream.Seek(0, SeekOrigin.Begin)
Me.PictureBox1.Image = Screenshot
PictureBox1.Invalidate()

Catch ex As Exception
MsgBox(ex.Message)
End Try
 
Back
Top