I was just starting to learn sockets through various Google searches, but I was having trouble figuring out how to use Sockets correctly in C # and I need some help.
I have a test application (Windows Forms) and on another class (which is actually located in it .dll, but it does not matter) I have all the server / client code for my socket code.
Question 1)
In my test application, on the server part, the user can click the “start listening” button, and the server part of the socket application should start listening on the connections at the specified address and port, as far as it is good.
However, the application will be blocked, and I can not do anything until someone connects to the server. What if no one connects? How can I handle this? I can indicate the waiting time for the reception, but then what? Does that rule out what I can do about it? I would like to have some activity in the main application, so that the user knows that the application has not frozen and is waiting for connections. But if the connection does not occur, it should be delayed and close everything.
Perhaps I should use asynchronous calls to the send and receive methods, but they seem confusing, and I could not get it to work, only synchronous work (I will lay out my current code below).
Question 2)
Do I need to close something when the time for sending and receiving calls expires. As you will see in my current code, I have a bunch of closures in the socket, but this is somehow not the case. But it also doesn’t feel good when the time works, and I do not close the socket.
In conclusion of my two questions .... I would like the application not to be blocked, so that the user knows that the server is waiting for a connection (for example, with a small tent animation). If the connection is not established after some time, I want to close everything that should be closed. When the connection is established or if this does not happen after some time, I would like to inform the main application of the result.
Here are some of my code, the rest is similar. The Packet class is a custom class that is a custom data block, it's just a bunch of properties based on enums , now with methods for converting them into bytes and back into properties.
A function that starts listening for connections looks something like this:
public void StartListening(string address, int port) { try { byte[] bufferBytes = new byte[32]; if(address.Equals("0.0.0.0")) { udpSocket.Bind(new IPEndPoint(IPAddress.Any, port)); } else { udpSocket.Bind(new IPEndPoint(IPAddress.Parse(address), port)); } remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); int numBytesReceived = udpSocket.ReceiveFrom(bufferBytes, ref remoteEndPoint); if(numBytesReceived == 0) { udpSocket.Close(); return; } Packet syncPacket = new Packet(bufferBytes); if(syncPacket.PacketType != PacketType.Control) { udpSocket.Close(); return; } } catch { if(udpSocket != null) { udpSocket.Close(); } } }
I am sure that I have a bunch of unnecessary code, but I'm new to this, and I'm not sure what to do, any help correcting my code and solving the above problems is really appreciated.
EDIT:
I probably should have said that my requirements were to use UDP and implement these things on my own at the application level. You can consider this as homework, but I am not marked as such because the code does not matter and will not be part of my class, and my problem (my question) is in “how to encode”, since my socket experience is minimal and this not taught.
However, I have to say that I solved my problem at the moment, I think ... I used streams in a demo application that gave me some problems, now I use it in protocol connections, it makes more sense, and I can easily change my own properties of the protocol class and read them from the demo application.
I set a timeout and threw a SocketException if it reaches a timeout. Whenever such an exception occurs, the socket connection is closed. I'm just talking about making a connection, nothing more. If no exceptions are found, the code is likely to be smooth and the connection will be established.
Please adapt your answers accordingly. Right now, it makes no sense for me to mark any of them as an accepted answer, I hope you understand.