Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# อยาก Check ว่า Client ต่อ TCP SERVER อยู่รึเปล่าต้องทำไงครับ



 

C# อยาก Check ว่า Client ต่อ TCP SERVER อยู่รึเปล่าต้องทำไงครับ

 



Topic : 089754

Guest




คิอผมโปรเจคเกี่ยวกับ 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 : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-01-24 23:28:06 By : numton View : 1214 Reply : 1
 

 

No. 1

Guest


ping






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-01-25 07:48:02 By : ห้ามตอบเกินวันละ 2 กระทู้
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# อยาก Check ว่า Client ต่อ TCP SERVER อยู่รึเปล่าต้องทำไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่