好吧,我正在尝试创建一个连接到游戏登录数据库并连接到聊天服务器的应用程序,但是每当我尝试使用 Socket.getInputStream() 函数时,它都会抛出异常并且没有给我任何东西......我不确定它为什么会这样,所以我想知道你们中的任何人是否可以提供帮助..
我这样连接:
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;
}
然后像这样绑定到我的输入和输出:
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;
}
这是我的输出
~ Connected to: 96.127.149.202:11031
Failed to Connect: Could not bind to socket
[SocketHelper::Bind IOE] null (java.io.ObjectOutputStream@1202f4d | null)