Home All Groups Group Topic Archive Search About

Can I map the data received from a socket directly to a structure in VB.NET

Author
21 Jul 2006 6:51 AM
Uma - Chellasoft
Hai,

I am new to VB.Net programming, directly doing socket programming. In C, I
will be able to map the message arrived in a socket directly to a structure.
Is this possible in VB.Net. Can anyone please help me with some sample
codings and guidance?

Can you also suggest some other news group available for socket programming
in VB.Net?

My TCP/IP server program is done in C. I have to create a TCP/IP client
socket in VB.Net and transfer some data between the server and client. The
data sent should be in predefined structure with a header and body.
Depending on the Message Type in the header, I have to map the arrived
message to the corresponing structure. I have done this in C, but not aware
of implementing in VB.Net. Please guide me.

Thanks in advance,
Uma

Author
21 Jul 2006 7:29 AM
tommaso.gastaldi
Yes Uma, actually you can directly serialize objects through the
socket. It's quite easy you can just use a Networkstream and a
Formatter to do that. The process is simply to serialize the object to
an array of bytes, send it, and deserialize it on the other end.

-tom

Uma - Chellasoft ha scritto:

Show quoteHide quote
> Hai,
>
> I am new to VB.Net programming, directly doing socket programming. In C, I
> will be able to map the message arrived in a socket directly to a structure.
> Is this possible in VB.Net. Can anyone please help me with some sample
> codings and guidance?
>
> Can you also suggest some other news group available for socket programming
> in VB.Net?
>
> My TCP/IP server program is done in C. I have to create a TCP/IP client
> socket in VB.Net and transfer some data between the server and client. The
> data sent should be in predefined structure with a header and body.
> Depending on the Message Type in the header, I have to map the arrived
> message to the corresponing structure. I have done this in C, but not aware
> of implementing in VB.Net. Please guide me.
>
> Thanks in advance,
> Uma
Author
21 Jul 2006 1:49 PM
Uma - Chellasoft
Thanks for your guidance for using Networkstream and Formatter. But I am
unable to come out with the exact solution even after having a look at
Networkstream. If time permits, can you please give me a sample code for
mapping the binary message from a socket directly to a structure in VB.Net.

Regards,
Uma

<tommaso.gasta***@uniroma1.it> wrote in message
Show quoteHide quote
news:1153466981.472863.17360@m79g2000cwm.googlegroups.com...
> Yes Uma, actually you can directly serialize objects through the
> socket. It's quite easy you can just use a Networkstream and a
> Formatter to do that. The process is simply to serialize the object to
> an array of bytes, send it, and deserialize it on the other end.
>
> -tom
>
> Uma - Chellasoft ha scritto:
>
>> Hai,
>>
>> I am new to VB.Net programming, directly doing socket programming. In C,
>> I
>> will be able to map the message arrived in a socket directly to a
>> structure.
>> Is this possible in VB.Net. Can anyone please help me with some sample
>> codings and guidance?
>>
>> Can you also suggest some other news group available for socket
>> programming
>> in VB.Net?
>>
>> My TCP/IP server program is done in C. I have to create a TCP/IP client
>> socket in VB.Net and transfer some data between the server and client.
>> The
>> data sent should be in predefined structure with a header and body.
>> Depending on the Message Type in the header, I have to map the arrived
>> message to the corresponing structure. I have done this in C, but not
>> aware
>> of implementing in VB.Net. Please guide me.
>>
>> Thanks in advance,
>> Uma
>
Author
21 Jul 2006 2:44 PM
tommaso.gastaldi
Hello,

UMA, at the moment I am a little overwhelmed (I am replying between
compilations) but I found a few links with code:

http://www.ureader.com/message/1084726.aspx
http://www.developmentnow.com/g/4_2005_6_0_0_551476/Simple-binary-serialization-for-Socket-stream.htm
http://www.thescripts.com/forum/thread381461.html

if you have problems let us know. If none works, I will try to spare
some time later to write your classes. Ciao,

-Tommaso

Uma - Chellasoft ha scritto:

