By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 21 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
Jul 21 '06 #2

P: n/a

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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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

Jul 21 '06 #3

P: n/a
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_20...ket-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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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
Jul 21 '06 #4

P: n/a

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

<to**************@uniroma1.itwrote in message
news:11**********************@75g2000cwc.googlegro ups.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_20...ket-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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegr oups.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

Jul 24 '06 #5

P: n/a
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:
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

<to**************@uniroma1.itwrote in message
news:11**********************@75g2000cwc.googlegro ups.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_20...ket-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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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
Jul 24 '06 #6

P: n/a
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/

Jul 24 '06 #7

P: n/a
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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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/

Jul 25 '06 #8

P: n/a
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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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/
Jul 25 '06 #9

P: n/a
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

<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegr oups.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/

Jul 27 '06 #10

P: n/a
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:
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

<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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/
Jul 27 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.