|
-
Oct 28th, 2006, 05:57 PM
#1
Thread Starter
Junior Member
Data Across Network
How do you send data of any type (integer,string,etc) from one computer to another?
-
Oct 28th, 2006, 09:36 PM
#2
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
 
-
Oct 28th, 2006, 10:05 PM
#3
Thread Starter
Junior Member
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?
-
Oct 28th, 2006, 10:26 PM
#4
Re: Data Across Network
How big will the text be? This sounds more like UDP than TCP.
My usual boring signature: Nothing
 
-
Oct 28th, 2006, 10:53 PM
#5
Thread Starter
Junior Member
Re: Data Across Network
The text is a string variable.
-
Oct 28th, 2006, 11:26 PM
#6
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
 
-
Oct 29th, 2006, 03:27 AM
#7
Thread Starter
Junior Member
Re: Data Across Network
Were talking maybe one to five characters.
-
Oct 29th, 2006, 07:48 AM
#8
Junior Member
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:
Dim TcpClient As New TcpClient
If Not TcpClient.Connected Then
TcpClient.Connect(9000, "127.0.0.1")
End If
Dim networkStream As NetworkStream = TcpClient.GetStream()
' If networkStream.CanWrite Then
Dim sendbytes(10240) As Byte
sendbytes = Encoding.ASCII.GetBytes("TEST DATA TEST DATA")
networkStream.Write(sendBytes, 0, sendBytes.Length)
'End If
TcpClient.Close()
Listener... just spits the output to console....
VB Code:
Dim server As TcpListener
server = Nothing
'On Error Resume Next
Try
' Set the TcpListener on port 9000.
Dim port As Int32 = 9000
Dim localAddr As System.Net.IPAddress = System.Net.IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(20480) As Byte
Dim data As String = Nothing
' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine("Received: {0}", data)
' Process the data sent by the client.
data = data.ToUpper()
'Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
'stream.Write(msg, 0, msg.Length)
'Console.WriteLine("Sent: {0}", data)
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
What could be the problem of sending conents of a textbox > 512 bytes < 1024 bytes??????
-
Oct 29th, 2006, 11:02 AM
#9
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
 
-
Nov 1st, 2006, 03:56 PM
#10
Member
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:
' Sending an Image
Dim picture As Image
picture = Image.FromFile("c:\test.jpg")
Dim f As New FileInfo("c:\test.jpg")
Dim buffer(f.Length) As Byte
Dim client As New TcpClient
client.Connect(IPAddress.Parse("127.0.0.1"), 8000)
Dim stream As NetworkStream = client.GetStream()
stream.Write(picture, 0, picture.Length)
stream.Close()
client.Close()
Catch e As ArgumentNullException
' ...
Catch e As SocketException
' ...
End Try
Server...
VB Code:
Dim server As TcpListener = Nothing
Try
server = New TcpListener(8000)
server.Start()
Label1.Text = "Listening..."
Dim client As TcpClient = server.AcceptTcpClient()
Label1.Text = "Connected"
Dim stream As NetworkStream = client.GetStream()
' How do I write the data to a file?????
Dim buffer(CInt(client.ReceiveBufferSize)) As Byte ' Size of File?
' stream.Read(Buffer, 0, Buffer.Length)
client.Close()
Label1.Text = "Worked .... Disconnected ...."
Catch e As SocketException
Label1.Text = "ErrorInSocket"
Finally
server.Stop()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|