C
C#•5mo ago
vietyka2020

Need help with my server-client application

Hello, I am creating a server - client programm using Tcp. However, I am encoutering a problem where I gets an error called: System.IO.IOException: 'Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..' from the server while loop each time after I have received a message notifying whether I had entered a correct username and a correct password. The following is the while loop i am talking about
while ((count = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, count);
string[] parts = data.Split(',');

if (parts.Length >= 3)
{
string examCode = parts[0];
string username = parts[1];
int password = int.Parse(parts[2]);

resultExamCode = SearchForExamCode(examCode);
resultUsername = SearchForUsername(username);

if (resultExamCode.Equals("Not found") ||
resultExamCode.Equals("Not found") ||
password != 12345678)
{
result = "There is an incorrect field from your inputted information, re-enter again!";

} else
{
result = "Success";
}
}

Byte[] msg = System.Text.Encoding.ASCII.GetBytes(result);
stream.Write(msg, 0, msg.Length);
}
while ((count = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, count);
string[] parts = data.Split(',');

if (parts.Length >= 3)
{
string examCode = parts[0];
string username = parts[1];
int password = int.Parse(parts[2]);

resultExamCode = SearchForExamCode(examCode);
resultUsername = SearchForUsername(username);

if (resultExamCode.Equals("Not found") ||
resultExamCode.Equals("Not found") ||
password != 12345678)
{
result = "There is an incorrect field from your inputted information, re-enter again!";

} else
{
result = "Success";
}
}

Byte[] msg = System.Text.Encoding.ASCII.GetBytes(result);
stream.Write(msg, 0, msg.Length);
}
5 Replies
not guilty
not guilty•5mo ago
it could be that the issue is in client part
leowest
leowest•5mo ago
you're checking for resultExamCode.Equals("Not found") twice aside from that we would need more $code to see what else is going on
MODiX
MODiX•5mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
leowest
leowest•5mo ago
it could be the size of the message being split in multiple packets, it could be your logic on the other side ending and closing the socket all I can tell is that one side or the other terminated the socket
cap5lut
cap5lut•5mo ago
are u sure u got the whole message before actually processing it? with stream based protocols u have to account for not getting the whole message at once, or even more than just one message. eg if the server sends "hello world" and then "how are you doing?", u could receive it as "hell" and then start processing, or u could receive "hello worldhow are you", etc as we do not know much about the server itself i can only guess. but usually if the server abruptly closes the connection its a protocol error of some kine (eg sending invalid packets), as in the protocol on top of tcp, not tcp itself oh wait u mentioned that u r writing both sides 😒 yeah show us both ends of the connection
Want results from more Discord servers?
Add your server
More Posts