Results 1 to 3 of 3
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Socket programming - port issues

    Review the code below:

    Java Code:
    int PortNumber = 1500;
    ServerSocket MyService;
        try {
           MyServerice = new ServerSocket(PortNumber);
            }
            catch (IOException e) {
               System.out.println(e);
            }
    I have hard coded the port. If port is already occupied, exception will be thrown. I want to catch that exception and want my program to retry by adding 1 to it until connection is made. How this can be done?

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Place your connection code inside a method, define port as a variable of the class, and when you catch appropriate exception, increase port number and recall yourself.

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

Similar Threads

  1. Serial Port
    By radhika in forum New To Java
    Replies: 5
    Last Post: 11-06-2009, 10:40 AM
  2. Socket programming - accepting files
    By ravian in forum Networking
    Replies: 2
    Last Post: 11-29-2007, 10:40 AM
  3. Programming Socket Question
    By paul in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 07:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •