469,343 Members | 5,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to send data as tcp stream using C#

I have a C# program which outputs x,y,z coorditate values of objects tracked by sensors connected to the system. These output values are stored as a text file.

All i need to do is, send the output (x,y,z) of the program directly as a tcp stream so that it can be received by another computer on the network.

It would be nice if someone can help me on how to send tcp stream using C#.
May 25 '07 #1
5 10509
Plater
7,872 Expert 4TB
Assuming that other computer is already to receive that tcp connection, check into the TcpClient object under System.Net.Sockets
May 25 '07 #2
Assuming that other computer is already to receive that tcp connection, check into the TcpClient object under System.Net.Sockets

The following is the code im using to send data from client to the server. The output of the entire program however is a series of x,y,z coordinates and other values but using the code im able to send only one instance of the output and not the entire series of outputs which are generated one after another. Please help me, i need to send the entire series of outputs generated one after another .


private void exportData(string person, DateTime time, double x, double y, double z,
double distance, double seconds)
{
// Write data to file: could modify this method to write to a database or other
// external system, filtering will work in the same way
StreamWriter file = (StreamWriter)files[person];
file.WriteLine("{0},{1},{2},{3},{4},{5}",
time.ToString("u"),x,y,z,distance,seconds);
Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}",
person,time.ToString(),x.ToString("0.00"),y.ToStri ng("0.00"),z.ToString("0.00"),
distance.ToString("0.00"),seconds.ToString("0.00") );


IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 9050);

Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

try
{
server.Connect(ipep);
}
catch (SocketException ex)
{
Console.WriteLine("Unable to connect to server.");
Console.WriteLine(ex.ToString());
return;
}


NetworkStream ns = new NetworkStream(server);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);

sw.WriteLine("{0},{1},{2},{3},{4},{5},{6}",
person, time.ToString(), x.ToString("0.00"), y.ToString("0.00"), z.ToString("0.00"),
distance.ToString("0.00"), seconds.ToString("0.00"));
sw.Flush();

}
Jun 4 '07 #3
Plater
7,872 Expert 4TB
Well, I don't see any looping mechanism there and you open a new socket every call there.
(PS: Don't forget to close all your files and sockets when you're done with them)
Jun 5 '07 #4
I have finally sent data from client to server using tcp connection. Now i need to send the same data using udp.

How do i replace the following code when implementing in udp ?.

sw.WriteLine("{0},{1},{2},{3},{4},{5},{6}",
person, time.ToString(), x.ToString("0.00"), y.ToString("0.00"), z.ToString("0.00"),
distance.ToString("0.00"), seconds.ToString("0.00"));
sw.Flush();
Jun 10 '07 #5
I have established connection between udp client and server. Now i need to send the data from client to server. How do i do that ?. The data consists of x,y,z coordinate values and other parameters which are generated continuosly and need to be sent at runtime.
Jun 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Fatih BOY | last post: by
2 posts views Thread by Cuong.Tong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.