tehdream 0 Newbie Poster

i have a server and a client(run on the same machine, for testing purposes). this is basically, sorry for it, a help thread. but i've been searching, tracing, retracing, researching for days now to no avail(unlike other problems ive encountered while coding this, i just cant fix or solve this) so yeah.

scenario:

open server
open client
client connects(server automatically listens)
server "looks" at the screen of the client 'no problems

now if the client is closed(by client) or disconnected(by server) 'sending of bitmaps stops

and i reconnect the client

the client goes into "not responding mode"

i can send messages or other commands(disconnect, shutdown, lock) just fine though.

but when i ask for the server to "look at the screen" of the client again... yeah "not responding" - prolly a loop or something.

ive tried tracing it but i really cant find whats wrong.

sidenote: sorry for the way my code is.

CLIENT SIDE:

dim variables

   Dim ipAddress As IPAddress = ipAddress.Parse("127.0.0.1")
   Dim IpEndPoint As IPEndPoint = New IPEndPoint(ipAddress, 8000)

   Dim client As New TcpClient
   Dim nstream As NetworkStream

   Dim byteData(1023) As Byte

when connect button is pressed:

Try
       client = New System.Net.Sockets.TcpClient
       client.BeginConnect(ipAddress, 8085, New AsyncCallback(AddressOf OnConnect), Nothing)
Catch ex As Exception
       MsgBox("Failed to connect...")
       Exit Sub
End Try



 Private Sub OnConnect(ByVal ar As IAsyncResult)

        Try
            client.EndConnect(ar)
        Catch ex As Exception
            MsgBox("Failed to connect...")
            Exit Sub
        End Try

        SetText("Connected") 'settext is for changing GUI (textbox is this case, im sure its not relevant)

        client.Client.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
End Sub

Private Sub OnRecieve(ByVal ar As IAsyncResult)

Dim client As TcpClient = ar.AsyncState

        Try
            client.Client.EndReceive(ar)
        Catch ex As Exception
            Exit Sub
        End Try

        Dim bytesRec As Byte()
        Dim message As String

        bytesRec = Nothing
        message = Nothing

        bytesRec = byteData
        message = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec)

        Read(message) 'read.. well reads the message and checks if its a message or command from the server
        'if you have pressing need to see this Sub just post but im telling you the way i work is really. weird.

        Try
            client.Client.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
        Catch ex As ObjectDisposedException
            Exit Sub
        End Try
    End Sub

when Read() receives a certain from the server and lets say that string was to - tell the client to start sending the bitmaps for monitoring

Timer1.Start()

timer starts :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SendImage()
End Sub

does SendImage() everytick

    Private Sub SendImage()
        Dim bf As New BinaryFormatter
        Try
            nstream = client.GetStream 'client is a TCPClient
            bf.Serialize(nstream, Desktop())
        Catch ex As Exception
            Timer1.Stop()
            MsgBox("Connection Has Been Severed", MsgBoxStyle.Critical, "Connection Severed")
            SetText("Not Connected")
            cmdConnect.Enabled = True
        End Try

    End Sub

Desktop() is

Public Function Desktop() As Image
        Dim bounds As Rectangle = Nothing
        Dim screenshot As System.Drawing.Bitmap = Nothing
        Dim graph As Graphics = Nothing
        bounds = Screen.PrimaryScreen.Bounds
        screenshot = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
        Return screenshot
End Function

SERVER SIDE:

well the server sends the "command" string to the client

 ' tell all clients not to send anything
      For Each client As TcpClient In CoCC 'CollectionofConnectedClients
        Try
            Send("NOTMONITOR", items)
        Catch ex As System.ArgumentOutOfRangeException
            MsgBox("No Freaking Nothing")
            Exit Sub
        End Try
      Next

    ' code to tell a specific client to send bitmaps - be noisy
    Try
        Send("MONITOR890", lsvClients.SelectedItems(0).Tag)
    Catch ex As System.ArgumentOutOfRangeException
        MsgBox("No Freaking Nothing")
        Exit Sub
    End Try

    If continuemonitor = 1 Then ' if error was found on silencing the clients
        continuemonitor = 0 ' return to 0
        Exit Sub '... dont continue
    End If

    endpointmoni = lsvClients.SelectedItems(0).Text ' to tell which client to get from | endpointmoni is a TCPClient
    iwannagetendpoint = 1 ' |iwannagetendpoint is a trigger of sorts
    ContinueGetImage() ' this code to continue getting images

getimage loops

            If iwannagetendpoint = 1 Then
                For Each clnt As TcpClient In CoCC
                    If clnt.Client.LocalEndPoint.ToString = endpointmoni Then
                        tobemonitored = clnt
                        Exit For
                    End If
                Next
            End If

            iwannagetendpoint = 0

            While tobemonitored.Connected = True
                Try
                    nstream = tobemonitored.GetStream
                    Try
                        PictureBox1.Image = bf.Deserialize(nstream)
                    Catch serex As Exception 'serialize something exception
                        StopGetImage() 'getimage(thread) runs on a while this sends a trigger so the while will become false
                        RefreshClients() 'clears listview that show Connected clients and refresh them
                    End Try
                Catch ioex As IOException
                    StopGetImage()
                    RefreshClients()
                End Try
            End While

i think ive put all the relevant codes. if you need anything more then please tell me.

ill be trying to fix this myself while i wait for an answer but... if i can do that.. well.. i would have. im really on my last rope here.

thanks.

drEam