Can't send messages to server with Lidgren

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();
}

                switch (im.MessageType)
                {
                    case NetIncomingMessageType.ConnectionApproval:
                        NetIncomingMessage hail = im.SenderConnection.RemoteHailMessage;
                        Console.WriteLine(hail.ReadString());
                        Console.WriteLine(hail.ReadString());
                        im.SenderConnection.Approve();
                        break;

Please use code tags.
You also need to enable the ConnectionApproval type messages on the server.

I already solved the problem, it had nothing to do with lidgren but with a break i didnt realize it was the problem.

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);

As stated before

It was enabled but doesnt work, the approval works but not the remoteHail

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:

config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

The server calls the remote hail:

NetIncomingMessage hail = im.SenderConnection.RemoteHailMessage;
                        Console.WriteLine("receivedHail username: " + hail.ReadString());
                        Console.WriteLine("receivedHail password: " + hail.ReadString());
                        NetOutgoingMessage returnHail = server.CreateMessage();
                        returnHail.Write(1); // return userid
                        im.SenderConnection.Approve(returnHail);

The client then catches the hail in the OnConnected method as an incoming message.

This is wrong :

NetOutgoingMessage outmsg = new NetOutgoingMessage();

It should be :

NetOutgoingMessage outmsg = client.CreateMessage();

I tried with CreteMessage first but it didnt work and i tried new NetOutgoingMessage, it doesnt work with either

post your code so we can have a look

Client

	    NetPeerConfiguration config = new NetPeerConfiguration("nombre");
        client = new NetClient(config);
        client.Start();
        NetOutgoingMessage outmsg = client.CreateMessage();
        outmsg.Write("username");
        outmsg.Write("password");
		client.Connect("127.0.0.1", 843, outmsg);

Server

		if ((inc = server.ReadMessage()) != null)
		{
	        switch(inc.MessageType)
	        {
	            case NetIncomingMessageType.ConnectionApproval:

                            NetIncomingMessage hail = inc.SenderConnection.RemoteHailMessage;
                            string usuario = hail.ReadString();
                            string pass = hail.ReadString();

	                break;

And the server config, where you enable the connection approval messages ?

Here some code about the policy server and configuration

		const string AllPolicy =
		@"<?xml version='1.0'?>
		<cross-domain-policy>
		    <allow-access-from domain=""*"" to-ports=""*"" />
		</cross-domain-policy>";
        SocketPolicyServer policyServer = new SocketPolicyServer(AllPolicy);
        int ret = policyServer.Start();

        config = new NetPeerConfiguration("nombre");
        config.Port = 843;
        config.MaximumConnections = 1000;
	  config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
	  config.EnableMessageType(NetIncomingMessageType.Data);
        server = new NetServer(config);
        server.Start();

This config can never work, port 843 is the socket of the policy server.

I used also other ports before like 14248 and didnt work. But why cant use the same port?

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.

Ah I see now what the issue is.
On the client send the connect like this:

            NetOutgoingMessage hail = client.CreateMessage();
            hail.Write(username);
            hail.Write(password);
            client.Connect(host, port, hail);

On the server catch them like this:

case NetIncomingMessageType.ConnectionApproval:
                        NetOutgoingMessage hail = server.CreateMessage();
                        string username = im.ReadString();
                        string password = im.ReadString();
                        Console.WriteLine(username + " " + password);
                        hail.WriteVariableInt32(1);
                        im.SenderConnection.Approve(hail);
                        break;

And then on the client again:

                            case NetConnectionStatus.Connected:
                                NetIncomingMessage hail = im.SenderConnection.RemoteHailMessage;
                                Console.WriteLine(hail.ReadVariableInt32());
                                break;

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)