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.
Socket can't receive back message.
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 and for game 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.