Reputation: 23
I am currently working on a Chat Client(Windows Store apps - C#) and I'm currently stuck at the registration part. Before I send the relevant data like username, password, email etc. to the server to make a new user, I call the function "CheckSignUp(...)" which checks first if the username already exists. It first sends the keyword /F011/ and then the username, then I should the receive the keyword OK or NOK(username already exists). Here's the code:
public async Task<string> CheckSignUp(string username)
{
writer.WriteString("/F011/" + "\n");
writer.WriteString(username + "\n");
await writer.StoreAsync();
reader.InputStreamOptions = InputStreamOptions.Partial;
string result = reader.ReadString(await reader.LoadAsync(3));
return result;
}
This function is called when I press the "Sign Up" button. The problem is that I get the error only after the 2nd time I've pressed the "Sign Up" button. Here's the relevant code for the Button_click event:
private async void bnSignUp_Click(object sender, RoutedEventArgs e)
{
...
string ValidUser = await App.ChatConnection.CheckSignUp(tbUserName.Text);
if (ValidUser == "NOK")
{
FailText.Children.Add(new InfoTextBox("Error! Username already exists."));
tbUserName.Text = "";
}
....
}
The creation of the reader
and writer
objects occurs when connection to the server is made:
socket = new StreamSocket();
HostName hostname = new HostName(host);
await socket.ConnectAsync(hostname, port);
writer = new DataWriter(socket.OutputStream);
reader = new DataReader(socket.InputStream);
EDIT: I also have the same problem when I try to log in(the Login() function works similar like the CheckSignUp one):
public async Task<string> Login(string username, string password)
{
writer.WriteString("/F050/" + "\n");
writer.WriteString(GetHash(password) + "\n");
writer.WriteString("\n");
writer.WriteString(username + "\n");
await writer.StoreAsync();
reader.InputStreamOptions = InputStreamOptions.Partial;
return reader.ReadString(await reader.LoadAsync(3));
}
Upvotes: 1
Views: 258
Reputation: 457207
I recommend that you first do your best to not use TCP/IP. Use WebAPI, SignalR, etc.
If you do need to use TCP/IP, then you'll need to implement message framing, as I describe on my blog.
Upvotes: 2
Reputation: 7622
You may have to flush the writer so that your data is actually sent over socket and not buffered.
public async Task<string> CheckSignUp(string username)
{
writer.WriteString("/F011/" + "\n");
writer.WriteString(username + "\n");
await writer.StoreAsync();
await writer.FlushAsync();
// ..rest of code...
}
Upvotes: 0