Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a program that checking ip address continuously

i have a code that checking ip address and port, like this

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.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace WinNetworkIOCS {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void ConnectButton_Click(object sender, EventArgs e) {
            DisableFields();
            DoNetworkingConnection();
        }

        private void DisableFields() {
            PortBox.Enabled = false;
            IPAddressBox.Enabled = false;
            SendMessageBox.Enabled = false;
            ConnectButton.Enabled = false;
        }

        private void EnableFields() {
            PortBox.Enabled = true;
            IPAddressBox.Enabled = true;
            SendMessageBox.Enabled = true;
            ConnectButton.Enabled = true;
        }

        private void WriteToStatusBar(string Message) {
            //EnableFields();
            ThreadHelperClass.SetText(this, lblStatus, Message);
        }

        private void DoNetworkingConnection() {
            Thread MyThread = null;

            try {
                ThreadStart ThreadMethod = new ThreadStart(ConnectTo);
                MyThread = new Thread(ThreadMethod);
            } catch (Exception e) {
                WriteToStatusBar("Failed to create thread with error: " + e.Message);
                return;
            }

            try {
                MyThread.Start();
            } catch (Exception e) {
                WriteToStatusBar("The thread failed to start with error: " + e.Message);
            }
        }

        private void ConnectTo() {
            string ServerName = this.IPAddressBox.Text;
            int Port = System.Convert.ToInt32(this.PortBox.Text);

            WriteToStatusBar("IP Address: " + ServerName + "Port: " + Port);
            Socket ClientSocket = null;

            try {
                // Let's connect to a listening server
                try {
                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    WriteToStatusBar("Socket is OK...");
                } catch (Exception e) {
                    throw new Exception("Failed to create client Socket: " + e.Message);
                }

                IPEndPoint ServerEndPoint = new IPEndPoint(IPAddress.Parse(ServerName), Convert.ToInt16(Port));

                try {
                    ClientSocket.Connect(ServerEndPoint);
                    WriteToStatusBar("Connect() is OK...");
                } catch (Exception e) {
                    throw new Exception("Failed to connect client Socket: " + e.Message);
                }
            } catch (Exception e) {
                WriteToStatusBar(e.Message);
                ClientSocket.Close();
                return;
            }

            // Let's create a network stream to communicate over the connected Socket.
            NetworkStream ClientNetworkStream = null;

            try {
                try {
                    // Setup a network stream on the client Socket
                    ClientNetworkStream = new NetworkStream(ClientSocket, true);
                    WriteToStatusBar("Instantiating NetworkStream...");
                } catch (Exception e) {
                    // We have to close the client socket here because the network
                    // stream did not take ownership of the socket.
                    ClientSocket.Close();
                    throw new Exception("Failed to create a NetworkStream with error: " + e.Message);
                }

                StreamWriter ClientNetworkStreamWriter = null;

                try {
                    // Setup a Stream Writer
                    ClientNetworkStreamWriter = new StreamWriter(ClientNetworkStream);
                    WriteToStatusBar("Setting up StreamWriter...");
                } catch (Exception e) {
                    ClientNetworkStream.Close();
                    throw new Exception("Failed to create a StreamWriter with error: " + e.Message);
                }

                try {
                    ClientNetworkStreamWriter.Write(this.SendMessageBox.Text.ToString());
                    ClientNetworkStreamWriter.Flush();
                    WriteToStatusBar("We wrote " + this.SendMessageBox.Text.Length.ToString() + " character(s) to the server.");
                } catch (Exception e) {
                    throw new Exception("Failed to write to client NetworkStream with error: " + e.Message);
                }
            } catch (Exception e) {
                WriteToStatusBar(e.Message);
            } finally {
                // Close the network stream once everything is done
                ClientNetworkStream.Close();
            }
        }

        delegate void SetTextCallback(string text);
    }
}


but does not check the IP address and port continuously.
What should I add in my coding.


Thank you.
Posted

1 solution

Well - you wrote the code, didn't you? So why not change it? You understand how it works...

You aren't trying to "do it continuously" and probably, you don't want to: because if you create a loop to continuously run that code, it will block the port and cause whatever else you are trying to get to use it to fail because the port will be in use.

I suspect you have grabbed a lump of random code from the internet, and don't have a clue what to do with it: software development does not work like that. You have to think, and understand the code you use before you can modify it. Slamming lumps of random code together and hoping is not a successful development strategy.
 
Share this answer
 
Comments
FX Andy Widjaja 31-May-14 2:17am    
Are there any examples as I want because I've been looking for it but no corresponding?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900