Results 1 to 11 of 11
  1. #1
    Fadel is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default IOException help

    Hey guys I have to make a program for tcp/ip communication. Did everything i needed except I cannot get my buttons working, because I cannot throw Exception in their functions. Could someone help me?

    Java Code:
    		btnGauti.addActionListener(new ActionListener() { 
    			public void actionPerformed(ActionEvent e) { 
    					String serverAddress = txtip.getText();
    					int port = Integer.parseInt(txtport.getText());
    				    Socket s = new Socket(serverAddress, port);
    				    BufferedReader input =
    				        new BufferedReader(new InputStreamReader(s.getInputStream()));
    				    String answer = input.readLine();
    					txtreceive.setText(answer);
    				    System.exit(0);
    				    s.close();
    				    s.shutdownInput(); 
    			}});

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: IOException help

    you can surround the code that may throw the error in a try catch block, and catch the IOException.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: IOException help

    Or completely dump the code: you're trying to make a socket connection in the EDT thread, effectively locking it up for what it's made for.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fadel is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: IOException help

    Quote Originally Posted by JosAH View Post
    Or completely dump the code: you're trying to make a socket connection in the EDT thread, effectively locking it up for what it's made for.

    kind regards,

    Jos
    So then, how should I make a connection without a socket?
    Last edited by Fadel; 05-08-2013 at 08:12 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: IOException help

    Quote Originally Posted by Fadel View Post
    So then, how should I make a connection without a socket?
    You can use a socket connection alright; just don't try to make such a connection from the EDT (i.e. in the actionPerformed( ... ) method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fadel is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: IOException help

    Lesson 1: Socket Communications
    In this lesson he gets a string from other method and uses it in actionPerformed method. How does he do that?

    http://pastebin.com/Mh04iYng
    This is my code. If someone could help me solve this I'll be thankful forever. I tried reading everywhere and I'm quite new to Java, so please, help me ;/ .
    Last edited by Fadel; 05-08-2013 at 10:09 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: IOException help

    And what's that System.exit(0) doing in there?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Fadel is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: IOException help

    Quote Originally Posted by Tolls View Post
    And what's that System.exit(0) doing in there?
    It doesnt change anything

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: IOException help

    Quote Originally Posted by Fadel View Post
    It doesnt change anything
    If that's your definition of 'gracefully shutting down the application', may I suggest:

    Java Code:
    Runtime.getRuntime().exec("shutdown -h now");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: IOException help

    Quote Originally Posted by Fadel View Post
    It doesnt change anything
    It shuts your application down.
    Which makes the following 2 lines utterly pointless.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Fadel is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: IOException help

    I meant that it didnt make the program work, no offense. :) Now I'm trying to put the whole socket connection in the GUI() and the button should change the textField text, but variables get lost.. ;/

Similar Threads

  1. is this IOException problematic?
    By ozzyman in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 06:34 PM
  2. IOException error
    By eeraj in forum AWT / Swing
    Replies: 3
    Last Post: 08-31-2009, 04:05 AM
  3. IOException error
    By eeraj in forum AWT / Swing
    Replies: 0
    Last Post: 08-29-2009, 09:02 AM
  4. [SOLVED] IOException
    By sfe23 in forum New To Java
    Replies: 11
    Last Post: 02-20-2009, 10:36 PM
  5. GUI IOException
    By serfster in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 04:19 AM

Posting Permissions

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