我一直在制作TCP服务器,您可以在其中创建帐户并登录。然后,服务器将使用您的帐户数据制作记事本。
我遇到关闭连接的客户端问题,服务器停止。
任何人都可以修复此代码吗?不介意长度,我一直在测试和试验很多东西,所以代码似乎很长,但这不是“最终”项目,它只是一个测试版本。
public static void Main()
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("IP", 500);
// use the ipaddress as in the server program
Console.Write("Write: Create/Login");
Console.WriteLine();
String str_ = Console.ReadLine();
Stream stm1 = tcpclnt.GetStream();
ASCIIEncoding asen_ = new ASCIIEncoding();
byte[] ba_ = asen_.GetBytes(str_);
stm1.Write(ba_, 0, ba_.Length);
if (str_ == "Create")
{
start1:
string Order = "";
Stream stm2 = tcpclnt.GetStream();
Console.Clear();
Console.WriteLine("Username:");
String usernam = Console.ReadLine();
byte[] usern = asen_.GetBytes(usernam);
stm2.Write(usern, 0, usern.Length);
byte[] Code = new byte[100];
int St = stm2.Read(Code, 0, 100);
for (int a = 0; a < St; a++)
{
Order += Convert.ToChar(Code[a]);
}
if (Order == "Error")
{
Console.WriteLine("Username already exist! Choose another one! Press Enter");
Console.ReadLine();
goto start1;
}
else
{
Order = "";
Stream stm3 = tcpclnt.GetStream();
Console.WriteLine("Password:");
String Passwor = Console.ReadLine();
byte[] Pass = asen_.GetBytes(Passwor);
stm3.Write(Pass, 0, Pass.Length);
byte[] Code1 = new byte[100];
int St1 = stm3.Read(Code1, 0, 100);
for (int a = 0; a < St1; a++)
{
Order += Convert.ToChar(Code1[a]);
}
if (Order == "Created")
{
Order = "";
Console.Clear();
Console.WriteLine("Account created!");
Stream stm4 = tcpclnt.GetStream();
String IDoutput = Console.ReadLine();
byte[] IDout = asen_.GetBytes(IDoutput);
stm4.Write(IDout, 0, IDout.Length);
byte[] Code2 = new byte[100];
int St2 = stm4.Read(Code2, 0, 100);
for (int a = 0; a < St2; a++)
{
Order += Convert.ToChar(Code2[a]);
}
Console.WriteLine("This is your ID:" + Order + ". Remember it, u will use it to login!");
Console.ReadLine();
tcpclnt.Close();
}
}
}
if (str_ == "Login")
{
start3:
string Order = "";
Stream stm2 = tcpclnt.GetStream();
Console.Clear();
Console.WriteLine("ID:");
String usernam = Console.ReadLine();
byte[] usern = asen_.GetBytes(usernam);
stm2.Write(usern, 0, usern.Length);
Stream stm3 = tcpclnt.GetStream();
Console.Clear();
Console.WriteLine("Password:");
String usernam1 = Console.ReadLine();
byte[] usern1 = asen_.GetBytes(usernam1);
stm3.Write(usern1, 0, usern1.Length);
byte[] Code = new byte[100];
int St = stm2.Read(Code, 0, 100);
for (int a = 0; a < St; a++)
{
Order += Convert.ToChar(Code[a]);
}
if (Order == "Correct")
{
Console.WriteLine("Correct");
Console.ReadLine();
tcpclnt.Close();
}
if (Order == "Wrong")
{
Console.WriteLine("Combination is incorrect, please try again");
Console.ReadLine();
goto start3;
}
}
Stream stm_ = tcpclnt.GetStream();
byte[] bb_ = new byte[100];
int k_ = stm_.Read(bb_, 0, 100);
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
bool c = false;
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
string ReadID = "";
string ReadLine = "";
for (int i = 0; i < k; i++)
if (bb[i] != '*' && c == false)
ReadID += Convert.ToChar(bb[i]);
else
for (int a = i + 1; a < k; a++)
{
i++;
c = true;
ReadLine += Convert.ToChar(bb[a]);
}
Console.WriteLine(ReadID);
Console.WriteLine(ReadLine);
Console.ReadLine();
tcpclnt.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
try
{
ASCIIEncoding asen = new ASCIIEncoding();
IPAddress ipAddress = IPAddress.Parse("IP");
Console.WriteLine("Starting TCP listener...");
TcpListener listener = new TcpListener(ipAddress, 500);
listener.Start();
while (true)
{
Socket client = listener.AcceptSocket();
Console.WriteLine("Connection accepted.");
string ReadID = "";
string ReadLine = "";
string password = "";
string Order = "";
var childSocketThread = new Thread(() =>
{
byte[] data__ = new byte[100];
int size__ = client.Receive(data__);
for (int a = 0; a < size__; a++)
{
Order += Convert.ToChar(data__[a]);
}
if (Order == "Create")
{
while (true)
{
start:
string username = "";
byte[] data_ = new byte[100];
int size_ = client.Receive(data_);
for (int a = 0; a < size_; a++)
{
username += Convert.ToChar(data_[a]);
}
System.IO.StreamReader reader =
new System.IO.StreamReader(@"C:\Users\Gregor\Desktop\USernames.txt");
string line_ = "";
int o = 0;
while ((line_ = reader.ReadLine()) != null)
{
if (line_ == username)
{
client.Send(asen.GetBytes("Error"));
reader.Close();
goto start;
}
}
reader.Close();
client.Send(asen.GetBytes("OK"));
byte[] dataa = new byte[100];
int sizea = client.Receive(dataa);
for (int a = 0; a < sizea; a++)
{
password += Convert.ToChar(dataa[a]);
}
string[] lines = { username, password };
System.IO.StreamReader reader2 =
new System.IO.StreamReader(@"C:\Users\Gregor\Desktop\IDcount.txt");
string idcounter = "";
idcounter = reader2.ReadLine();
int idcounterint;
bool idcounte = Int32.TryParse(idcounter, out idcounterint);
idcounterint++;
idcounter = Convert.ToString(idcounterint);
reader2.Close();
System.IO.File.WriteAllText(@"C:\Users\Gregor\Desktop\IDcount.txt", idcounter);
System.IO.File.WriteAllLines(@"C:\Users\Gregor\Desktop\User" + idcounter + ".txt", lines);
reader.Close();
System.IO.File.AppendAllText(@"C:\Users\Gregor\Desktop\Usernames.txt", (username + Environment.NewLine));
client.Send(asen.GetBytes("Created"));
client.Send(asen.GetBytes(idcounter));
username = "";
break;
}
}
if (Order == "Login")
{
byte[] data1 = new byte[100];
int size1 = client.Receive(data1);
for (int a = 0; a < size1; a++)
{
ReadID += Convert.ToChar(data1[a]);
}
byte[] data2 = new byte[100];
int size2 = client.Receive(data2);
for (int a = 0; a < size2; a++)
{
password += Convert.ToChar(data2[a]);
}
System.IO.StreamReader reader1 =
new System.IO.StreamReader(@"C:\Users\Gregor\Desktop\User" + ReadID + ".txt");
string read1 = reader1.ReadLine();
string read2 = reader1.ReadLine();
if (password == read2)
{
client.Send(asen.GetBytes("Correct"));
}
else
{
client.Send(asen.GetBytes("Wrong"));
}
}
byte[] data = new byte[100];
int size = client.Receive(data);
bool c = false;
for (int i = 0; i < size; i++)
{
if (data[i] != ' ' && c == false)
ReadID += Convert.ToChar(data[i]);
else
for (int a = i + 1; a < size; a++)
{
c = true;
ReadLine += Convert.ToChar(data[i]);
}
}
int ReadID_;
bool result = Int32.TryParse(ReadID, out ReadID_);
int counter = 0;
string line = "";
string ID = ReadID;
string rLine = "";
System.IO.StreamReader file =
new System.IO.StreamReader(@"C:\Users\Gregor\Desktop\User" + ReadID + ".txt");
while ((line = file.ReadLine()) != null)
{
if (counter == ReadID_)
rLine = line;
counter++;
}
file.Close();
client.Send(asen.GetBytes(ID + "*" + rLine));
Console.WriteLine();
client.Close();
});
childSocketThread.Start();
}
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.StackTrace);
Console.ReadLine();
}
}
}