Show quoteHide quote
> Thanks for your guidance for using Networkstream and Formatter. But I am
> unable to come out with the exact solution even after having a look at
> Networkstream. If time permits, can you please give me a sample code for
> mapping the binary message from a socket directly to a structure in VB.Net.
>
> Regards,
> Uma
>
> <tommaso.gasta***@uniroma1.it> wrote in message
> news:1153466981.472863.17360@m79g2000cwm.googlegroups.com...
> > Yes Uma, actually you can directly serialize objects through the
> > socket. It's quite easy you can just use a Networkstream and a
> > Formatter to do that. The process is simply to serialize the object to
> > an array of bytes, send it, and deserialize it on the other end.
> >
> > -tom
> >
> > Uma - Chellasoft ha scritto:
> >
> >> Hai,
> >>
> >> I am new to VB.Net programming, directly doing socket programming. In C,
> >> I
> >> will be able to map the message arrived in a socket directly to a
> >> structure.
> >> Is this possible in VB.Net. Can anyone please help me with some sample
> >> codings and guidance?
> >>
> >> Can you also suggest some other news group available for socket
> >> programming
> >> in VB.Net?
> >>
> >> My TCP/IP server program is done in C. I have to create a TCP/IP client
> >> socket in VB.Net and transfer some data between the server and client.
> >> The
> >> data sent should be in predefined structure with a header and body.
> >> Depending on the Message Type in the header, I have to map the arrived
> >> message to the corresponing structure. I have done this in C, but not
> >> aware
> >> of implementing in VB.Net. Please guide me.
> >>
> >> Thanks in advance,
> >> Uma
> >
Author
24 Jul 2006 10:44 AM
Uma - Chellasoft
Tom, thanks for the links, but I am sorry to say that all links contains
samples in C#. If possible, pls try to send me some links in VB.Net. But
these links are providing me a  a good idea, and I am trying to work it out
in VB.Net. As I have been working in C for a long time, it is taking some
time for me to shift to VB.Net.

Thanks,
Uma

<tommaso.gasta***@uniroma1.it> wrote in message
Show quoteHide quote
news:1153493077.935578.234830@75g2000cwc.googlegroups.com...
> Hello,
>
> UMA, at the moment I am a little overwhelmed (I am replying between
> compilations) but I found a few links with code:
>
> http://www.ureader.com/message/1084726.aspx
> http://www.developmentnow.com/g/4_2005_6_0_0_551476/Simple-binary-serialization-for-Socket-stream.htm
> http://www.thescripts.com/forum/thread381461.html
>
> if you have problems let us know. If none works, I will try to spare
> some time later to write your classes. Ciao,
>
> -Tommaso
>
> Uma - Chellasoft ha scritto:
>
>> Thanks for your guidance for using Networkstream and Formatter. But I am
>> unable to come out with the exact solution even after having a look at
>> Networkstream. If time permits, can you please give me a sample code for
>> mapping the binary message from a socket directly to a structure in
>> VB.Net.
>>
>> Regards,
>> Uma
>>
>> <tommaso.gasta***@uniroma1.it> wrote in message
>> news:1153466981.472863.17360@m79g2000cwm.googlegroups.com...
>> > Yes Uma, actually you can directly serialize objects through the
>> > socket. It's quite easy you can just use a Networkstream and a
>> > Formatter to do that. The process is simply to serialize the object to
>> > an array of bytes, send it, and deserialize it on the other end.
>> >
>> > -tom
>> >
>> > Uma - Chellasoft ha scritto:
>> >
>> >> Hai,
>> >>
>> >> I am new to VB.Net programming, directly doing socket programming. In
>> >> C,
>> >> I
>> >> will be able to map the message arrived in a socket directly to a
>> >> structure.
>> >> Is this possible in VB.Net. Can anyone please help me with some sample
>> >> codings and guidance?
>> >>
>> >> Can you also suggest some other news group available for socket
>> >> programming
>> >> in VB.Net?
>> >>
>> >> My TCP/IP server program is done in C. I have to create a TCP/IP
>> >> client
>> >> socket in VB.Net and transfer some data between the server and client.
>> >> The
>> >> data sent should be in predefined structure with a header and body.
>> >> Depending on the Message Type in the header, I have to map the arrived
>> >> message to the corresponing structure. I have done this in C, but not
>> >> aware
>> >> of implementing in VB.Net. Please guide me.
>> >>
>> >> Thanks in advance,
>> >> Uma
>> >
>
Author
24 Jul 2006 12:48 PM
tommaso.gastaldi
Ok here we go. Here is an example sending from an application to
another application
the image contained in a picture box

