473,692 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
10 4025
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

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.itw rote in message
news:11******** *************@m 79g2000cwm.goog legroups.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
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.itw rote in message
news:11******** *************@m 79g2000cwm.goog legroups.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

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.itw rote in message
news:11******** **************@ 75g2000cwc.goog legroups.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
Networkstrea m. 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.it wrote in message
news:11******* **************@ m79g2000cwm.goo glegroups.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
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 SizeOfTheReceiv eBuffer As Integer = 2048 'change
appropriately
Const Port As Integer = 60000 'change appropriately
Dim LocalIP As System.Net.IPAd dress =
IPAddress.Parse ("127.0.0.1" )
Dim TcpListener As New TcpListener(Loc alIP, Port)

Try
TcpListener.Sta rt()
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.Pen ding Then

Socket = TcpListener.Acc eptSocket 'wait for client
call
Socket.ReceiveB ufferSize = SizeOfTheReceiv eBuffer
Dim ReceiveBuffer(S izeOfTheReceive Buffer - 1) As Byte

'Receive object
Dim MemoryStream As New System.IO.Memor yStream
Dim BytesActuallyRe ceived As Integer
Do
BytesActuallyRe ceived =
Socket.Receive( ReceiveBuffer, SizeOfTheReceiv eBuffer, SocketFlags.Non e)
If BytesActuallyRe ceived 0 Then
MemoryStream.Wr ite(ReceiveBuff er, 0,
BytesActuallyRe ceived)
Else
Exit Do
End If
Loop

Try
Dim o As Object
With New BinaryFormatter
MemoryStream.Po sition = 0
o = .Deserialize(Me moryStream)
Me.PictureBoxRe ceived.Image = DirectCast(o,
Image)
End With

Catch ex As Exception
MsgBox(ex.Messa ge)
Finally
MemoryStream.Cl ose()
End Try

Else
Application.DoE vents()
End If
Loop

Socket.Shutdown (SocketShutdown .Both)
Socket.Close()
TcpListener.Sto p()


CLIENT SIDE CODE
--------------------------------------------------------------------------------
Dim SizeOfTheSendBu ffer 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(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p)

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

Socket.SendBuff erSize = SizeOfTheSendBu ffer

If Not Socket.Connecte d 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(Memo ryStream, MyObject)
ObjectLength = MemoryStream.Po sition
End With

Dim SendBuffer(Size OfTheSendBuffer - 1) As Byte
Dim BytesActuallyRe ad As Integer

'Send pieces of the object
MemoryStream.Po sition = 0
Do While MemoryStream.Po sition < ObjectLength
BytesActuallyRe ad = MemoryStream.Re ad(SendBuffer,
0, SizeOfTheSendBu ffer)
Socket.Send(Sen dBuffer, BytesActuallyRe ad,
SocketFlags.Non e)
Loop

MemoryStream.Cl ose()
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.itw rote in message
news:11******** **************@ 75g2000cwc.goog legroups.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.itw rote in message
news:11******** *************@m 79g2000cwm.goog legroups.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
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
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.Ge tBytes. 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.itw rote in message
news:11******** *************@m 79g2000cwm.goog legroups.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
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.Ge tBytes. 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.itw rote in message
news:11******** *************@m 79g2000cwm.goog legroups.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
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.itw rote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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
Serializatio n.

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
serializatio n, 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.G etBytes. 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.it wrote in message
news:11******* **************@ m79g2000cwm.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3916
by: Jerome Symons | last post by:
Hi everyone, I have developed a C# client/server system that creates bitmap images at the server and sends them to the client as an array of bytes using a Socket. When the data rate through the socket reaches about 4.5 MB/s, the Socket.Read command at the client end uses all of my CPU. I don't know why it takes so much CPU to read the data from the socket.
5
11700
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine if , say a chat message "Hi Charlie" has been received completely?
7
3935
by: Crirus | last post by:
Hi all! I use a webClient for requesting data from a server of mine. Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a terminator sign in the data in order to realise in the client that all data arrived? -- Ceers, Crirus
9
3598
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6873
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server" app. I am using the standard asynch socket code from MSDN to listen for connections and they...
0
4675
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
5
2632
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server) that almost does the job. It listens on a user-defined port and responds to incoming data by writing how many times somebody has tried to connect to the server.
6
6994
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the supernode. Everything I have written works fine sending and receiving uncompressed data. But now I want to implement compression using the deflate algorithm as the Gnutella protocol accepts: Accept-Encoding: deflate Content-Encoding: deflate in the...
1
9759
by: starter08 | last post by:
Hi, I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket. On the server side I have a cgi script which receives all the data and creates a file in the specified directory. If I am uploading only the file all works well, however I want to send data of other fields too (field1, field2 ..etc), this fails the post request and even the file is...
0
8608
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9086
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8963
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8803
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8806
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5820
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4324
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.