Socket & String 
Author Message
 Socket & String

Hi

Could any one please let me know the equivalent for this in VB.NET:
Dim strAvailDevices As String * 500

Also I need to send and recive Structures through Sockets.
Could any one share with me some sample code to do this.

Thanks in Advance
Selva



Sat, 05 Nov 2005 03:08:20 GMT  
 Socket & String


Quote:
> Hi

> Could any one please let me know the equivalent for this in VB.NET:
> Dim strAvailDevices As String * 500

There are no fixed length strings in .NET.  What specifically are you trying
to do?  If it is for use with VB.NET File IO (which I would avoid and use
the System.IO classes), you can use the VBFixedString attribute.  If it is
for interop, you can use the MarshalAs(UnmanagedType.LPArray,
SizeConst:=...), or just initialize the buffer to the correct size before
the call - like in VB6.  If you give an indication of exactly what your
trying to accomplish, then you will be able to get a much more specific
answer.

Quote:
> Also I need to send and recive Structures through Sockets.
> Could any one share with me some sample code to do this.

A simple way would to be to mark the structure as serializable, and then
serialize it to a MemoryStream.  Then take the resulting byte array and send
it down the wire, where it can be deseralized on the other end.

Tom Shelton



Sat, 05 Nov 2005 03:24:07 GMT  
 Socket & String
Hi Tom,
Thanks for your reply.

Regarding the String i need to initialize the string for 200 characters send
it to C Dll which fills in the value.

Regarding Socket:
The VB.NET Socket Client sends message to the C Socket Server which expects
the Structure and send the structure back to me or not.

<Could you please provide some sample to serialize into memory stream also
how to read the Byte Array to the structure>

Thanks
Selva


Quote:
> Hi

> Could any one please let me know the equivalent for this in VB.NET:
> Dim strAvailDevices As String * 500

> Also I need to send and recive Structures through Sockets.
> Could any one share with me some sample code to do this.

> Thanks in Advance
> Selva



Sat, 05 Nov 2005 04:02:09 GMT  
 Socket & String
Is there any place where i can download the whole picture of System Classes
like the One MFC has,

Selva

Quote:
> Hi

> Could any one please let me know the equivalent for this in VB.NET:
> Dim strAvailDevices As String * 500

> Also I need to send and recive Structures through Sockets.
> Could any one share with me some sample code to do this.

> Thanks in Advance
> Selva



Sat, 05 Nov 2005 04:09:43 GMT  
 Socket & String


Quote:
> Hi Tom,
> Thanks for your reply.

> Regarding the String i need to initialize the string for 200 characters
send
> it to C Dll which fills in the value.

Ah,  well then... If the buffer is going to be filled in by the API call
then... I woud do something like this...

Private Declare Ansi Function TheFunction Lib "mydll.dll" (ByVal buffer As
System.Text.StringBuilder) As Integer

Dim buffer As New System.Text.StringBuilder(200)
TheFunction(buffer)

I could of course be more specific if you gave the actual C function
declaration.

Quote:
> Regarding Socket:
> The VB.NET Socket Client sends message to the C Socket Server which
expects
> the Structure and send the structure back to me or not.

> <Could you please provide some sample to serialize into memory stream also
> how to read the Byte Array to the structure>

This method may not work since you are going to a C server...  I can whip up
an example using the Marshal class methods, but I need to see the structure
definition (preferably the C version :)

Tom Shelton



Sat, 05 Nov 2005 04:16:15 GMT  
 Socket & String
Add System.Runtime.InteropServices

And then place one of these above the form load:

<VBFixedString(size = 200)>Dim strAvailDevices as String
or
<MarshalAs(UnmanagedType.AnsiBStr, sizeconst:=200)> Public strAvailDevices
As String

or

To use this add:

Imports System.Text

Dim strAvailDevices as StringBuilder = new StringBuilder(200)

hth

Quote:
> Hi

> Could any one please let me know the equivalent for this in VB.NET:
> Dim strAvailDevices As String * 500

> Also I need to send and recive Structures through Sockets.
> Could any one share with me some sample code to do this.

> Thanks in Advance
> Selva



Sat, 05 Nov 2005 04:22:10 GMT  
 Socket & String
