示例#1
0
        ConnectionInfoStruct ConnectSocket(string hostname, int port, ProtocolType ThisProtocol)
        {
            ConnectionInfoStruct result     = new ConnectionInfoStruct();
            IPHostEntry          ipHostInfo = null;

            ipHostInfo         = Dns.GetHostEntry(hostname);
            result.HostName    = ipHostInfo.HostName;
            result.IPAddresses = new string[ipHostInfo.AddressList.Length];

            for (int i = 0; i < ipHostInfo.AddressList.Length; i++)
            {
                result.IPAddresses[i] = ipHostInfo.AddressList[i].ToString();
            }

            foreach (IPAddress currentIP in ipHostInfo.AddressList)
            {
                Console.WriteLine("Соединяюсь с " + currentIP);
                IPEndPoint Target = new IPEndPoint(currentIP, port);

                Socket tempSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ThisProtocol);

                tempSocket.Connect(Target);

                if (tempSocket.Connected)
                {
                    // Use the SelectWrite enumeration to obtain Socket status.
                    if (tempSocket.Poll(-1, SelectMode.SelectWrite))
                    {
                        Console.WriteLine("В этот сокет можно писать");
                    }
                    else if (tempSocket.Poll(-1, SelectMode.SelectRead))
                    {
                        Console.WriteLine("Этот сокет может только отсылать");
                    }
                    else if (tempSocket.Poll(-1, SelectMode.SelectError))
                    {
                        Console.WriteLine("В этом сокете ошибка");
                    }

                    result.ConnectionSocket = tempSocket;
                    result.HostInfo         = ipHostInfo;
                    result.EndPoint         = Target;

                    break;
                }
            }

            return(result);
        }
示例#2
0
        string SocketSendReceive(ConnectionInfoStruct ConnectionInfothis)
        {
            Socket s = ConnectionInfothis.ConnectionSocket;

            if (s == null)
            {
                Console.WriteLine("Connection failed");
                return("");
            }
            else
            {
                Console.WriteLine("Началась передача");
            }

            string request = "GET /inbox/ret23454 HTTP/1.1\r\n" +
                             "Accept-Encoding: gzip,deflate\r\n" +
                             "Host: " + ConnectionInfothis.EndPoint.Address.ToString() + "\r\n" +
                             "Connection: Close\r\n\r\n";

            Byte[] bytesSent     = System.Text.Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];


            // Send request to the server.
            s.Send(bytesSent, bytesSent.Length, 0);

            // Receive the server home page content.
            int bytes = 0;

            string page = "";

            // The following will block until te page is transmitted.
            do
            {
                bytes = s.Receive(bytesReceived);
                page += System.Text.Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            }while (bytes > 0);

            return(page);
        }
