I am using Lidgren, i can connect to server and server can reply to client but when i try to send a message with the connect the server doesn’t receive anything
From client
NetPeerConfiguration config = new NetPeerConfiguration(“name”);
client = new NetClient(config);
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.Login);
outmsg.Write(“username”);
outmsg.Write(“password”);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 0);
client.Start();
client.Connect(“127.0.0.1”, 14248, outmsg);
From server
if ((inc = server.ReadMessage()) != null)
{
switch(inc.MessageType)
{
case NetIncomingMessageType.Data:
switch ((PacketTypes)inc.ReadByte())
{
case PacketTypes.Login:
{
string usuario = inc.ReadString();
string pass = inc.ReadString();
}
I used RemoteHailMessage as you said and i receive an error “Object reference not set to an instance of an object” in the first ReadString line, seems the remotehailmessage is null?
Code in client is
NetPeerConfiguration config = new NetPeerConfiguration("name");
client = new NetClient(config);
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write("username");
outmsg.Write("password");
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 0);
client.Start();
client.Connect("127.0.0.1", 14248, outmsg);
You probably have some error, it does work.
The client creates the hail, sends it with the connect method.
The server has to have the message type enabled:
You can’t have 2 sockets listening on the same port. When you open the second socket, it will die as the port is in use.
The policy server normaly listens on 843 and another port for the gameserver.
On the client you also need to prefetch the policy before any connection is made which you are not doing as far as i can see.
You mean i would have to approve the connection first and then send a message to server with user and password ? i would like to approve or deny connection based on user and password
hmm odd, I think I explained it fairly well.
Client sends credentials when he connect’s.
Server catches it in ConnectionApproval and sends back to the client in the approval ( or deny )
Client catches what server sends back.
case NetIncomingMessageType.ConnectionApproval:
NetOutgoingMessage hail = server.CreateMessage();
string username = im.ReadString(); // read from incoming message
string password = im.ReadString(); // read from incoming message
Console.WriteLine(username + " " + password);
hail.WriteVariableInt32(1);
im.SenderConnection.Approve(hail); // send response to client
break;
You can validate the credentials here and send a Deny(string)