Lemme give u a little background on my problem. Ive created a client and server applications that receive and stream live time values. The client software is a clock that receives the time and displays it in a wpf app. Now when i use the DateTime.Now on the client app locally without using streaming the clock works great. However when I stream it from a server app which is also running on my computer or on a remote computer there is tremendous lag. That means, the time on the client app is updated at random intervals and not every second. Although when it updates, it updates to the correct time, but it doesnt tick like a normal clock should. Hope this makes sense...lol. Furthermore, when the client app receives the time, it displays the same time multiple times rather than just one line. Overall, the display is very ugly and the streaming is abysmal. Ive posted all my code, any suggestions on how to solve my problem. Thanks.
The client display app:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- using System.Threading;
- namespace MainClock
- {
- /// <summary>
- /// Interaction logic for Window1.xaml
- /// </summary>
- public partial class Window1 : Window
- {
- Client c = new Client();
- public Window1()
- {
- InitializeComponent();
- DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
- {
- this.label1.Content = getDateTime();
- }, this.Dispatcher);
- }
- public String getDateTime()
- {
- return c.getData();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- c.endConnection();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace MainClock
- {
- class Client
- {
- IPEndPoint ip;
- Socket server;
- byte[] data;
- int receivedDataLength;
- public Client()
- {
- ip = new IPEndPoint(IPAddress.Parse("130.63.184.179"), 9999);
- server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- server.Connect(ip);
- }
- catch (SocketException e)
- {
- //Console.WriteLine("Unable to connect to server.");
- return;
- }
- }
- public string getData()
- {
- data = new byte[1024];
- receivedDataLength = server.Receive(data);
- string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
- return stringData;
- }
- public void endConnection()
- {
- server.Shutdown(SocketShutdown.Both);
- server.Close();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace ClockServer
- {
- class Program
- {
- static void Main(string[] args)
- {
- IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.Bind(ip);
- socket.Listen(10);
- Console.WriteLine("Waiting for a client...");
- Socket client = socket.Accept();
- IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
- Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);
- Clock c = new Clock();
- //This is just a sort of timer that keeps the connection alive for 100000 counts. Its a stop gap arrangement.
- for (int i = 0; i < 100000; i++)
- {
- string time = c.getHour() + ":" + c.getMinutes() + ":" + c.getSeconds();
- string date = c.getDay() + "-" + c.getMonth() + "-" + c.getYear();
- string dateTime = time + " EDT( -5GMT)\n" + date;
- byte[] data = new byte[1024];
- data = Encoding.ASCII.GetBytes(dateTime);
- client.Send(data, data.Length, SocketFlags.None);
- }
- Console.WriteLine("Disconnected from {0}", clientep.Address);
- client.Close();
- socket.Close();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ClockServer
- {
- class Clock
- {
- public Clock()
- {
- }
- public String getHour()
- {
- return DateTime.Now.Hour.ToString();
- }
- public String getMinutes()
- {
- return DateTime.Now.Minute.ToString();
- }
- public String getSeconds()
- {
- return DateTime.Now.Second.ToString();
- }
- public String getDay()
- {
- return DateTime.Now.Day.ToString();
- }
- public String getMonth()
- {
- return DateTime.Now.Month.ToString();
- }
- public String getYear()
- {
- return DateTime.Now.Year.ToString();
- }
- }
- }