|
|
|
C# อยาก Check ว่า Client ต่อ TCP SERVER อยู่รึเปล่าต้องทำไงครับ |
|
|
|
|
|
|
|
คิอผมโปรเจคเกี่ยวกับ GPS ครับ
GPS ก็จะส่งข้อมูลเข้า Server แต่พอไปในที่ ๆ ไม่มีสัญญาณ GPRS มันจะหยุดส่ง
Server ก็ไม่รู้ว่า Client หยุดไปแล้วเลยค้างอยู่ที่เดิม ต้องทำไงครับถึงจะรู้ว่า
Client ขาดการติดต่อไปแล้ว
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Int32 realport;
public Form1()
{
InitializeComponent();
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
realport = Convert.ToInt32(numericUpDown1.Value);
numericUpDown1.Enabled = false;
button1.Text = "Stop";
new Thread(new ThreadStart(StartClient)).Start();
}
else if (button1.Text == "Stop")
{
Application.Exit();
}
}
public void StartClient()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
//Int32 port = 13000;
//IPAddress localAddr = IPAddress.any;
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Any, realport);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
String time = null;
// Enter the listening loop.
while (true)
{
AddItem("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
AddItem("-----------------------------------------");
time = Convert.ToString(DateTime.Now);
AddItem(time);
AddItem("Connected!");
AddItem("-----------------------------------------");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
string[] arInfo = new string[2000];
string[] artime = new string[2000];
string[] latlong = new string[2000];
char[] splitter_one = { '|' };
char[] splitter_two = { ',' };
int check_one, check_two;
string latitude, longtitude, flat, slat;
double dlat;
arInfo = data.Split(splitter_one);
check_one = data.IndexOf('|');
Console.WriteLine(check_one);
longtitude = "";
latitude = "";
string[] timee = new string[2000];
string speed, fuel;
speed = "";
fuel = "";
if (check_one != -1)
{
check_two = arInfo[1].IndexOf(',');
if (check_two != -1)
{
latlong = arInfo[1].Split(splitter_two);
if (latlong[3].Length == 9)
{
flat = latlong[3].Substring(0, 2);
slat = latlong[3].Substring(2, 7);
dlat = Convert.ToSingle(slat);
dlat = dlat / 60;
dlat = dlat + Convert.ToInt16(flat);
latitude = string.Format("{0:F5}", dlat);
}
if (latlong[3].Length == 10)
{
flat = latlong[3].Substring(0, 3);
slat = latlong[3].Substring(3, 7);
dlat = Convert.ToSingle(slat);
dlat = dlat / 60;
dlat = dlat + Convert.ToInt16(flat);
latitude = string.Format("{0:F5}", dlat);
}
if (latlong[5].Length == 9)
{
flat = latlong[5].Substring(0, 2);
slat = latlong[5].Substring(2, 7);
dlat = Convert.ToSingle(slat);
dlat = dlat / 60;
dlat = dlat + Convert.ToInt16(flat);
longtitude = string.Format("{0:F5}", dlat);
}
if (latlong[5].Length == 10)
{
flat = latlong[5].Substring(0, 3);
slat = latlong[5].Substring(3, 7);
dlat = Convert.ToSingle(slat);
dlat = dlat / 60;
dlat = dlat + Convert.ToInt16(flat);
longtitude = string.Format("{0:F5}", dlat);
}
timee[0] = (arInfo[6].Substring(0, 4));
timee[1] = (arInfo[6].Substring(4, 2));
timee[2] = (arInfo[6].Substring(6, 2));
timee[3] = (arInfo[6].Substring(8, 2));
int hr;
int dayy;
dayy = Convert.ToInt16(timee[2]);
hr = Convert.ToInt16(timee[3]) + 7;
if ((hr / 24) >= 1)
{
hr = hr % 24;
dayy = Convert.ToInt16(timee[2]) +1;
}
timee[3] = Convert.ToString(hr);
timee[4] = (arInfo[6].Substring(10, 2));
timee[5] = (arInfo[6].Substring(12, 2));
fuel = (arInfo[8].Substring(0, 4));
time = timee[0] + "-" + timee[1] + "-" + dayy + " " + timee[3] + ":" + timee[4] + ":" + timee[5];
speed = latlong[7];
string connString = "Server=localhost;Port=3306;Database=cmlk;Uid=root;password=1472552; ";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "Insert into data (time,lat,lng,speed,fuel) value('" + time + "','" + latitude + "','" + longtitude + "','" + speed + "','" + fuel + "')";
conn.Open();
check_one = 1;
command.ExecuteNonQuery();
conn.Close();
}
else
{
AddItem(time);
AddItem("Gps No Signal");
}
}
else
{
AddItem(time);
AddItem("Gps No Signal");
}
foreach (string da in arInfo)
{
AddItem(da);
}
AddItem("---------------------------------------------");
if (check_one == 1)
{
AddItem(time);
AddItem("*** Add To DataBase Complete ***");
AddItem("---------------------------------------------");
button2_Click(null,null);
server.Stop();
}
}
//Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
//AddItem(String.Format("Received: {0}", data));
// data = data.ToUpper();
//byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
// stream.Write(msg, 0, msg.Length);
// Console.WriteLine("Sent: {0}", data);
client.Close();
}
// Shutdown and end connection
// client.Close();
}
catch (SocketException ex)
{
AddItem("SocketException: {0}" + ex);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
delegate Int32 SetControlCallBack(object o);
private Int32 AddItem(object o)
{
int i = 0;
if (this.listBox1.InvokeRequired)
{
SetControlCallBack d = new SetControlCallBack(AddItem);
this.listBox1.Invoke(d, new object[] { o });
}
else
{
i = this.listBox1.Items.Add(o);
}
return i;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
//listBox1.Items.Clear();
//listBox1.SelectedIndex = listBox1.Items.Count - 1;
//listBox1.SelectedIndex = -1;
}
}
}
Tag : - - - -
|
|
|
|
|
|
Date :
2013-01-24 23:28:06 |
By :
numton |
View :
1255 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ping
|
|
|
|
|
Date :
2013-01-25 07:48:02 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|