Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Tcp datagrams and other questions.....

Îåêßíçóå áðü ôï ìÝëïò cs010327. Τελευταία δημοσίευση από το μέλος sakalis στις 10-11-2009, 15:04. Υπάρχουν 3 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  10-11-2009, 11:59 55089

    Tcp datagrams and other questions.....

    Έχω ένα project στην σχολή όπου η εργασία είναι η εξής, μια άλλη,ομάδα θα φτιάξει έναν server ,όπου σκοπός μου είναι η υλοποίησει ενός client που θα στελνει διάφορα datagrams ,όπου θα τελειώνουν με @@@ ,ανάλογα με το τί συμβαίνει σε μία βάση.


    Το communication του interface  έχει ως εξής :

    IP is used as communications protocol

    TCP datagram's are defined and sent exclusively to avoid transmission problems at network level

    Client must be configured as tcp/ip client.


    Α datagram has the following syntax

    Size                 token |  Token Value @@@

    Packet Type    Value|…….

    Three @@@ character mark the end of a datagram.

    The packet size indicates the length of the datagram (ASCII character string) including three @-characters in bytes. Data type :UINT

    Ερώτηση 1) Τα datagrams δεν χρησιμοποιούνται μόνο με udp protocol???,


    Από ένα παράδειγμα που βρήκα στο msdn για έναν asychronious client , έκανα την παρακάτω υλοποιήσει


    sto module
    Imports System.Net.Sockets
    Imports System.net
    Imports System.Text
    Imports System.Net.DnsPermissionAttribute
    Imports System.Security.Permissions

    Public Class StateObject
        ' Client socket.
        Public workSocket As Socket = Nothing
        ' Size of receive buffer.
        Public Const BufferSize As Integer = 256
        ' Receive buffer.
        Public buffer(BufferSize) As Byte
        ' Received data string.
        Public sb As New StringBuilder
    End Class 'StateObject

    Public Class AsynchronousClient
        ' The port number for the remote device.
        'Private Const port As Integer = 11000

        ' ManualResetEvent instances signal completion.
        Public Shared connectDone As New Threading.ManualResetEvent(False)
        Public Shared sendDone As New Threading.ManualResetEvent(False)
        Public Shared receiveDone As New Threading.ManualResetEvent(False)


        ' The response from the remote device.
        Private Shared response As String = String.Empty

        Public Shared Sub Main(ByVal port As Integer)
            ' Establish the remote endpoint for the socket.
            ' For this example use local machine.
            Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
            Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
            Dim remoteEP As New IPEndPoint(ipAddress, port)


            ' Create a TCP/IP socket.
            Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

            ' Connect to the remote endpoint.
            client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client)

            ' Wait for connect.
            If client.Connected = True Then
                CClient.CmdBox.Items.Add("Connected")
            Else
                CClient.CmdBox.Items.Add("Not Connected")
                ' Main("5100")
            End If


            ' Send test data to the remote device.
            Send(client, "This is a test@@@")
            sendDone.WaitOne()

            ' Receive the response from the remote device.
            Receive(client)
            receiveDone.WaitOne()

            ' Write the response to the console.
            CClient.CmdBox.Items.Add("Response received : {0}" + response)

            ' Release the socket.
            client.Shutdown(SocketShutdown.Both)
            client.Close()
        End Sub 'Main


        Public Shared Sub ConnectCallback(ByVal ar As IAsyncResult)
            ' Retrieve the socket from the state object.
            Dim client As Socket = CType(ar.AsyncState, Socket)
            Try
                ' Complete the connection.
                client.EndConnect(ar)

                CClient.CmdBox.Items.Add("Socket connected to {0}" + client.RemoteEndPoint.ToString())

                ' Signal that the connection has been made.
                connectDone.Set()

            Catch ex As Exception
                CClient.CmdBox.Items.Add("Not Connected")
                client.Close()
            End Try


        End Sub 'ConnectCallback


        Public Shared Sub Receive(ByVal client As Socket)

            ' Create the state object.
            Dim state As New StateObject
            state.workSocket = client

            ' Begin receiving the data from the remote device.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
        End Sub 'Receive


        Public Shared Sub ReceiveCallback(ByVal ar As IAsyncResult)
            Dim content As String = String.Empty
            ' Retrieve the state object and the client socket
            ' from the asynchronous state object.
            Dim state As StateObject = CType(ar.AsyncState, StateObject)
            Dim client As Socket = state.workSocket

            ' Read data from the remote device.
            Dim bytesRead As Integer = client.EndReceive(ar)

            If bytesRead > 0 Then

                ' There might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))

                content = state.sb.ToString()
                If content.IndexOf("@@@") > -1 Then
                    ' Get the rest of the data.
                    Console.WriteLine("Read {0} bytes from socket. " + vbLf + " Data : {1}", content.Length, content)
                    Send(client, content)
                    receiveDone.Set()
                Else
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
                End If
            End If
        End Sub 'ReceiveCallback


        Public Shared Sub Send(ByVal client As Socket, ByVal data As String)
            ' Convert the string data to byte data using ASCII encoding.
            Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)

            ' Begin sending the data to the remote device.
            client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
        End Sub 'Send


        Public Shared Sub SendCallback(ByVal ar As IAsyncResult)
            ' Retrieve the socket from the state object.
            Dim client As Socket = CType(ar.AsyncState, Socket)

            ' Complete sending the data to the remote device.
            Dim bytesSent As Integer = client.EndSend(ar)
            CClient.CmdBox.Items.Add("Sent {0} bytes to server.")

            ' Signal that all bytes have been sent.
            sendDone.Set()
        End Sub 'SendCallback

    End Class 'AsynchronousClient


    στιν Forma

    Public Class CClient



        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
            't.workSocket = client


            AsynchronousClient.Main(Port_T.Text)



        End Sub

        Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Srv_T.Text = "Localhost"
            Port_T.Text = "5100"
        End Sub

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


        End Sub

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tystr.Click
            Dim ar As IAsyncResult
            Dim client As Net.Sockets.Socket = CType(ar.AsyncState, Net.Sockets.Socket)
            AsynchronousClient.Send(client, "TYSTRAT test @@@")

        End Sub
    End Class


    Είναι σωστός ο τρόπος με τον οποίο στέλνω (και αν στέλνω) το datagram?

    Γιατί δεν μπορώ να μέσα από έναν timer να πάρω το status του socket πχ αν είναι connected, αν έβγαλε error κπλ.?

    Είναι σωστός ο τρόπος υλοποιήσεις του Client.?

    Επίσης όταν τρέχω το πρόγραμμα μου κολλάει γιατί δέν βρίσκει τον server (naturally) ,πως γίνέται απλός να μου γράψει στο cmdbox ‘not connected’ και να μην κολλήσει??

  •  10-11-2009, 13:31 55099 σε απάντηση της 55089

    Απ: Tcp datagrams and other questions.....

    Σε παρακαλώ καθάρισε πρώτα τη δημοσίευσή σου από τα σκουπίδια για να καταφέρουμε να τη διαβάσουμε

    Γιώργος Σακαλής
  •  10-11-2009, 13:37 55101 σε απάντηση της 55099

    Απ: Tcp datagrams and other questions.....

    I fixed it. Κανει πρέπει να έκανε το Word...
  •  10-11-2009, 15:04 55103 σε απάντηση της 55101

    Απ: Tcp datagrams and other questions.....

    θα σου πρότεινα να διαβάσεις το παρακάτω βιβλίο πρώτα το οποίο θα σου λύσει πολλές από τις απορίες σου




    Γιώργος Σακαλής
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems