|
-
Jan 16th, 2005, 09:06 PM
#1
Thread Starter
Hyperactive Member
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.
-
Jan 16th, 2005, 10:01 PM
#2
Thread Starter
Hyperactive Member
Re: Network Stream?
This is what i have so far
VB Code:
Public Class SocketMaster
Public Socket As Socket = Nothing
Private Form As frmMain
Public Sub New(ByVal frm As Form)
Form = DirectCast(frm, frmMain)
End Sub
Public Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
Dim hostEntry As IPHostEntry = Nothing
hostEntry = Dns.Resolve(server)
Dim address As IPAddress
For Each address In hostEntry.AddressList
Dim endPoint As New IPEndPoint(address, port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
Try
tempSocket.Connect(endPoint)
Catch Err As SocketException
AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
End Try
If tempSocket.Connected Then
Socket = tempSocket
Exit For
End If
Next address
Return True
End Function
Public Function Disconnect() As Boolean
Try
Socket.Close()
Catch Err As SocketException
AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
End Try
Return True
End Function
End Class
-
Jan 17th, 2005, 04:01 AM
#3
Fanatic Member
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:
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect(strIPAddress, PortNo)
str_SendData = "data to be sent"
networkStream = tcpClient.GetStream
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(str_SendData)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Else
MessageBox.Show("The connection is not writeable", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim ReadBuffer(1024) As Byte
If networkStream.CanRead Then
strData = ""
Do
timeOut += 1
numberOfBytesRead = networkStream.Read(ReadBuffer, 0, ReadBuffer.Length)
strData = [String].Concat(strData, Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead))
Loop While (networkStream.DataAvailable) And (timeOut < 60)
Hope this helps.
-
Jan 17th, 2005, 08:14 AM
#4
Thread Starter
Hyperactive Member
Re: Network Stream?
I will look into it, thank you.
-
Jan 17th, 2005, 12:50 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|