Tom,
Here is the sample of the structure which is defined in the Socket Server
side (C)

/// specific message example

typedef struct {

    unsigned int Length;

    unsigned int MsgId;

    unsigned char FileName[100];

Quote:
} SPECIFIC_SOCKET_MSG;

Thanks

Selva



Sat, 05 Nov 2005 04:25:30 GMT  
 Socket & String


Quote:
> Tom,
> Here is the sample of the structure which is defined in the Socket Server
> side (C)

> /// specific message example

> typedef struct {

>     unsigned int Length;

>     unsigned int MsgId;

>     unsigned char FileName[100];

> } SPECIFIC_SOCKET_MSG;

> Thanks

> Selva

Well Selva,

Here is a short example...

Option Explicit On
Option Strict On

Imports System.Runtime.InteropServices

Module Module1

    ' typedef struct {
    '   unsigned int Length;
    '   unsigned int MsgId;
    '   unsigned char FileName[100];
    ' } SPECIFIC_SOCKET_MSG;
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Private Structure SPECIFIC_SOCKET_MSG
        Public Length As Integer
        Public MsgId As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=100)> _
        Public FileName As String
    End Structure

    Sub Main()
        Dim msg As SPECIFIC_SOCKET_MSG

        msg.FileName = "my file"
        msg.Length = msg.FileName.Length
        msg.MsgId = 1

        ' allocate the buffer
        Dim pBuffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(msg))

        ' copy the structure into the buffer
        Marshal.StructureToPtr(msg, pBuffer, True)

        ' allocate the byte array
        Dim byteBuffer(Marshal.SizeOf(msg) - 1) As Byte

        ' copy the value of the memory buffer to the byte array
        Marshal.Copy(pBuffer, byteBuffer, 0, byteBuffer.Length)

        ' free the memory
        Marshal.FreeHGlobal(pBuffer)

        ' Send your data!....

        ' *****************************************************
        ' now lets see if we get back what we started with :)
        ' *****************************************************

        ' Receive your data...

        ' create the temporary memory buffer
        pBuffer = Marshal.AllocHGlobal(byteBuffer.Length)

        ' copy the bytes to the buffer
        Marshal.Copy(byteBuffer, 0, pBuffer, byteBuffer.Length)

        ' copy the structure to the new variable
        Dim newMsg As SPECIFIC_SOCKET_MSG =
CType(Marshal.PtrToStructure(pBuffer, GetType(SPECIFIC_SOCKET_MSG)),
SPECIFIC_SOCKET_MSG)

        ' free the memory
        Marshal.FreeHGlobal(pBuffer)

        ' print out the values...
        Console.WriteLine("Message Id: {0}", newMsg.MsgId)
        Console.WriteLine("Message Length: {0}", newMsg.Length)
        Console.WriteLine("File Name: {0}", newMsg.FileName)
    End Sub

End Module

HTH,
Tom Shelton



Sat, 05 Nov 2005 05:15:43 GMT  
 Socket & String
Thanks a Lot Tom. One more small help,
what needs to done on this to send byte array to the socket server

Public Sub SendData(ByVal data As String)

Dim writer As New IO.StreamWriter(clientSocket.GetStream)

writer.Write(data)

writer.Flush()

End Sub

Thanks
Selva



Sun, 06 Nov 2005 01:56:58 GMT  
 Socket & String


Quote:
> Thanks a Lot Tom. One more small help,
> what needs to done on this to send byte array to the socket server

> Public Sub SendData(ByVal data As String)

> Dim writer As New IO.StreamWriter(clientSocket.GetStream)

> writer.Write(data)

> writer.Flush()

> End Sub

Public Sub SendData(ByVal data() As  Byte)
    Dim writer As NetworkStream = clientSocket.GetStream()
    writer.Write(data, 0, data.Length)
End Sub

You could also make it a one liner....

Public Sub SendData(ByVal data() As Byte)
    clientSocket.GetStream().Write(data, 0, data.Length)
End Sub

HTH,
Tom Shelton



Sun, 06 Nov 2005 02:06:53 GMT  
 Socket & String
Tom,

Please find my function declaration as follows:

Declare Function GetAvailUSBDevices Lib "MCTDLL32.DLL" Alias

When I tried to use any one of the declaration

