开发者

Cannot connect through socket for client/server program

package server;

import java.net.*;  // for network 
import java.util.*; // for utilities
import java.io.*;   // for streams

import server.ex12ClientThread;


public class ex12Server implements ex12Constants
{

    public static void main(String args[])
    {

int well_known_port     = SERVERPORT; // default port value 
        ServerSocket serverSock = null;

        ex12ClientThread thread = null;

        try  { // to get a port number
            if ( args.length > 0 )
            {
                well_known_port = Integer.parseInt(args[0]);
                        //initialises port number to connect to
            }

        } catch (NumberFormatException e ) {} // do nothing accept default.

        try
        {


            serverSock = new ServerSocket( well_known_port, 10 ) ;


            // information to log file (ie screen)
            System.out.println("ServerSocket " + serverSock.toString());
            System.out.println("Entering server loop");



            while( true ) // Main Server loop
   开发者_C百科         {
                Socket clientSocket = serverSock.accept();

                thread = new ex12ClientThread(clientSocket);
                                thread.setDaemon(true); 
                thread.start();



                        }   

                } catch( Exception e )
{ 
            System.err.println( "Socket Error!!!." ) ; 
            System.exit(1) ; 
        }
        finally
                {
                    try
                    {
                        serverSock.close();
                    }
                    catch (IOException e) {}
                }
    }
}


It might help, at this point at least, if you were to printStackTrace on some of those exceptions you're discarding. I know on my personal development box I'm usually running at least 1 app server on a port like 8080, and I've got either Apache or IIS usually running on port 80, so if something else is already bound on the port so an exception will be thrown and caught by that first catch(exception e). You might be exiting the app before you close executes as well, as I think, the Javadoc says the call does not return, so while that probably isn't a problem, since you're tossing threads out you might have your own threads out there blocking other connections, though I really doubt that, it probably is one of those "behavior determined by the implementer" types of things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