I can't seem to find a function to check to see if a connection was success

Hello everyone. I'm running into 2 problems that I can't seem to find functions for after reading the java API on socket and serverSocket.
If a client closed a connection on my server, like if someone telnets into my server, then closes the console, i get the following:
Event from: localhost-> null
Exception in thread "Thread-1" java.lang.NullPointerException
     at server.MultiThreadServer.run(MultiThreadServer.java:62)
     at java.lang.Thread.run(Thread.java:803)This is the check i do to avoid getting this, but it doesn't seem to work:
//close a connection on this condition.
              if(csocket.getRemoteSocketAddress() == null || input.equals("bye"))
                   System.out.println("Connection closed by client.");
                   break;
              }If I attempt to run a server socket on a socket that is already being used, I'd like to print out a statement saying its being used and do something about it, like let the user enter another port number. Right now I have:
public class MainTest {
       public static void main(String args[]) throws Exception
            //get console input
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));  
            System.out.println("Enter port to run server on: ");
            String input = stdin.readLine();
            int servPort = Integer.parseInt(input);
            try
            ServerSocket ssock = new ServerSocket(servPort);
            System.out.println("Listening on : " + ssock);
              while (true) {
                //waiting for client to connect to server socket
                Socket sock = ssock.accept();
                System.out.println(sock + " connected.");
                //get host information
                InetAddress clientIa = sock.getInetAddress( );
                String clientName = clientIa.getHostName();
                String clientIp = clientIa.getHostAddress();
                int localPort = sock.getLocalPort();
                System.out.println("hostname: "+clientName  +
                          '\n' + "Ip address: " + clientIp
                          +":"+ localPort + "\n\n");
                new Thread(new MultiThreadServer(sock)).start();
            catch(IOException e)
                 e.printStackTrace();
}Me catching the IOException is too broad....if anything goes wrong it will spit out the error and stop program execution as it should.
I tried
if(ssock.isClosed()) { System.out.println("Socket is being used, try another");but it doesn't work.
Also inside my server program I'm connecting to another server and if that other server isn't running I'd like to print out an error message stating that.
Right now it will just say: Connection Refused if its not running.
here's that code:
public class MultiThreadServer implements Runnable {
     Socket csocket;
  MultiThreadServer(Socket csocket) {
    this.csocket = csocket;
  public void run() {
    try {
      //setting up channel to recieve events from the omnibus server
         BufferedReader in= new BufferedReader(
                    new InputStreamReader(
                    csocket.getInputStream()));
         //This socket will be used to communicate with the reciever
         //we will need a new socket each time because this is a multi-threaded
         //server thus, the (outputServ) will need to be
         //multi threaded to handle all the output.
         Socket outputServ = new Socket("localhost",1234);
         if(outputServ.getLocalSocketAddress() == null)
              System.out.println("OutputServer isn't running...");
         //Setting up channel to send data to outputserv
          PrintWriter out
               = new PrintWriter(
               new OutputStreamWriter(
               outputServ.getOutputStream()));
         while(true)
              //accepting events from omnibus server and storing them
              //in a string for later processing.
              String input = in.readLine();
              //accepting and printing out events from omnibus server
              //also printing out connected client information
              System.out.println("Event from: " + csocket.getInetAddress().getHostName()
                        +"-> "+ input +"\n");
              out.println(input);
              out.flush();
              //close a connection on this condition.
              if(csocket.getRemoteSocketAddress() == null || input.equals("bye"))
                   System.out.println("Connection closed by client.");
                   break;
         //cleaning up
      in.close();
      out.close();
      outputServ.close();
      csocket.close();
    } catch (IOException e) {
      System.out.println(e);
      e.printStackTrace();
}Both those conditions don't seem to work, if a client closes a connection it doesn't respond and if the server isn't running on port 1234, i get that connection refused.
Any help would be great!

Thanks for the help guys!
adding -1 to the stream worked perfect:
i have it like this:
     String input;
     //accepting events from omnibus server and storing them
     //in a string for later processing.
     while ((input = in.readLine()) != null)  
              //accepting and printing out events from omnibus server
              //also printing out connected client information
              System.out.println("Event from: " + csocket.getInetAddress().getHostName()
                        +"-> "+ input +"\n");
              out.flush();
              out.println(input);
              out.flush();
              //close a connection on this condition.
              if(in.read() == -1)
                   System.out.println("Connection closed by client.");
                   break;
    //cleaning up
      in.close();
      out.close();
      outputServ.close();
      csocket.close();
    } I had one last question, when I catch a socketExpection, such as, if the user enters a port that is already in use, I would like to give them the chance to enter another port before stopping the program.
I'm not sure how I can do this with exceptions though, like i have:
  //setting up sockets
            ServerSocket ssock = null;
            Socket sock = null;
            try
            //setting up server to run on servPort
           ssock = new ServerSocket(servPort);
       catch (SocketException e )
                 System.out.println ("Socket error: " + e);
            }now inside that catch block, is there a way for me to allow the user to enter another socket and try again? or am I approaching this wrong?
while(true)
if the port is already been used
allow the user to enter in another port number
else
break;
}I'm confused on how this will work though becuase:
once it hits this line:
ssock = new ServerSocket(servPort);the exception is thrown and it jumps down to the catch block,
now inside that catch block how would I let the user enter in another port number and try this line of code again?
ssock = new ServerSocket(servPort); //this time with the new servPort number?
Thanks!

Similar Messages

  • After updating to iOS 5, I can't seem to find my digital zoom on my iPhone 4.

    Hi, I just recently updated my iPhone 4 16 GB GSM to the newest operating system - iOS 5. The only problem I have encountered is when i'm in camera mode (rear-facing camera) and I tap the screen once to try and zoom in, the digital zoom bar doesn't show up. Is there some kind of place in the settings where I can turn this feature back on again? I have tried restarting my phone also, and just can't seem to find it. Thanks in advance.

    Thank you! I was about to ask the same question.

  • I made a list in reminders, but now want to delete the title of the list, I can't seem to find this option anywhere??

    I made a list in reminders, but now want to delete the title of the list, I can't seem to find this option anywhere??

    See this article for information on manually managing your iPod's contents.
    http://support.apple.com/kb/ht1535
    B-rock

  • I can't seem to find any books to purchase on Adobe Media Server 5 or even tutorials.

    I can't seem to find any books to purchase on Adobe Media Server 5 or even tutorials, is there a reason for this? Is AMS gone by the wayside?

    I can't find my photos that I've just downloaded.
    Look in Last Import
      I can't find my photo information (i.e. shutter speed, aperture, etc).
    Click on the circled "i" in edit mode
    There is no histogram.  I can only find about 3 sliders to adjust color, tint, etc.
    In edit mode click on "adjust" then "add" and check the options you want to see
    The crop function is difficult to use.
    Ok - that is a personal opinion - I like it better
    I cannot geotag my photos any longer.
    That is correct - hopefully it will be added with an update - for now you have to do it prior to Import - HoudahGeo hopes to be able to do it directly to the Photos library later also and is a good solution for Geotagging
    Right now, I feel like I'm being forced to use Adobe Photoshop and Lightroom to get the tools I need to edit my photos.
    Forced? Someone is there with a gun? You are not being Forced to do anything - you can do what ever you want including continue to use iPhoto - or even post invalid winning complains instead of acting to resolve your concerns - your choice - do what you want
    LN

  • After updating to the latest version of Firefox on my Mac there is no progress bar for the page load. I really miss this feature and can't seem to find a way to obtain it.

    The page load progress bar that was on the lower right of the window is no longer there. After updating to the latest version of Firefox on my Mac there is no progress bar for the page load. I really miss this feature and can't seem to find a way to obtain it. The tab has a circular progress wheel but this is useless for determining a stuck or slow loading page.
    PLEASE NOTE: I am typing this in from a Windows based work computer but am asking about my Apple MacBook Pro that i use at home.

    Firefox 4 saves the previous session automatically, so there is no longer need for the dialog asking if you want to save the current session.<br />
    You can use "Firefox > History > Restore Previous Session" to get the previous session at any time.<br />
    There is also a "Restore Previous Session" button on the default <b>about:home</b> Home page.<br />
    Another possibility is to use:
    * [http://kb.mozillazine.org/Menu_differences Firefox (Tools) > Options] > General > Startup: "When Firefox Starts": "Show my windows and tabs from last time"

  • I created a new Apple ID for iCloud on my iPhone, but I can't seem to find a way to "sign out" or "log out" of that Apple ID (for iCloud) to sign in with another Apple ID. The only account login I could change was the Store. How do I "log out"

    I created a new Apple ID for iCloud on my iPhone, but I can't seem to find a way to "sign out" or "log out" of that Apple ID (for iCloud) to sign in with another Apple ID. The only account login I could change was the Store.
    How do I "log out" or "sign out" of iCloud on my iPhone? There is an option to delete the account, but I just want to log off. I want to keep the account name for future use.

    @fernandamagalhaes
    It looks like the article below has the information you are looking for.
    iCloud: Change iCloud feature settings
    http://support.apple.com/kb/PH2613
    Turn off iCloud completely
    Depending on whether you want to stop using iCloud on all or only some devices, do one or more of the following:
    On your iOS device’s Home screen, go to Settings > iCloud, then at the bottom of the screen, tap Delete Account.
    Note:   If you delete your iCloud account, iCloud will no longer back up your iOS data. You can still back up your device in iTunes (for more information, open iTunes and choose iTunes > Help). 
    On your Mac, open iCloud preferences, then click Sign Out.
    If your Mac has OS X v10.7.5 and you turn off iCloud, your calendar information and reminders aren’t stored locally in iCal. If you want to retain your calendar and reminder information, you need to back it up before turning off iCloud. For more information, see the Apple Support article iCloud: Calendar & reminder data removed from Calendar and Reminders or iCal when disabling iCloud Calendar.
    On your Windows computer, open the iCloud Control Panel, then click Sign Out.
    Note:   If you turned on automatic download of music, app, or book purchases (in iTunes preferences or in Settings on your iOS device), your iTunes purchases are still downloaded to your devices.
    Set up iCloud on your devices

  • I have an older Power Mac G5 Quad, and my video card just went out. I am trying to find a video card that will work for my Power Mac. I have duel screens but I can't seem to find the appropriate video card

    I have an older Power Mac G5 Quad, and my video card just went out. I am trying to find a video card that will work for my Power Mac. I have duel screens but I can't seem to find the appropriate video card. The serial number on my computer is: RM620603R6W. Apple won't help me because the computer is older than suggested. Please give me the exact video card that will work best for my computer.

    Hi Mike, make sure the card has Mac ROM, or is a PC card with Mac ROMs flashed to it...
    G5 PCIe options are listed here:
    http://www.jcsenterprises.com/Japamacs_Page/Blog/71BBF3EF-9713-4C53-8B80-26771F8 A4087.html

  • I've downloaded CS6 Master Collection but only see The Photoshop icon.  I can't seem to find the other programs, except on "uninstall programs" How do I open and run the others?

    I've downloaded CS6 Master Collection but only see The Photoshop icon.  I can't seem to find the other programs, except on "uninstall programs" How do I open and run the others?

    How about you give us some real information.
    Did you install the software? What operating system?

  • Every time I try to install itunes on my PC I get the following message, and I can't seem to find a solution. Any help out there?? "There is a problem with this Windows Installer package. A program required for this install to complete could not be run."

    Every time I try to install itunes on my PC I get the following message, and I can't seem to find a solution. Any help out there?? "There is a problem with this Windows Installer package. A program required for this install to complete could not be run."

    Try the following user tip:
    "There is a problem with this Windows Installer package ..." error messages when installing iTunes for Windows

  • I'm trying to update iPhoto 7.1.5 to current versions so I can get my photos onto iCloud to access from my iPad.  I can't seem to find an upgrade route that will work?  Any suggestions?

    I'm trying to update iPhoto 7.1.5 on my old iMac (running  10.9.5) to current versions so I can get my photos onto iCloud to access from my iPad.  I can't seem to find an upgrade route that will work?  Any suggestions?

    For Mavericks you need to upgrade to iPhoto 9.5.1.
    You cannot buy this version from the AppStore, since the current version is iPhoto 9.6, and that requires Yosemite. The easiest route for you would be to upgrade to Yosemite. Then you can simply buy the newest version of iPhoto from the App Store and can use iCloud Photo Library (iCloud Photo Library beta FAQ), once it has been release for the Mac as well early next year.
    If you want to stick with Mavericks, you'll have limited iCloud features, only the Photo Streams  (iCloud: iCloud Photo Sharing FAQ), but no iCloud Drive (iCloud Drive FAQ) and iCloud Photo Library.  And to upgrade iPhoto you need to buy an iPhoto '11 installer. It comes as part of iLife '11. You can try to buy a boxed version from Amazon or try to find it at eBay.  Once installed on Mavericks, download the iPhoto '11 updaters from the apple Support Downloads page.

  • The instagram icon on my iTunes says it has been downloaded but I can't seem to find it. Where do I find it?

    The instagram icon on my iTunes says it has been downloaded but I can't seem to find it. Where do I find it?

    Hi Charlene1018,
    You can locate any of your iTunes media including apps in Finder by following this article:
    Where are my iTunes files located?
    http://support.apple.com/kb/ht1391
    Locating the file for an item you see in iTunes
    If you're not sure where some of the content you see in iTunes is stored, iTunes will also show you what it knows about your media in the Get Info window.
    Select an item in iTunes and choose File > Get Info.
    You can right-click on the 'Where' to get the option to Show in Finder (Mac OS X) or Show in Windows Explorer (Windows).
    Cheers!
    - Ari

  • Do you know of an app like a weekly planner/ diary that I can see and edit in my phone and my boyfriend can on his? Been looking on the app store but can't seem to find one! Please help! :)

    Do you know of an app like a weekly planner / diary that I can see and edit in my phone and my boyfriend can on his? Been looking on the app store but can't seem to find one! Please help!

    Welcome to the Apple Community.
    How about calendar, you can share calendars.

  • Can't seem to find oracle native 32-bit cleint for a 64-bit Windows 2008 op

    Hi,
    We have a Windows 2008 server (64-bit) and have installed oracle 64-bit client on there.
    Have a third party application which is usggesting need also a 32-bit oracle native client.
    Had a look on otn but can't seem to find. For 64-bit o/s can only see 64-bit oracle client.
    Any ideas/links much appreciated.
    Thanks

    Hi,
    It seems there is a 32 bit client and a 64 bit client.
    We have a 64 bit operating system. (MS Windows 2008 SERVER).
    Is it correct theses are the only2 versions of the client a 32bit client and a 64 bit client and the operating system is irrelevant.
    i.e. no 32 bit client for 32 bit operating system operating and 32 bit client for 64 bit operating system?
    Thanks

  • I'm running os 10.5.8 and want to upgrade to new mountain lion but need to first upgrade to 10.6.8. I downloaded that but got message saying i needed 10.6 first, which I can't seem to find anywhere. When i try Software Update from the apple menu, it says

    Help! I want to upgrade to the new Mountain Lion OS but first need to upgrade rto 10.6.8. I'm currenty running 10.5.8. When i try the Software update from my desktop apple menu, it says i already have the most redent software. I tried downloading 10.6.8 but when i tried to run it said i first need 10.6, which i can't seem to find anywhere on the Apple site or internet. Any suggestions? Can i somehow not upgrade form 10.5.8 to 10.6 or higher?
    Thanks.

    Get this from Amazon before the prices really get out of control!
    OS X 10.6.3
    http://www.amazon.com/Mac-version-10-6-3-Snow-Leopard/dp/B001AMHWP8/ref=sr_1_1?s =software&ie=UTF8&qid=1343250995&sr=1-1&keywords=OS+X+Snow+Leopard
    OR
    OS X 10.6.0
    http://www.amazon.com/Mac-OS-Snow-Leopard-10-6/dp/B002KG02QO/ref=sr_1_2?s=softwa re&ie=UTF8&qid=1343250995&sr=1-2&keywords=OS+X+Snow+Leopard

  • My hard drive crashed and I am using a different machine. How do I retrieve my personalized Pages 5.2 templates from my backup drive? I have searched at length and can't seem to find them.

    My hard drive crashed and I am using a different machine. How do I retrieve my personalized Pages 5.2 templates from my backup drive? I have searched at length and can't seem to find them.

    login_directory = /Users/yourname
    If the Library folder is not shown in your login directory, then open a Finder window, and press command+J. This will open a Finder View Options panel. On it, there is a selectable entry to Show Library Folder. Once this is selected, the Library folder will appear. Now, follow the instructions in my May 6 post. You will have to right-click on com.apple.iWork.Pages and choose Show Package Contents, before continuing.
    Correction: login_directory/Library/Containers/com.apple.iWork.Pages/Data/Library/Applicatio n Support/User Templates/
    The host software persists in changing the correct Application Support text above to Applicatio n Support, which does not exist.

Maybe you are looking for

  • Financial statement by business area

    hi, Has anyone encountered implementation of running balance sheet by business area and it only balances at company level but not at business area level?  What are the likely causes and rectifications? I noticed we cannot post a reclassifcation entry

  • How do I export a 2-channel stereo from a Soundtrack Pro master mix?

    I'm using Soundtrack Pro to create a 7-track music mix for a live event in a few weeks. The mixing and balancing of audio levels went fine. But when I exported the project to an AIFF file, the resulting file had 6 channels instead of the normal 2, an

  • Data Trigger interfering with parameters??

    Hi, I have my data template set to print the user parameters to XML. However, only a couple of the parameters are making it there. I was wondering if the use of data triggers could be the cause of this... Anyone else had this problem? Thanks, Matt

  • Clearing out previous password history

    Hi everyone! As part of our User maintenance is to clear out old user password histories. Just want to ask if there's a way that i could clear out the old/previous password history of a user? Thank you very much in advance! Regards, Braille

  • Easily duplicate / paste layer in different group folder?

    I design Web sites using Layer Comps so the homepage and innerpage examples are all in one .PSD. This means my Photoshop CC file has TONS of layers and folders that require me to scroll in the Layers Palette. Is there a way to copy/paste or move a la