Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help with a C# Asynchronous Socket Program that has multiple sends
    primarykey
    data
    text
    <p>So, I have a board game that uses Asynchronous socket to operate over LAN. The thing is, I have little to no understanding of Asynchronous socket programming, or of threads, but I do my best to try.</p> <p>I based my program off a chat program, so I use that part to send multiple strings.</p> <p>So, here's part of the code for the Client:</p> <pre><code>private void Connect(IAsyncResult iar) { try { Socket client_conn = (Socket)iar.AsyncState; client_conn.EndConnect(iar); g_bmsg = new byte[1024]; check = true; string szData = "@Player " + lblName.Text + " connected."; sendingFunction(szData); g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Receive), g_client_conn); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "GG"); } } private void Send(IAsyncResult iar) { Socket client_conn = (Socket)iar.AsyncState; client_conn.EndSend(iar); } private void Receive(IAsyncResult iar) { if (g_bmsg.Length != 0) { SetLabelText(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length)); check = false; } } private void SetLabelText(string txt) { if (lblBuffer.InvokeRequired) lblBuffer.Invoke(new MethodInvoker(delegate { SetLabelText(txt); })); else { lblBuffer.Text = txt; } if (lblBuffer.Text.StartsWith("@")) { lblStatmsg.Text = lblBuffer.Text.Replace("@", ""); } if (lblBuffer.Text.StartsWith("$")) { lblStatmsg.Text = "Server Settings Received."; lblBuffer.Text = lblBuffer.Text.Replace("$", ""); option_Postn = int.Parse(lblBuffer.Text.Substring(0, 1)); option_First = int.Parse(lblBuffer.Text.Substring(2, 1)); } if (lblBuffer.Text.StartsWith("#")) { MessageBox.Show(lblBuffer.Text); } } </code></pre> <p>And here's part of the code for the Server:</p> <pre><code>private void Accept(IAsyncResult iar) { Socket server_conn = (Socket)iar.AsyncState; g_server_conn = server_conn.EndAccept(iar); g_bmsg = new byte[1024]; check = true; g_server_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Recieve), g_server_conn); } private void Send(IAsyncResult iar) { Socket server_conn = (Socket)iar.AsyncState; server_conn.EndSend(iar); } private void Recieve(IAsyncResult iar) { try { Socket server_conn = (Socket)iar.AsyncState; server_conn.EndReceive(iar); if (g_bmsg.Length != 0) { SetLabelText(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length)); check = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "GG"); } } private void SetLabelText(string txt) { if (lblBuffer.InvokeRequired) lblBuffer.Invoke(new MethodInvoker(delegate { SetLabelText(txt); })); else { lblBuffer.Text = txt; } if (lblBuffer.Text.StartsWith("@")) { lblStatmsg.Text = lblBuffer.Text.Replace("@", ""); } else if (lblBuffer.Text.StartsWith("#")) { MessageBox.Show(lblBuffer.Text); } else if (lblBuffer.Text.StartsWith("%")) { } } </code></pre> <p>Basically, since the game sends more than messages (it can send settings, or game pieces, etc), I ran the sender function everytime I need to send something, and on the other side, the Receiver decodes the string sent based on the first character (@ means the string is a setting, for example).</p> <p>The problem is, after the first time both host and client sent something to one another, they can't seem to send again. No error, no message, no nothing. Just won't send. Is there something wrong with the sendingFunction? Or perhaps the delegate something? I don't know. Some advice would be appreciated, guys. And thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload