JAVA Servr Soket problem...PLZ Help ME

HI

i have a server port whch can support mulptiple connection. im running this as a Java HotSpotserver .im getting problm after some,

im running this server on 5001 port ,it will accept many connection and running propel. the clients are applets.

these are the problems i am getting

1..From the new applet connection trying to connect to thisits giving connection refused error

2. .then i tried netstat i am getting as that port is listening and aslo so many conection already established also.

3..when try from the telnet i am not able to connect that server from globally and also from the local machine.

4..then i changed the port of the server then its fine .but very next day i am getting same problem(connection refused for new port)

the machine the server running is windows 2003 server and apache tomcat also running on that without any problem.I am not sure that it is java programming problem or firewall problem

plz help me.........

[1006 byte] By [jobina] at [2007-11-27 10:01:48]
# 1

perhaps pasting some code might help further answer some questions, I just wrote a similar multi threaded Server which is working fine.

Many clients can connect, and send events and it seems to work properly but I had problems like yours at the start of my project.

Show where your starting the new thread each time a client connects to a socket and also show how your accepting these connections once you do spawn a new thread in the run() method if you have one.

null

lokiea at 2007-7-13 0:35:36 > top of Java-index,Core,Core APIs...
# 2

Hi

i will paste the code in the run method of my thread

public voidrun()

{

while( true )

{

try

{

if(oServerSocket.isClosed())

{

SharedResources.WriteToLog( "Server port closed and reopening");

int iServerPort=0;

try

{

iServerPort =Integer.parseInt( oBundle.getString( "QuoteServerPort" ).trim() );

oServerSocket=new ServerSocket( iServerPort );

}// try

catch( IOException e )

{

SharedResources.WriteToLog( "Error in reopen part " + e.toString() );

}

}

SharedResources.WriteToLog( "start accepting a client");

Socket oClntsocket=oServerSocket.accept();

AcceptClient( oClntsocket);

SharedResources.WriteToLog( " accepting a client over "+oClntsocket.getInetAddress());

}

catch(ClosedChannelException e )

{

//System.out.println(e);

SharedResources.WriteToLog( "Run ( AcceptClient )>ClosedChannelException " + e.toString() );

}

catch( SocketException e )

{

//System.out.println(e);

SharedResources.WriteToLog( "Run ( AcceptClient )IOException " + e.toString() );

}

catch( IOException e )

{

//System.out.println(e);

SharedResources.WriteToLog( "Run ( AcceptClient )IOException " + e.toString() );

}

catch( Exception e )

{

//System.out.println(e);

SharedResources.WriteToLog( "Run ( AcceptClient ) " + e.toString() );

}// catch()

}// while( true ) loop

}// method run()

and that accept method contains

void AcceptClient(Socket oClientSocket)

throws Exception