Sorry not much refined but  I am in the middle of a huge project. It
works anyway...
I have coded it so that yone can send object of any size (buffer can be
adjusted: could
use a few MB buffer) I used different small numbers just to try...

In case of problems post here. Others may contribute with some
important suggestion...
Sorry I am in a hurry...

-Tommaso


SERVER SIDE CODE
------------------------------------------------------------------------------


        Dim SizeOfTheReceiveBuffer As Integer = 2048    'change
appropriately
        Const Port As Integer = 60000            'change appropriately
        Dim LocalIP As System.Net.IPAddress =
IPAddress.Parse("127.0.0.1")
        Dim TcpListener As New TcpListener(LocalIP, Port)

        Try
            TcpListener.Start()
        Catch ex As Exception
            MsgBox("Port may be already in use by another app" & _
            vbCrLf & ex.Message)
            Exit Sub
        End Try

        Dim Socket As Sockets.Socket

        Do

            If TcpListener.Pending Then

                Socket = TcpListener.AcceptSocket  'wait for client
call
                Socket.ReceiveBufferSize = SizeOfTheReceiveBuffer
                Dim ReceiveBuffer(SizeOfTheReceiveBuffer - 1) As Byte

                'Receive object
                Dim MemoryStream As New System.IO.MemoryStream
                Dim BytesActuallyReceived As Integer
                Do
                    BytesActuallyReceived =
Socket.Receive(ReceiveBuffer, SizeOfTheReceiveBuffer, SocketFlags.None)
                    If BytesActuallyReceived > 0 Then
                        MemoryStream.Write(ReceiveBuffer, 0,
BytesActuallyReceived)
                    Else
                        Exit Do
                    End If
                Loop

                Try
                    Dim o As Object
                    With New BinaryFormatter
                        MemoryStream.Position = 0
                        o = .Deserialize(MemoryStream)
                        Me.PictureBoxReceived.Image = DirectCast(o,
Image)
                    End With

                Catch ex As Exception
                    MsgBox(ex.Message)
                Finally
                    MemoryStream.Close()
                End Try

            Else
                Application.DoEvents()
            End If


        Loop

        Socket.Shutdown(SocketShutdown.Both)
        Socket.Close()
        TcpListener.Stop()




CLIENT SIDE CODE
--------------------------------------------------------------------------------


        Dim SizeOfTheSendBuffer As Integer = 1024    'change appropriately

        Const Port As Integer = 60000            'change appropriately
        Dim ServerAddress As String = "127.0.0.1"       'change
appropriately if not local

        Dim MyObject As Object = Me.PictureBox1.Image
        Dim ObjectLength As Long

        'Connect to the server
        Dim Socket As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)

        Try
            Socket.Connect(ServerAddress, Port)
        Catch ex As Exception
            MsgBox("(Server may be not responding)" & vbCrLf & _
            ex.Message)
            Exit Sub
        End Try

        Socket.SendBufferSize = SizeOfTheSendBuffer

        If Not Socket.Connected Then
            MsgBox("Unable to connect to host " & ServerAddress)
        Else

            Using MemoryStream As New MemoryStream

                'Serialize object
                Dim BinaryFormatter As New BinaryFormatter
                With New BinaryFormatter
                    .Serialize(MemoryStream, MyObject)
                    ObjectLength = MemoryStream.Position
                End With

                Dim SendBuffer(SizeOfTheSendBuffer - 1) As Byte
                Dim BytesActuallyRead As Integer

                'Send pieces of the object
                MemoryStream.Position = 0
                Do While MemoryStream.Position < ObjectLength
                    BytesActuallyRead = MemoryStream.Read(SendBuffer,
0, SizeOfTheSendBuffer)
                    Socket.Send(SendBuffer, BytesActuallyRead,
SocketFlags.None)
                Loop

                MemoryStream.Close()
                Socket.Close()

            End Using

        End If








