I am making a client for my server and I am stuck. My server is sending Unicode text, and my client recieves it. The problem is this piece of code:
Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim()))
I am trying to get the text from my Enum FTPCommands
with the number the server sent me. First my client is recieving the ID: 1 732847823
(Result from the debugger).
It splits correctly, in this case: replace[0].ToString()
-->"1" AND replace[1].ToString()
-->"732847823"
No error occurs!
On the second command, the client is recieving an error from the server:
8 Server is too full, try again laiter
And it also splits correctly, in this case:
replace[0].ToString()
-->"1" AND replace[0].ToString()
-->"8" AND replace[1].ToString()
-->"Server" AND replace[2].ToString()
-->"is" AND replace[3].ToString()
-->"too" AND replace[4].ToString()
-->"full," AND replace[4].ToString()
-->"try" AND replace[4].ToString()
-->"again" AND replace[4].ToString()
-->"laiter"
And now the error occurs! I dont know why this is working with the first command and not also with the command 8
. If I replace the replace[0].Trim()
with the number 8 it works fine. What is wrong with the code, or what could cause that error? Is it because I am using Unicode?
Here are some samples of my code:
NetworkStream stream = client.GetStream();
StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);
StreamReader sr = new StreamReader(stream);
//SendFileToServer(@"\\fs1\home\manuel.boehmler\contacts.rar", client);
AnswerToServer(FTPCommands.ID,null,sw);
AnswerToServer(FTPCommands.Username, txtBUserName.Text, sw);
AnswerToServer(FTPCommands.Password, txtBPassword.Text, sw);
AnswerToServer(FTPCommands.login,null, sw);
AnswerToServer(FTPCommands.MaxMbytes, txtBPassword.Text, sw);
//AnswerToServer(FTPCommands.FileTransfer,"", sw);
string line = null;
while (client.Connected == true)
{
if (!string.IsNullOrEmpty(line = sr.ReadLine()) && line.Length >= 2)
{
RichTxtMessage("Bekommt: " + line, true);
string[] replace = null;
string command = "";
replace = line.Split(' ');
command = replace[0].ToUpperInvariant();
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : null;
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim())).ToUpperInvariant()) //Error occurs here
{
case "ID":
RichTxtMessage(arguments,true);
break;
case "ERROR":
RichTxtMessage(arguments, true);
break;
}
}
public enum FTPCommands
{
Persmission = 0,
ID = 1,
Username = 2,
Password = 3,
UserNameReq = 4,
PasswordReq = 5,
Chat = 6,
MessageRecived = 7,
Error = 8,
Warning = 9
}
EDIT (UPDATE):
When Iam using UTF8 as encoding the console gives me this result back:
As you can see the wired symbols as first char. However it doesnt show me the symboles when Iam using UNICODE (but still not working). I solved the problem, I dont use any Encoding for my FTP-Server
Instead of Convert.ToInt32(replace[0].Trim())) use int.TryParse:
int i = 0;
int.TryParse(replace[0].Trim(), out i)
Edit: If null was in an element, that element cannot be converted as int and it will throw error
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : null;
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim())).ToUpperInvariant()) //Error occurs here
Assign some int instead of null, or check it is not null or empty before converting to int
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : "-1";
// OR do this
int opt = 0;
if (!string.IsNullOrEmpty(replace[0].Trim())
opt = int.TryParse(replace[0].Trim(), out opt);
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(opt)).ToUpperInvariant()) //Error occurs here
I am not sure why you are bothering to convert the integer to a string. You could just cast to the enum and use switch with just enum values, eg
int id = -1;
if (int.TryParse (replace[0].Trim (), id))
{
switch ((FTPCommands)id)
{
case FTPCommands.ID:
RichTxtMessage(arguments,true);
break;
case FTPCommands.Error:
RichTxtMessage(arguments, true);
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.