Socket can't receive back message.

EggPlant Hosirov 1 Reputation point
2021-04-20T14:49:02.113+00:00

First of all, I'm really new to sockets, so if i misunderstand something, i'm really sorry.
I'm trying to create a simple game using windows.forms. The idea is simple: You and your friend connect to each other and connect, who's cps (clicks per second) is higher. I named my game "ClickerVS". I created 2 usercontrols for menu 89605-%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5.png and for game 89612-%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5.png for connection i decided to use quite a stupid system. First of all, the connector send the "Here!" message to the guy, who is host.

private void button1_Click(object sender, EventArgs e)
{
port = int.Parse(textBox2.Text);

        IP = IPAddress.Parse(textBox1.Text);  

        Game game = new Game();  
        game.Dock = DockStyle.Fill;  
        this.Controls.Add(game);  
        Controls.Remove(this);  
        game.BringToFront();  
        try  
        {  
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);  

            string message = "Here!";  

            byte[] data = Encoding.Unicode.GetBytes(message);  
            EndPoint endPoint = new IPEndPoint(IP, port);  
            sock.SendTo(data, endPoint);  
        }  
        catch (Exception exc)  
        {  
            Console.WriteLine(exc.ToString());  
        }  
        finally  
        {  
            Close();  
        }  

        thread = new Thread(Game.Multiplayer);  
        thread.Start();  

        sock.Close();  
    }  

then, when host gets the message, he sends "Yeah!" message back

while (!gameStarted && isHost)
{
try
{
localIp = new IPEndPoint(IPAddress.Any, Menu.port);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.Bind(localIp);

Console.WriteLine("Waiting For Players...");
StringBuilder builder = new StringBuilder();
int bytes = 0;
byte[] data = new byte[256 * 10000];

endPoint = new IPEndPoint(IPAddress.Any, 0);
do
{
bytes = sock.ReceiveFrom(data, ref endPoint);
builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
}
while (sock.Available > 0);

if (builder.ToString() == "Here!")
{
Console.WriteLine("Game Started!");
try
{
string backMessage = "Yeah!";

byte[] backData = Encoding.ASCII.GetBytes(backMessage);
Console.WriteLine(backData.Length);
var send = sock.SendTo(backData, endPoint);
Console.WriteLine(send);

gameStarted = true;
sock.Close();
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}

and connector tries to catch it.

while(!isHost && !gameStarted)
{
try
{
localIp = new IPEndPoint(IPAddress.Any, Menu.port);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.Bind(localIp);

StringBuilder builder = new StringBuilder();
int bytes = 0;
byte[] data = new byte[256];

endPoint = new IPEndPoint(IPAddress.Any, 0);

do
{
Console.WriteLine("Waiting For Answer...");
bytes = sock.ReceiveFrom(data, ref endPoint);
Console.WriteLine(bytes.ToString());
builder.Append(Encoding.ASCII.GetString(data, 0, bytes));
Console.WriteLine(builder.ToString());
}
while (sock.Available > 0);

if (builder.ToString() == "Yeah!")
{
Console.WriteLine("Game Started!");
sock.Close();
gameStarted = true;
}
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}
And this part doesn't work. I have no idea why it doesn't work and wasn't able to find the answer anywhere. I hope, you guys help me.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,839 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,310 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2021-04-21T02:04:38.647+00:00

    Your button click handler need to call sock.ReceiveFrom() after sock.sendTo() in order to read replies from the server side. In fact this is usually done in form of while loop.

    0 comments No comments