示例#3
0
        public void Get()
        {
            bool change = false;

            while (true)
            {
                ConnectionInfoStruct connectionInfo = new ConnectionInfoStruct()
                {
                    MACAddress = new byte[6], IPAddress = new byte[4], SubnetMask = new byte[4]
                };

                try
                {
                    GetNetworkInfo(ref connectionInfo);
                }
                catch (Exception e) { }

                if (connectionInfo.IsConnected == 0)
                {
                    if (Database.GlobalConnectionInfo.ConnectionState != ConnectionState.DISCONNECTED)
                    {
                        change = true;
                        Database.GlobalConnectionInfo.ConnectionState = ConnectionState.DISCONNECTED;
                    }

                    if (change)
                    {
                        publisher.ReturnConnectionInfo(Database.GlobalConnectionInfo);
                    }

                    change = false;

                    Thread.Sleep(isConnectedSleep);
                    continue;
                }
                else
                {
                    if (Database.GlobalConnectionInfo.ConnectionState != ConnectionState.CONNECTED)
                    {
                        change = true;
                        Database.GlobalConnectionInfo.ConnectionState = ConnectionState.CONNECTED;
                    }
                }

                Database.GlobalConnectionInfo.ConnectionState = ConnectionState.CONNECTED;

                if (Database.GlobalConnectionInfo.Name != connectionInfo.Name)
                {
                    change = true;
                    Database.GlobalConnectionInfo.Name = connectionInfo.Name;
                }
                if (Database.GlobalConnectionInfo.Description != connectionInfo.Description)
                {
                    change = true;
                    Database.GlobalConnectionInfo.Description = connectionInfo.Description;
                }
                if (Database.GlobalConnectionInfo.FriendlyName != connectionInfo.FriendlyName)
                {
                    change = true;
                    Database.GlobalConnectionInfo.FriendlyName = connectionInfo.FriendlyName;
                }
                for (int i = 0; i < 4; i++)
                {
                    if (Database.GlobalConnectionInfo.IPAddress[i] != connectionInfo.IPAddress[i])
                    {
                        change = true;
                        Database.GlobalConnectionInfo.IPAddress = connectionInfo.IPAddress;
                        break;
                    }
                }
                for (int i = 0; i < 6; i++)
                {
                    if (Database.GlobalConnectionInfo.MACAddress[i] != connectionInfo.MACAddress[i])
                    {
                        change = true;
                        Database.GlobalConnectionInfo.MACAddress = connectionInfo.MACAddress;
                    }
                }
                for (int i = 0; i < 4; i++)
                {
                    if (Database.GlobalConnectionInfo.SubnetMask[i] != connectionInfo.SubnetMask[i])
                    {
                        change = true;
                        Database.GlobalConnectionInfo.SubnetMask = connectionInfo.SubnetMask;
                    }
                }

                if (change)
                {
                    publisher.ReturnConnectionInfo(Database.GlobalConnectionInfo);
                }

                change = false;

                Thread.Sleep(isConnectedSleep);
            }
        }
示例#4
0
 public static extern void GetNetworkInfo(ref ConnectionInfoStruct name);
示例#5
0
        public List <Host> SniffForHosts()
        {
            ConnectionInfoStruct connectionInfoStruct = new ConnectionInfoStruct();

            connectionInfoStruct.SubnetMask     = Database.GlobalConnectionInfo.SubnetMask;
            connectionInfoStruct.DefaultGateway = new byte[4] {
                192, 168, 0, 1
            };
            connectionInfoStruct.IPAddress  = Database.GlobalConnectionInfo.IPAddress;
            connectionInfoStruct.MACAddress = Database.GlobalConnectionInfo.MACAddress;
            connectionInfoStruct.Name       = Database.GlobalConnectionInfo.Name;
            connectionInfoStruct.Hosts      = new byte[200];
            connectionInfoStruct.Sleep      = (this.sniffForHostsInterval) * 1000;

            SniffForHosts(ref connectionInfoStruct);

            List <Host> hosts = new List <Host>();

            int j = 0;

            while (connectionInfoStruct.HostCount > 0)
            {
                if (hostsIPends.Contains(connectionInfoStruct.Hosts[j]) || connectionInfoStruct.Hosts[j] == Database.GlobalConnectionInfo.IPAddress[3])
                {
                    j += 7;
                    connectionInfoStruct.HostCount--;
                    continue;
                }

                hostsIPends.Add(connectionInfoStruct.Hosts[j]);

                Host host = new Host();

                // bug ako subnet mask nije odgovarajuc!!!!!
                host.IPAddressArray
                    = new byte[4] {
                    Database.GlobalConnectionInfo.IPAddress[0],
                    Database.GlobalConnectionInfo.IPAddress[1],
                    Database.GlobalConnectionInfo.IPAddress[2],
                    connectionInfoStruct.Hosts[j]
                    };

                for (int k = 0; k < 4; k++)
                {
                    host.IPAddressString += host.IPAddressArray[k];

                    if (k != 3)
                    {
                        host.IPAddressString += ".";
                    }
                }

                host.MACAddressArray = new byte[6];
                for (int k = 0; k < 6; k++)
                {
                    host.MACAddressArray[k] = connectionInfoStruct.Hosts[++j];

                    host.MACAddressString += host.MACAddressArray[k].ToString("X");

                    if (k != 5)
                    {
                        host.MACAddressString += ":";
                    }
                }

                hosts.Add(host);

                j++;
                connectionInfoStruct.HostCount--;
            }

            return(hosts);
        }
示例#6
0
 public static extern void SniffForHosts(ref ConnectionInfoStruct connectionInfo);