Results 1 to 5 of 5

Thread: Network Stream?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Network Stream?

    I used the System.Net.Sockets

    To create a socket, and connect to a host

    I have no idea where to begin sending/recieving

    I want to use the Network Stream class, am just not sure on how to go about doing it.

    Any information, Sample Code Snippets/Projects would be GREATLY apreciated.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Re: Network Stream?

    This is what i have so far

    VB Code:
    1. Public Class SocketMaster
    2.  
    3.     Public Socket As Socket = Nothing
    4.     Private Form As frmMain
    5.  
    6.     Public Sub New(ByVal frm As Form)
    7.         Form = DirectCast(frm, frmMain)
    8.     End Sub
    9.  
    10.     Public Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
    11.         Dim hostEntry As IPHostEntry = Nothing
    12.  
    13.         hostEntry = Dns.Resolve(server)
    14.  
    15.         Dim address As IPAddress
    16.  
    17.         For Each address In hostEntry.AddressList
    18.             Dim endPoint As New IPEndPoint(address, port)
    19.             Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
    20.             Try
    21.                 tempSocket.Connect(endPoint)
    22.             Catch Err As SocketException
    23.                 AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
    24.             End Try
    25.             If tempSocket.Connected Then
    26.                 Socket = tempSocket
    27.                 Exit For
    28.             End If
    29.         Next address
    30.         Return True
    31.     End Function
    32.  
    33.     Public Function Disconnect() As Boolean
    34.         Try
    35.             Socket.Close()
    36.         Catch Err As SocketException
    37.             AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
    38.         End Try
    39.         Return True
    40.     End Function
    41.  
    42. End Class

  3. #3
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Network Stream?

    Originally posted by BaDDBLooD:
    I have no idea where to begin sending/recieving
    After you've successfully connected to the remote m/c then you can start sending data to the connected m/c.
    VB Code:
    1. Dim tcpClient As New System.Net.Sockets.TcpClient()
    2.                 tcpClient.Connect(strIPAddress, PortNo)
    3.                 str_SendData = "data to be sent"
    4.                 networkStream = tcpClient.GetStream
    5.                 If networkStream.CanWrite Then
    6.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(str_SendData)
    7.                     networkStream.Write(sendBytes, 0, sendBytes.Length)
    8.                 Else
    9.                     MessageBox.Show("The connection is not writeable", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    10.                     Exit Sub
    11.                 End If
    12.                 Dim ReadBuffer(1024) As Byte
    13.                 If networkStream.CanRead Then
    14.                     strData = ""
    15.                     Do
    16.                         timeOut += 1
    17.                         numberOfBytesRead = networkStream.Read(ReadBuffer, 0, ReadBuffer.Length)
    18.                         strData = [String].Concat(strData, Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead))
    19.                     Loop While (networkStream.DataAvailable) And (timeOut < 60)
    Hope this helps.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Re: Network Stream?

    I will look into it, thank you.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Network Stream?

    I was dealing with something like this for communicating with a PDA. The TCPClient and TCPListener classes are much easier to work with than straight sockets as you were doing. However, if you look at the documentation, you will see that these two are actually built on sockets, so you might consider inheriting from them to get direct access to the underlying socket. I tried to do that because there is no obvious way to determine whether or not a connection has been made in the TCPClient class, since the connection information is protected. Unfortunately, I was doing that on a PDA, and that particular protected value was not available in the CF.

    More than you want to know, but there it is.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width