Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my sockets getInputStream return null?

Tags:

java

sockets

Well I'm trying to make an application that will connect to a game's login database and connect to the chat server but whenever I try to use the Socket.getInputStream( ) function it throws an exception and doesn't give me anything... I'm not sure why it does this so I was wondering if any of you could help..

I Connect like this:

public boolean Connect( String RemoteHostIP, int RemotePort )
{
    try
    {
        if( m_Socket != null )
        {
            m_Socket.close( );
            m_Socket        = null;
        }

        m_Socket            = new Socket( );
        m_Host              = RemoteHostIP;
        m_Port              = RemotePort;

        m_Socket.connect( new InetSocketAddress( m_Host, m_Port ) );

        System.out.println( "   ~ Connected to: " + m_Socket.getInetAddress( ).getHostAddress( ) + ":" + m_Socket.getPort( ) );
    }
    catch( SocketException Ex )
    {
        m_Socket            = null;
        System.err.println( "[SocketHelper::Connect SEN] " + Ex.getLocalizedMessage( ) );
        return false;
    }
    catch( IOException Ex )
    {
        m_Socket            = null;
        System.err.println( "[SocketHelper::Connect IOE] " + Ex.getLocalizedMessage( ) );
        return false;
    }

    return true;
}

and then bind to my Input and Output like this:

public boolean Bind( )
{
    try
    {
        if( !m_Socket.isConnected( ) || m_Socket.isClosed( ) || !m_Socket.isBound( ) )
            return false;

        if( m_Socket.getOutputStream( ) != null )
        {
            m_Output            = new ObjectOutputStream( m_Socket.getOutputStream( ) );
            m_Output.flush( );
        }
        if( m_Socket.getInputStream( ) != null )
        {
            m_Input             = new ObjectInputStream( m_Socket.getInputStream( ) );
        }
    }
    catch( SocketException Ex )
    {
        System.err.println( "[SocketHelper::Bind SEN] " + Ex.getLocalizedMessage( ) + " (" + m_Output + " | " + m_Input + ")" );
        return false;
    }
    catch( IOException Ex )
    {
        System.err.println( "[SocketHelper::Bind IOE] " + Ex.getLocalizedMessage( ) + " (" + m_Output + " | " + m_Input + ")" );
        return false;
    }

    return true;
}

and this is my output

    ~ Connected to: 96.127.149.202:11031
Failed to Connect: Could not bind to socket
[SocketHelper::Bind IOE] null (java.io.ObjectOutputStream@1202f4d | null)
like image 246
The Char Avatar asked Mar 17 '26 22:03

The Char


1 Answers

Neither of those methods returns null at any time, and there is no evidence anywhere here to suggest otherwise. The only things that are null here are the exception's localised message, and m_input because you set it to null yourself, or because it was initially null and never got set at all. Try doing it the easy way and printing the stack trace. When you do so you will find that the exception is being thrown by new ObjectInputStream(). All this is obvious but it is being disguised by your poor treatment of exceptions.

And while you're at it please rename your method. Bind already has a meaning in socket programming, and this isn't it.

like image 54
user207421 Avatar answered Mar 19 '26 16:03

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!