'Dim strAvailDevices As StringBuilder = New StringBuilder(200)

Dim strAvailDevices As New StringBuilder(500)

I get a error "value of type system.text.stringbuilder can not converted as
string"

-----------------

when I try this

'<VBFixedString(500)> Private strAvailDevices As String

'<MarshalAs(UnmanagedType.AnsiBStr, sizeconst:=200)> Public strAvailDevices
As String

I am getting NullException at Runtime.

Could you please help me resolve this!

Thanks
Selva


Quote:



> > Hi Tom,
> > Thanks for your reply.

> > Regarding the String i need to initialize the string for 200 characters
> send
> > it to C Dll which fills in the value.

> Ah,  well then... If the buffer is going to be filled in by the API call
> then... I woud do something like this...

> Private Declare Ansi Function TheFunction Lib "mydll.dll" (ByVal buffer As
> System.Text.StringBuilder) As Integer

> Dim buffer As New System.Text.StringBuilder(200)
> TheFunction(buffer)

> I could of course be more specific if you gave the actual C function
> declaration.

> > Regarding Socket:
> > The VB.NET Socket Client sends message to the C Socket Server which
> expects
> > the Structure and send the structure back to me or not.

> > <Could you please provide some sample to serialize into memory stream
also
> > how to read the Byte Array to the structure>

> This method may not work since you are going to a C server...  I can whip
up
> an example using the Marshal class methods, but I need to see the
structure
> definition (preferably the C version :)

> Tom Shelton



Sun, 13 Nov 2005 01:16:46 GMT  
 Socket & String
Change this:

Declare Function GetAvailUSBDevices Lib "MCTDLL32.DLL" Alias

To This:

Declare Function GetAvailUSBDevices Lib "MCTDLL32.DLL" Alias


Quote:
> Tom,

> Please find my function declaration as follows:

> Declare Function GetAvailUSBDevices Lib "MCTDLL32.DLL" Alias

> When I tried to use any one of the declaration

> 'Dim strAvailDevices As StringBuilder = New StringBuilder(200)

> Dim strAvailDevices As New StringBuilder(500)

> I get a error "value of type system.text.stringbuilder can not converted
as
> string"

> -----------------

> when I try this

> '<VBFixedString(500)> Private strAvailDevices As String

> '<MarshalAs(UnmanagedType.AnsiBStr, sizeconst:=200)> Public
strAvailDevices
> As String

> I am getting NullException at Runtime.

> Could you please help me resolve this!

> Thanks
> Selva




message

> > > Hi Tom,
> > > Thanks for your reply.

> > > Regarding the String i need to initialize the string for 200
characters
> > send
> > > it to C Dll which fills in the value.

> > Ah,  well then... If the buffer is going to be filled in by the API call
> > then... I woud do something like this...

> > Private Declare Ansi Function TheFunction Lib "mydll.dll" (ByVal buffer
As
> > System.Text.StringBuilder) As Integer

> > Dim buffer As New System.Text.StringBuilder(200)
> > TheFunction(buffer)

> > I could of course be more specific if you gave the actual C function
> > declaration.

> > > Regarding Socket:
> > > The VB.NET Socket Client sends message to the C Socket Server which
> > expects
> > > the Structure and send the structure back to me or not.

> > > <Could you please provide some sample to serialize into memory stream
> also
> > > how to read the Byte Array to the structure>

> > This method may not work since you are going to a C server...  I can
whip
> up
> > an example using the Marshal class methods, but I need to see the
> structure
> > definition (preferably the C version :)

> > Tom Shelton



Sun, 13 Nov 2005 02:42:07 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Sockets, Strings, and other things...

2. Named Pipes & TCP/IP sockets

3. tcp/ip & sockets

4. VB & windows sockets

5. Trouble with System.Net.Sockets.Socket under Windows 98

6. System.Net.Sockets.Socket Problem

7. System.Net.Sockets.Socket State

8. system.net.sockets.socket async server example

9. Detect Remote Disconnection System.Net.Sockets.Socket

10. System.Net.Sockets.Socket.EndReceive has a bogus return value

11. "&&" string curiosity

12. *&*&*&* Date math question *&*&*&*

 

 
Powered by phpBB® Forum Software