Results 1 to 10 of 10

Thread: Data Across Network

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Data Across Network

    How do you send data of any type (integer,string,etc) from one computer to another?

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

    Re: Data Across Network

    There are many different options, depending on what you need to do. Others might disagree, but I think the basic solution is TCP/IP. The most common example of this is probably client server solutions currently. In this model, there is a server that is listening to a port. A client requests a connection, and the server accepts the connection. The client then sends or receives using TCP, which is a connection based, reliable protocol (which means that the message will get through, and if it has to be broken into pieces, this will be done transparently, and all pieces will be sewn back together before the user sees it).

    However, within TCP/IP, there is also UDP, which is a connectionless, unreliable, protocol. There is no connection, and any or all packets might get lost without any notification (there isn't even any tracking of packets, so there can't be notification). The value of UDP is that there isn't a connection, and it's fast. You can send small chunks of data to all listeners at once, and it's faster and easier than TCP. This is often used for small messaging systems, like peer-to-peer systems and games.

    However, in addition to these options, there are all kinds of other options, including things as simple as writing data to a file that is accessible to both systems. There are even times when such a blind drop would actually be advantageous (effectively, database systems are all variations on a blind drop like this, as data is written to the database from multiple sources, and can be simultaneoulsly read by many sources).
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: Data Across Network

    I have two applications, a client and a server. The server is a program with a single textbox that listens for a message.

    The client has a textbox and button. When the button is pushed the text in the textbox is sent over the network and appears in the server textbox.

    How would you do this?

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

    Re: Data Across Network

    How big will the text be? This sounds more like UDP than TCP.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: Data Across Network

    The text is a string variable.

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

    Re: Data Across Network

    Yes, but how big is it? If you can be certain that the size is fairly small (the exact number of characters/packet is network specific), you can use UDP easily. However, if the size gets large (100 characters is small, 500 might be small, 1000 isn't small), then the string will get divided into different chunks. While you can still deal with this under UDP, it becomes much more complex, as you have to deal with the possibility that packet A arrives after packet B, or that one or both fail to arrive at all. Dealing with that is not all that hard, but it would take sufficiently more code (each packet would have to have some indicator as to which packet number it is, and you'd want to send a receipt) that a TCP solution would be easier. If that is the case, look into the TCPListener and TCPClient classes. These are pretty easy to use as long as the communication is always initiated by one side. With TCP, you don't have to worry about the order of the packets, or lost packets, as the TCP protocol will handle both situations without direct action by you.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: Data Across Network

    Were talking maybe one to five characters.

  8. #8
    Junior Member Dario's Avatar
    Join Date
    Oct 2006
    Posts
    25

    Re: Data Across Network

    I'll give you code 4 that... i'm working on a simple sender/receiver... but i got a problem.
    It seems to send small strings accross fine ... but not the content of my textbox.

    Transmitter
    VB Code:
    1. Dim TcpClient As New TcpClient
    2.  
    3.         If Not TcpClient.Connected Then
    4.             TcpClient.Connect(9000, "127.0.0.1")
    5.         End If
    6.         Dim networkStream As NetworkStream = TcpClient.GetStream()
    7.  
    8.         '        If networkStream.CanWrite Then
    9.         Dim sendbytes(10240) As Byte
    10.         sendbytes = Encoding.ASCII.GetBytes("TEST DATA TEST DATA")
    11.         networkStream.Write(sendBytes, 0, sendBytes.Length)
    12.         'End If
    13.         TcpClient.Close()

    Listener... just spits the output to console....
    VB Code:
    1. Dim server As TcpListener
    2.         server = Nothing
    3.         'On Error Resume Next
    4.         Try
    5.             ' Set the TcpListener on port 9000.
    6.             Dim port As Int32 = 9000
    7.             Dim localAddr As System.Net.IPAddress = System.Net.IPAddress.Parse("127.0.0.1")
    8.  
    9.             server = New TcpListener(localAddr, port)
    10.  
    11.             ' Start listening for client requests.
    12.             server.Start()
    13.  
    14.             ' Buffer for reading data
    15.             Dim bytes(20480) As Byte
    16.             Dim data As String = Nothing
    17.  
    18.             ' Enter the listening loop.
    19.             While True
    20.                 Console.Write("Waiting for a connection... ")
    21.  
    22.                 ' Perform a blocking call to accept requests.
    23.                 ' You could also user server.AcceptSocket() here.
    24.                 Dim client As TcpClient = server.AcceptTcpClient()
    25.                 Console.WriteLine("Connected!")
    26.  
    27.                 data = Nothing
    28.  
    29.                 ' Get a stream object for reading and writing
    30.                 Dim stream As NetworkStream = client.GetStream()
    31.  
    32.                 Dim i As Int32
    33.  
    34.                 ' Loop to receive all the data sent by the client.
    35.                 i = stream.Read(bytes, 0, bytes.Length)
    36.                 While (i <> 0)
    37.                     ' Translate data bytes to a ASCII string.
    38.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    39.                     Console.WriteLine("Received: {0}", data)
    40.  
    41.                     ' Process the data sent by the client.
    42.                     data = data.ToUpper()
    43.                     'Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
    44.  
    45.                     ' Send back a response.
    46.                     'stream.Write(msg, 0, msg.Length)
    47.                     'Console.WriteLine("Sent: {0}", data)
    48.  
    49.                     i = stream.Read(bytes, 0, bytes.Length)
    50.  
    51.                 End While
    52.  
    53.                 ' Shutdown and end connection
    54.                 client.Close()
    55.             End While
    56.         Catch e As SocketException
    57.             Console.WriteLine("SocketException: {0}", e)
    58.         Finally
    59.             server.Stop()
    60.  
    61.         End Try
    62.  
    63.         Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
    64.         Console.Read()

    What could be the problem of sending conents of a textbox > 512 bytes < 1024 bytes??????

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

    Re: Data Across Network

    I had a problem similar to that, and it had to do with timing. I never actually liked my solution, so I don't think it's worth posting.

    Since you are only sending a few characters, you should also look into UDP, it is a faster, connectionless solution, which is usually easier to implement, though I haven't actually done so in .NET, only in VB6. Basically, the sender sends to a port, the listener reads from the port, and you skip all the stuff about connecting and accepting connections and the like.

    The sender sends out a bunch of bytes into the ether, not caring whether anybody is listening or not. The listener is sitting there listening to a port. Suddenly a bunch of bytes arrive and the listener deals with them, never caring where they came from. Naturally, there can be some issues there, as junk could come in, and the message from the sender could get lost in the ether. But those are things you can get around without great difficulty.

    Or you can just use Dario's code. It may be overkill for such a small amount of data, but it is more common.
    My usual boring signature: Nothing

  10. #10
    Member
    Join Date
    May 2006
    Posts
    60

    Re: Data Across Network

    Okay... I've been working on doing something like this for 2 days now.

    Using Dario's code, how would you do this if
    1) The client could send a FILE of any type, .jpg, .mpg, or .txt etc... ??
    2) The server didn't know what type it would get, but would save it to a folder on the server's hard drive with the original file name's name (or in the very least, the same type of extension .jpg etc...)

    I'm trying this but it's not working

    Client:
    VB Code:
    1. ' Sending an Image
    2.         Dim picture As Image
    3.         picture = Image.FromFile("c:\test.jpg")
    4.         Dim f As New FileInfo("c:\test.jpg")
    5.         Dim buffer(f.Length) As Byte
    6.  
    7.         Dim client As New TcpClient
    8.         client.Connect(IPAddress.Parse("127.0.0.1"), 8000)            
    9.         Dim stream As NetworkStream = client.GetStream()
    10.         stream.Write(picture, 0, picture.Length)
    11.  
    12.         stream.Close()
    13.         client.Close()
    14.        
    15.         Catch e As ArgumentNullException
    16.         ' ...
    17.         Catch e As SocketException
    18.         ' ...
    19.         End Try


    Server...
    VB Code:
    1. Dim server As TcpListener = Nothing
    2.         Try
    3.  
    4.            server = New TcpListener(8000)
    5.            server.Start()
    6.            Label1.Text = "Listening..."
    7.  
    8.            Dim client As TcpClient = server.AcceptTcpClient()
    9.            Label1.Text = "Connected"
    10.            Dim stream As NetworkStream = client.GetStream()
    11.  
    12.            ' How do I write the data to a file?????
    13.  
    14.            Dim buffer(CInt(client.ReceiveBufferSize)) As Byte ' Size of File?
    15.            ' stream.Read(Buffer, 0, Buffer.Length)
    16.            
    17.            client.Close()
    18.  
    19.            Label1.Text = "Worked .... Disconnected ...."
    20.  
    21.        Catch e As SocketException
    22.            Label1.Text = "ErrorInSocket"
    23.        Finally
    24.            server.Stop()
    25.        End Try

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