Pergunta

I never saw someone commenting this. How should I program when using sockets?

For example, comparing web service and sockets, using web services I can create methods with a clarified name. How can I do this using sockets? What if I want to have "methods" in different classes? How do I organize them?

I am trying to make a game. What if I need to have 300 methods and I need to use sockets? What about this?

udpClient.Connect("localhost", 15000);
Byte[] sendBytes = Encoding.ASCII.GetBytes("MyClass MyMethod(firstParameter");
udpClient.Send(sendBytes, sendBytes.Length);

I need to pass a string to an UDP communication. How can I organize this in the server side? How can I split in classes and methods? Or do I need to put 300 "if" in the server side like this?

if(message.Contains("MyClass MyMethod"))
{
   MyClass.MyMethod();
}
if(message.Contains("MyClass MyMethod2"))
...
if(message.Contains("MyClass MyMethod3"))
...
Foi útil?

Solução

Web services like you're talking about tend to have a few layers between the actual data being received and the method that you've provided. For example:

  1. Client sends message
  2. Server receives message
  3. Server translates message into something usable
  4. Server provides usable message to web service method

In your case, you are wanting to emulate the same kind of functionality with a direct socket connection and no web service framework to do the heavy lifting for you, so you need to provide 2, 3, and 4 yourself.

It would help if you had a standard way of structuring your data so that you can provide information to the server, such as "Which method do you want to call?", and frankly, you can do it however you want. It's your client/server, you could use SOAP, JSON, you could even create your own fixed-length field format, as long as both sides speak in terms of the same data structure.

If you have specific implementation questions about a particular path you are going down, however, you are welcome to ask a different question.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top