0

I have created client and server application

i am trying to send the Arraylist from cilent to server the Arraylist is transfer from client but how to use that Array list in server to get the information of ArrayList

while i am printing the message in server side it showing System.collection.ArrayList

below is my code

Client Code

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        Client = New TcpClient("192.168.0.226", 8080)
        Dim Writer As New StreamWriter(Client.GetStream())

        detailList.Add(txtname.Text)
        detailList.Add(txtadd.Text)

        For Each i As String In detailList
            Console.WriteLine(i)
        Next

        Writer.Write(detailList)
        ' Writer.Write("</> " & txtaddress.Text & " <\>")

        MsgBox("datas send ")
        Writer.Flush()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Server Code

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Dim message As String
    Dim nStart As Integer
    Dim nLast As Integer

    If listener.Pending = True Then
        message = ""
        cline = listener.AcceptTcpClient()
        Dim reder As New StreamReader(cline.GetStream)
        While reder.Peek > -1
            message &= Convert.ToChar(reder.Read()).ToString
        End While

        If message.Contains("</>") Then
            nStart = InStr(message, "</>") + 10
            nLast = InStr(message, "<\>")
            message = Mid(message, nStart, nLast)
        End If

        Console.WriteLine(message)

        txtname.Text = message
        Label1.Text = message
        '   saveData()
    End If
End Sub

Public Sub saveData()

    Dim cmd As SqlCommand
    sc.Open()
    cmd = New SqlCommand("insert into demo values('" + txtname.Text + "')", sc)
    cmd.ExecuteNonQuery()
    msg = MsgBox("data save")

    sc.Close()

End Sub
1
  • Is "detailList" your ArrayList, or is there an ArrayList in your detailList? Commented Nov 20, 2014 at 12:28

1 Answer 1

1

On this line in Button1_Click:

Writer.Write(detailList)

You're using the overloaded version of Write that takes an Object. In order to write out something meaningful for Objects, .NET has internally called detailList's ToString() method. The ToString() method here is what's returning the string "System.Collection.ArrayList".

So, you aren't sending an actual ArrayList implementation to your server, but simply its string representation (the result of calling ToString() on it).

In order to send objects over TCP, you will need to serialize it first, and then deserialize it on the server side. You could use any number of binary, XML, JSON, etc. formatters to do it, or write your own.

Sign up to request clarification or add additional context in comments.

2 Comments

i am not having idea about json or xml so how to do it
I think a simple search for "tcpclient object serialization" would return more than enough hits to get you started, if that is truly what you think you need. If you're just looking to send a comma-separated list of strings or something and manually re-create the list on the other end of the pipe, then you already have the requirements for a very simple serialization/deserialization implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.