using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MT_Tools_InstaType_CLIENT;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace MT_Tools_gudIRC_CLIENT
{
public partial class Form1: Form
{
UdpClient udpClient = new UdpClient();
private BackgroundWorker clientWorker;
public Form1()
{
InitializeComponent();
Global.ServerIP = "127.0.0.1";
Global.ServerPort = "13354";
// 147.185.221.23:4278
Global.IntPort = int.Parse(Global.ServerPort);
using (InputBox inputForm = new InputBox("Nickname: (username)", "InputBox")) { DialogResult inputboxform = inputForm.ShowDialog(); if (inputboxform == DialogResult.OK) { Global.Nick = inputForm.UserInput; } else if (inputboxform == DialogResult.Cancel) Environment.Exit(0); }
using (InputBox inputForm = new InputBox("Password:", "InputBox")) { DialogResult inputboxform = inputForm.ShowDialog(); if (inputboxform == DialogResult.OK) { Global.Password = inputForm.UserInput; } else if (inputboxform == DialogResult.Cancel) Environment.Exit(0); }
NickLabel.Text = Global.Nick;
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, Global.IntPort);
byte[] message = System.Text.Encoding.ASCII.GetBytes("IAmConnecting " + Global.Nick + ":" + Global.Password);
udpClient.Send(message, message.Length, Global.ServerIP, Global.IntPort);
byte[] commandData = udpClient.Receive(ref remoteEndPoint);
string rajtOrWrok = System.Text.Encoding.ASCII.GetString(commandData);
if (rajtOrWrok == "true")
{
MessageBox.Show("
} else
{
MessageBox.Show("
Application.Exit();
}
message = System.Text.Encoding.ASCII.GetBytes(Global.Nick + " joined the chat");
udpClient.Send(message, message.Length, Global.ServerIP, Global.IntPort);
clientWorker = new BackgroundWorker();
clientWorker.DoWork += Client;
clientWorker.RunWorkerAsync();
}
///
/// whole client code
///
private void Client(object sender, DoWorkEventArgs e)
{
while (true)
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, Global.IntPort);
byte[] message = System.Text.Encoding.ASCII.GetBytes("keepAliveMessage_xZewTY");
udpClient.Send(message, message.Length, Global.ServerIP, Global.IntPort);
byte[] commandData = udpClient.Receive(ref remoteEndPoint);
string IrcChat = System.Text.Encoding.ASCII.GetString(commandData);
MessagesLabel.Text = IrcChat;
MessagesLabel.SelectionStart = MessagesLabel.Text.Length;
MessagesLabel.ScrollToCaret();
}
}
///
/// Button handling
///
private void DisconnectBtn_Click(object sender, EventArgs e)
{
//udpClient.Close();
//Form1 theform = new Form1();
//new Form1();
Task.Run(() => MessageBox.Show("Not working for now..."));
}
private void SendBtn_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(MessageInput.Text)) return;
byte[] message = System.Text.Encoding.ASCII.GetBytes("(" + Global.Nick + ") " + MessageInput.Text);
udpClient.Send(message, message.Length, Global.ServerIP, Global.IntPort);
MessageInput.Text = "";
}
///
/// Send Disconnect before close
///
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
byte[] message = System.Text.Encoding.ASCII.GetBytes(Global.Nick + " left the chat");
udpClient.Send(message, message.Length, Global.ServerIP, Global.IntPort);
}
}
///
/// Global Variables
///
public static class Global
{
public static string ServerIP;
public static string ServerPort;
public static int IntPort;
public static string Nick = "Failed To Load";
public static string Password;
}
}
}
< /code>
код сервера: < /p>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MT_Tools_InstaType_CLIENT;
using MT_Tools_gudIRC_SERVER.Properties;
using System.Security.Policy;
using System.Threading;
using System.IO;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace MT_Tools_gudIRC_SERVER
{
public partial class Form1 : Form
{
private BackgroundWorker serverWorker;
public Form1()
{
InitializeComponent();
using (InputBox inputForm = new InputBox("Bind on port: (default: 13354)", "InputBox")) { if (inputForm.ShowDialog() == DialogResult.OK) { Global.Port = inputForm.UserInput; } }
if (string.IsNullOrWhiteSpace(Global.Port))
Global.Port = "13354";
Global.IntPort = int.Parse(Global.Port);
serverWorker = new BackgroundWorker();
serverWorker.DoWork += Server;
serverWorker.RunWorkerAsync();
}
///
/// UDP irc Server
///
private void Server(object sender, DoWorkEventArgs e)
{
UdpClient udpServer = new UdpClient(Global.IntPort);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, Global.IntPort);
while (true)
{
if (Global.isServerActive == 1)
{
SetStatus("Server is listening on port " + Global.Port);
byte[] receivedData = udpServer.Receive(ref remoteEndPoint);
Global.receivedMessage = System.Text.Encoding.ASCII.GetString(receivedData);
if (Global.receivedMessage.StartsWith("IAmConnecting "))
{
string iconectremove = Global.receivedMessage.Replace("IAmConnecting ", "");
string[] usrandpass = iconectremove.Split(':');
if (CheckLogin(usrandpass[0], usrandpass[1]))
{
byte[] logedinsucesfuly = System.Text.Encoding.ASCII.GetBytes("true");
udpServer.Send(logedinsucesfuly, logedinsucesfuly.Length, remoteEndPoint);
}
else
{
byte[] logedinsucesfuly = System.Text.Encoding.ASCII.GetBytes("false");
udpServer.Send(logedinsucesfuly, logedinsucesfuly.Length, remoteEndPoint);
}
}
else if (Global.receivedMessage.StartsWith("(") && Global.receivedMessage.EndsWith(") /cls"))
{
MessagesBox.Text = "Chat cleared";
AddMsg(Global.receivedMessage);
}
else if (Global.receivedMessage == "keepAliveMessage_xZewTY")
{
}
else
{
AddMsg(Global.receivedMessage);
}
// Odpověď zpět na klienta
byte[] responseMessage = System.Text.Encoding.ASCII.GetBytes(MessagesBox.Text);
udpServer.Send(responseMessage, responseMessage.Length, remoteEndPoint);
} else
{
SetStatus("Server is NOT listening on port " + Global.Port);
}
}
}
static bool CheckLogin(string username, string password)
{
string filePath = "users.dat";
if (!File.Exists(filePath))
{
return false;
}
// Načtení všech řádků a ověření uživatele
foreach (string line in File.ReadAllLines(filePath))
{
string[] parts = line.Split(':');
if (parts.Length == 2 && parts[0] == username && parts[1] == password)
{
return true; // Přihlášení úspěšné
}
}
return false; // Přihlášení neúspěšné
}
//SetStatus("Listening on port" + Global.Port);
///
/// Fade Animations, and other functions
///
private void FadeOut()
{
for (double i = 1; i >= 0; i -= 0.1)
{
this.Opacity = i;
System.Threading.Thread.Sleep(50); // Nejběžnější způsob, jak zpomalit animaci
}
this.Hide(); // Nakonec skryje formulář
}
private void FadeIn()
{
this.Show();
// Tady dej kód pro FadeIn animaci
this.Opacity = 0;
for (double i = 0; i
Шаги для воспроизведения: < /p>
Создать users.dat с вашим именем пользователя в Direcotry с serevr < /p>
Start Сервер в порте по умолчанию
запустить клиент
Войти в
Закрыть клиент
запустить клиент
Войдите в систему
Я попробовал Ctrl+z-ing, когда это сработало, но без помощи.
Информация:
tcp или udp? udp < /p>
подпишите формат файла:
user: пароль < /p>
Подробнее здесь: https://stackoverflow.com/questions/794 ... nd-sign-in