{

try

{

boolean bConnectionRequest=false;

try

{

bConnectionRequest=false;

// bConnectionRequest = CheckForRequestRouterConnection(oClientSocket);

}

catch(Exception e)

{

SharedResources.WriteToLog("accept Client method--Router check"+e.toString());

bConnectionRequest=false;

}

if(bConnectionRequest)

{

try

{

oClientSocket.close();

connectToRequestRouter();

oMainScreen.append("Connected \n");

oStream.sendRequest(oClientThreads.size());

}

catch(Exception exp)

{

}

}

else

{

CServerRequestoServerRq;

ObjectInputStreamoInStream=new ObjectInputStream( oClientSocket.getInputStream() );

ObjectOutputStream oOutStream =new ObjectOutputStream( oClientSocket.getOutputStream() );

CUserStreamInfooUserStream =new CUserStreamInfo();

oUserStream.oInStream=oInStream;

oUserStream.oOutStream=oOutStream;

oUserStream.oClntSocket=oClientSocket;

try

{

oServerRq=( CServerRequest ) oInStream.readObject();

}

catch(Exception e)

{

ReleaseResources( oUserStream );

return;

}

oUserStream.oAccountId=oServerRq.oAccountId;

ClientNotifyThread oClientNotifyThread = new ClientNotifyThread(oServerRq.oAccountId, oUserStream.oOutStream, oServerRq.oAccountId );

oClientThreadList.put(oServerRq.oAccountId, oClientNotifyThread);

String[] sSocketProp = new String[4];

sSocketProp[0] = (oClientSocket.getInetAddress()+"").substring(1);

sSocketProp[1] = oClientSocket.getPort()+"" ;

sSocketProp[2] = oClientSocket.getInetAddress().getHostName();

sSocketProp[3] = ""+oClientSocket.isConnected();

try{

SharedResources.WriteToLog("AccID::"+oUserStream.oAccountId+"--IP::"+sSocketProp[0]+sSocketProp[1]+"--HOSTNAME::"+sSocketProp[2]+"--STATUS-->"+sSocketProp[3]);

}

catch(Exception e)

{

//System.out.println(e);

}

oDisplayTable.put(oServerRq.oAccountId, sSocketProp);

if( SanityCheck(oServerRq,

oUserStream) )

{

switch( oServerRq.iReqType )

{

case NESTconstants.QUOTES:

oMainScreen.append(oServerRq.oAccountId + " : connected" + "\n");

oUserStreamHash.put( oServerRq.oAccountId,

oUserStream );

oClientThreads.put( oServerRq.oAccountId,

new ClientThread( this,

oUserStream,

oServerRq.iDDInteractive,

oServerRq.sPortfolio));

UpdateTable();

boolean bSubscribed =false;

OMMisc.OMlog( oUserStream.oAccountId + " : Got Connected from "+oClientSocket.getInetAddress()+" @ "+new Date());

oUserStream.oDDInteractive=sDDInteractive;

oOmneLink.GetPortfolioNames(oServerRq.oAccountId);

oOmneLink.SubscribeForLogMsgs( oServerRq.oAccountId ,

oUserStream.oDDInteractive );

if( bGeneralDataFlag )

{

bGeneralDataFlag =false;

oOmneLink.SubscribeForIndex(); // will work for both index and exchng time

}// if( bSensexFlag )

oOmneLink.GetBSELastIndex();

oOmneLink.GetNSELastIndex();

oMainScreen.append(oServerRq.oAccountId + "Connected @ "+new Date()+ "\n");

try

{

oStream.sendRequest(oClientThreads.size());

}

catch(Exception e)

{

}

}// switch( iReqType )

}// if( SanityCheck() )

}

}// try

catch( Exception e )

{

SharedResources.WriteToLog("Accept clnt method2"+e.toString());

//e.printStackTrace();

}// catch()

}// method AcceptClient()

plz check out and tell me.

jobina at 2007-7-13 0:35:36 > top of Java-index,Core,Core APIs...
# 3

Difficult to tell much without proper formatting but this looks like a real mess. Have a good look at the networking tutorial. Some detailed comments:

> if(oServerSocket.isClosed())

Are you closing the server socket yourself? If so, why? If not, what is this code for?

> catch(ClosedChannelException e )

Who throws this exception? Nobody that I can see.

> ObjectInputStreamoInStream=new

> ObjectInputStream( oClientSocket.getInputStream());

>

> ObjectOutputStream oOutStream =new

> ObjectOutputStream( oClientSocket.getOutputStream());

Create the ObjectOutputStream first.

> ClientNotifyThread oClientNotifyThread =

> new ClientNotifyThread(oServerRq.oAccountId,

> oUserStream.oOutStream, oServerRq.oAccountId );

You should do that before reading the socket. The new thread should do all the I/O on the socket. Otherwise the accept loop can get stuck.

> sSocketProp[3] =

> ""+oClientSocket.isConnected();

The accepted socket will always return true for isConnected(). It was connected when it was accepted.

>oClientThreads.put( oServerRq.oAccountId,

> new

> ClientThread( this,

What is this second thread for?

> try

> oStream.sendRequest(oClientThreads.size());

The accept loop shouldn't do any I/O, or anything else on the socket that could stall, e.g. interrogating its InetAddresses.

ejpa at 2007-7-13 0:35:36 > top of Java-index,Core,Core APIs...