Uma - Chellasoft ha scritto:

Show quoteHide quote
> Tom, thanks for the links, but I am sorry to say that all links contains
> samples in C#. If possible, pls try to send me some links in VB.Net. But
> these links are providing me a  a good idea, and I am trying to work it out
> in VB.Net. As I have been working in C for a long time, it is taking some
> time for me to shift to VB.Net.
>
> Thanks,
> Uma
>
> <tommaso.gasta***@uniroma1.it> wrote in message
> news:1153493077.935578.234830@75g2000cwc.googlegroups.com...
> > Hello,
> >
> > UMA, at the moment I am a little overwhelmed (I am replying between
> > compilations) but I found a few links with code:
> >
> > http://www.ureader.com/message/1084726.aspx
> > http://www.developmentnow.com/g/4_2005_6_0_0_551476/Simple-binary-serialization-for-Socket-stream.htm
> > http://www.thescripts.com/forum/thread381461.html
> >
> > if you have problems let us know. If none works, I will try to spare
> > some time later to write your classes. Ciao,
> >
> > -Tommaso
> >
> > Uma - Chellasoft ha scritto:
> >
> >> Thanks for your guidance for using Networkstream and Formatter. But I am
> >> unable to come out with the exact solution even after having a look at
> >> Networkstream. If time permits, can you please give me a sample code for
> >> mapping the binary message from a socket directly to a structure in
> >> VB.Net.
> >>
> >> Regards,
> >> Uma
> >>
> >> <tommaso.gasta***@uniroma1.it> wrote in message
> >> news:1153466981.472863.17360@m79g2000cwm.googlegroups.com...
> >> > Yes Uma, actually you can directly serialize objects through the
> >> > socket. It's quite easy you can just use a Networkstream and a
> >> > Formatter to do that. The process is simply to serialize the object to
> >> > an array of bytes, send it, and deserialize it on the other end.
> >> >
> >> > -tom
> >> >
> >> > Uma - Chellasoft ha scritto:
> >> >
> >> >> Hai,
> >> >>
> >> >> I am new to VB.Net programming, directly doing socket programming. In
> >> >> C,
> >> >> I
> >> >> will be able to map the message arrived in a socket directly to a
> >> >> structure.
> >> >> Is this possible in VB.Net. Can anyone please help me with some sample
> >> >> codings and guidance?
> >> >>
> >> >> Can you also suggest some other news group available for socket
> >> >> programming
> >> >> in VB.Net?
> >> >>
> >> >> My TCP/IP server program is done in C. I have to create a TCP/IP
> >> >> client
> >> >> socket in VB.Net and transfer some data between the server and client.
> >> >> The
> >> >> data sent should be in predefined structure with a header and body.
> >> >> Depending on the Message Type in the header, I have to map the arrived
> >> >> message to the corresponing structure. I have done this in C, but not
> >> >> aware
> >> >> of implementing in VB.Net. Please guide me.
> >> >>
> >> >> Thanks in advance,
> >> >> Uma
> >> >
> >
Author
24 Jul 2006 12:55 PM
tommaso.gastaldi
Forgot to say that, clearly, in place of the image you can clearly use
any object.
The code is always the same for any object.

Also you can change the type of serialization if appropriate (eg.,
XML).

-tom

DataTime(tm) - Community Client Edition (free)
http://cam70.sta.uniroma1.it/Community/
Author
25 Jul 2006 7:44 AM
Uma - Chellasoft
Thanks Tom, your sample codings was very helpful. I am able to serialize the
object and send it through the socket. In middle of your busy work, u have
sent me some sample codings which was very clear for me to understand about
Serialization.

