יום ראשון, 30 בינואר 2022

מיפוי כונן רשת לאות כונן במחשב

מיפוי כונן רשת - נעשה בעבר כדי למפות database מרוחק

* חשוב לוודא תקשורת בין שני המחשבים (בעזרת ping)

שלבים:

1. למצוא את כתובת ip של המחשב (עם database) - למשל 125.16.54.77

2. לעשות מיפוי כונן רשת - לחיצה ימנית על סמל 'המחשב שלי' >  בחירת אות כונן (למשל A:) > 

לבחור ספריה בכונן הרשת שאנו רוצים, למשל:

\\125.16.54.77\d$\databases\

צריך לבחור ב-Connect using different Credntials אם רוצים להזין שםמשתמש וסיסמה לכונן.

3. לחיצה על Finish - אם סימנו V בסעיף הקודם, אז יפתח חלון Windows Security - נלחץ על More Choices - Use Different Accout ונזין את פרטי ההתחברות

4. כדי לנתקן מיפוי כונן - יש ללחוץ לחיצה ימנית על הכונן בחלון 'המחשב שלי' > Disconnect network drive





יום שלישי, 11 בינואר 2022

C# - Udp Server & Client

UDP Server:


  Thread trdServerUdp = 
     new Thread(new ThreadStart(serverThread));
  trdServerUdp.Start();
}

void ServerThread() {
  UdpClient server = new UdpClient(port);
  while (true) {
    IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(ip), port);
    byte[] receiveBytes = server.Receive(ref remoteIpep);
    string str = Encoding.ASCII.GetString(receiveBytes);
    Console.WriteLine($"{remoteIpep}: {str}");
  }
}
  
UDP Client:


  string ip = "127.0.0.1";
  int port = 8080;

  string input;
  UdpClient client = new UdpClient();
  client.Connect(ip, port);

  byte[] data;
  while (true) {
    input = Console.ReadLine();
    if (input == "exit") break;
    data = Encoding.ASCII.GetBytes(input);
    client.Send(data, data.Length);
  }
}
  

C# - Tcp Server & Client

קוד סרבר שמחכה עד שקליינט יתחבר, ואז מקבל הודעות ומחזיר אותן אל הקליינט:

TCP Server:

void server_foo(){
  int port = 9050;
  int recv;
  byte[]data = new byte[1024];
  IPEndPoint ipep = new IPEndPoint(IPAdress.Any, port);
  Socket newsock = new Socket(AddressFamily.InterNetwork, 
                        SocketType.Stream,
                        ProtocolType.Tcp);

  newsock.Bind(ipep);
  newsock.Listen(10);

  Console.WriteLine("waiting for client...");
  Socket client = newsoc.Accept();

  IPEndPoint clientep = (IPEndPoint )client.RemoteEndPoint;
  Console.WriteLine($"Connected with {clientep.Address} at port {clientep.Port}");
  data = Encoding.ASCII.GetBytes("[CONNECTED TO SERVER]");
  client.Send(data, data.Length, SocketFlag.None);

  while (true) {
     try {
        data = new byte[1024];
        recv = client.Receive(data);
        Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
        if(recv == 0) break; // 0 == exit
        client.Send(data, recv, SocketFlags.None);
     }
     catch (Exception ex) {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
        break;
     }
     finally {
        client.Close();
        newsock.Close();
     }
  }
  Console.WriteLine($"Disconnected from {clientep.Address}:{clientep.Port}");
}
  
לחץ כאן כדי להעתיק את הקוד

קוד קליינט שמנסה כל 5 שניות להתחבר:

TCP Client:

void client_foo(){
   byte[]data = new byte[1024];
   string input, stringData;
   IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port);
   Socket server = new Socket(AddressFamily.InterNetwork, 
        SocketType.Stream,
        ProtocolType.Tcp);
  
  //try to connect:
  while (true) {
    try {
      server.Connect(ipep);
      Console.WriteLine("CONNECTED");
      break;
    }
    catch {
      //try to reconnect:
      Console.WriteLine("Unable to connect: " + ex.Message);
      Console.WriteLine("Trying to reconnect in:");
  
      for (int i = 1; i <= 5; i++) {
        Console.Write(i);
        Console.CursorLeft = 0;
        Thread.Sleep(1000);
      }
    }
  }

  // connected
  int recv = server.Receive(data);
  stringData = Encoding.ASCII.GetString(data, 0 ,recv);
  Console.WriteLine(stringData);
                            
  while (true) {
    input = Console.ReadLine();
    if (input == "exit") break;

    // send message
    server.Send(Encoding.ASCII.GetBytes(input));
                            
    // get message back
    data = new byte[1024];
    recv = server.Receive(data);
    stringData = Encoding.ASCII.GetString(data, 0 ,recv);
    Console.WriteLine(stringData);
  }

  //close connection to server
  Console.WriteLine("Disconnected from server");
  server.Shutdown(SocketShutdown.Both);
  server.Close();
}
לחץ כאן כדי להעתיק את הקוד