As mentioned earlier, my client socket is in VB.Net and the server socket in
VC++ console application. While serializing and sending an object from
VB.Net, I am actually sending the entire details of the structure, and not
the required data alone. Say I am having a structure with two short data
types, now it is enough for me to transfer 4 bytes of data alone. But by
serialization, I am transferring some 159 bytes of data. Is it possible for
me to send the 4 bytes alone?

I am able to sending individual elements of the structure by using
BitConvertor.GetBytes. But I don't want this as the application contains
several structures with several members in all structures. How can I send
the data aloen?

Sorry for disturbing you, please give me some ideas and links for my case.

Thanks,
Uma

<tommaso.gasta***@uniroma1.it> wrote in message
Show quoteHide quote
news:1153745721.151868.58420@m79g2000cwm.googlegroups.com...
> Forgot to say that, clearly, in place of the image you can clearly use
> any object.
> The code is always the same for any object.
>
> Also you can change the type of serialization if appropriate (eg.,
> XML).
>
> -tom
>
> DataTime(tm) - Community Client Edition (free)
> http://cam70.sta.uniroma1.it/Community/
>
Author
25 Jul 2006 8:20 AM
tommaso.gastaldi
hi Uma
What you are asking I think is exactly the same question in the first
link I provided, if I remember well.

Of course is possible. If you do not have control on the receive
process and you just need to send only a few bytes you just put them in
the Stream and send them away. That's it. The process is the same. You
simply skip the serialization part.

-t

Uma - Chellasoft ha scritto:

Show quoteHide quote
> Thanks Tom, your sample codings was very helpful. I am able to serialize the
> object and send it through the socket. In middle of your busy work, u have
> sent me some sample codings which was very clear for me to understand about
> Serialization.
>
> As mentioned earlier, my client socket is in VB.Net and the server socket in
> VC++ console application. While serializing and sending an object from
> VB.Net, I am actually sending the entire details of the structure, and not
> the required data alone. Say I am having a structure with two short data
> types, now it is enough for me to transfer 4 bytes of data alone. But by
> serialization, I am transferring some 159 bytes of data. Is it possible for
> me to send the 4 bytes alone?
>
> I am able to sending individual elements of the structure by using
> BitConvertor.GetBytes. But I don't want this as the application contains
> several structures with several members in all structures. How can I send
> the data aloen?
>
> Sorry for disturbing you, please give me some ideas and links for my case.
>
> Thanks,
> Uma
>
> <tommaso.gasta***@uniroma1.it> wrote in message
> news:1153745721.151868.58420@m79g2000cwm.googlegroups.com...
> > Forgot to say that, clearly, in place of the image you can clearly use
> > any object.
> > The code is always the same for any object.
> >
> > Also you can change the type of serialization if appropriate (eg.,
> > XML).
> >
> > -tom
> >
> > DataTime(tm) - Community Client Edition (free)
> > http://cam70.sta.uniroma1.it/Community/
> >
Author
27 Jul 2006 7:36 AM
Uma - Chellasoft
Thanks Tom, finally I am able to transfer binary data from VB.Net to C and C
to VB.Net and map it to the required structure.

The first link had my exact requirement. Actually I saw it earlier, and
didn't concentrate on it as it was in C#, and I thought that memory
managements were not possible in VB.Net, but now I have done the same way in
VB.Net and it is working fine.

Regards,
Uma

<tommaso.gasta***@uniroma1.it> wrote in message
Show quoteHide quote
news:1153815642.768154.122470@m73g2000cwd.googlegroups.com...
> hi Uma
> What you are asking I think is exactly the same question in the first
> link I provided, if I remember well.
>
> Of course is possible. If you do not have control on the receive
> process and you just need to send only a few bytes you just put them in
> the Stream and send them away. That's it. The process is the same. You
> simply skip the serialization part.
>
> -t
>
> Uma - Chellasoft ha scritto:
>
>> Thanks Tom, your sample codings was very helpful. I am able to serialize
>> the
>> object and send it through the socket. In middle of your busy work, u
>> have
>> sent me some sample codings which was very clear for me to understand
>> about
>> Serialization.
>>
>> As mentioned earlier, my client socket is in VB.Net and the server socket
>> in
>> VC++ console application. While serializing and sending an object from
>> VB.Net, I am actually sending the entire details of the structure, and
>> not
>> the required data alone. Say I am having a structure with two short data
>> types, now it is enough for me to transfer 4 bytes of data alone. But by
>> serialization, I am transferring some 159 bytes of data. Is it possible
>> for
>> me to send the 4 bytes alone?
>>
>> I am able to sending individual elements of the structure by using
>> BitConvertor.GetBytes. But I don't want this as the application contains
>> several structures with several members in all structures. How can I send
>> the data aloen?
>>
>> Sorry for disturbing you, please give me some ideas and links for my
>> case.
>>
>> Thanks,
>> Uma
>>
>> <tommaso.gasta***@uniroma1.it> wrote in message
>> news:1153745721.151868.58420@m79g2000cwm.googlegroups.com...
>> > Forgot to say that, clearly, in place of the image you can clearly use
>> > any object.
>> > The code is always the same for any object.
>> >
>> > Also you can change the type of serialization if appropriate (eg.,
>> > XML).
>> >
>> > -tom
>> >
>> > DataTime(tm) - Community Client Edition (free)
>> > http://cam70.sta.uniroma1.it/Community/
>> >
>
Author
27 Jul 2006 8:33 AM
tommaso.gastaldi
Good. In general in the .net you can always look at c#. The classes are
just
the same: you only need to change the order of some keywords, that's it
.... :)
-t

Uma - Chellasoft ha scritto:

Show quoteHide quote
> Thanks Tom, finally I am able to transfer binary data from VB.Net to C and C
> to VB.Net and map it to the required structure.
>
> The first link had my exact requirement. Actually I saw it earlier, and
> didn't concentrate on it as it was in C#, and I thought that memory
> managements were not possible in VB.Net, but now I have done the same way in
> VB.Net and it is working fine.
>
> Regards,
> Uma
>
> <tommaso.gasta***@uniroma1.it> wrote in message
> news:1153815642.768154.122470@m73g2000cwd.googlegroups.com...
> > hi Uma
> > What you are asking I think is exactly the same question in the first
> > link I provided, if I remember well.
> >
> > Of course is possible. If you do not have control on the receive
> > process and you just need to send only a few bytes you just put them in
> > the Stream and send them away. That's it. The process is the same. You
> > simply skip the serialization part.
> >
> > -t
> >
> > Uma - Chellasoft ha scritto:
> >
> >> Thanks Tom, your sample codings was very helpful. I am able to serialize
> >> the
> >> object and send it through the socket. In middle of your busy work, u
> >> have
> >> sent me some sample codings which was very clear for me to understand
> >> about
> >> Serialization.
> >>
> >> As mentioned earlier, my client socket is in VB.Net and the server socket
> >> in
> >> VC++ console application. While serializing and sending an object from
> >> VB.Net, I am actually sending the entire details of the structure, and
> >> not
> >> the required data alone. Say I am having a structure with two short data
> >> types, now it is enough for me to transfer 4 bytes of data alone. But by
> >> serialization, I am transferring some 159 bytes of data. Is it possible
> >> for
> >> me to send the 4 bytes alone?
> >>
> >> I am able to sending individual elements of the structure by using
> >> BitConvertor.GetBytes. But I don't want this as the application contains
> >> several structures with several members in all structures. How can I send
> >> the data aloen?
> >>
> >> Sorry for disturbing you, please give me some ideas and links for my
> >> case.
> >>
> >> Thanks,
> >> Uma
> >>
> >> <tommaso.gasta***@uniroma1.it> wrote in message
> >> news:1153745721.151868.58420@m79g2000cwm.googlegroups.com...
> >> > Forgot to say that, clearly, in place of the image you can clearly use
> >> > any object.
> >> > The code is always the same for any object.
> >> >
> >> > Also you can change the type of serialization if appropriate (eg.,
> >> > XML).
> >> >
> >> > -tom
> >> >
> >> > DataTime(tm) - Community Client Edition (free)
> >> > http://cam70.sta.uniroma1.it/Community/
> >> >
> >