User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 429,899 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,308 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 502 | Replies: 12 | Solved
Reply
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Problem: Instant Messenger

  #1  
Jul 17th, 2008
  1. import java.awt.Color;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. import java.io.*;
  7. import java.net.*;
  8.  
  9. class SocketClient extends JFrame
  10. implements ActionListener {
  11.  
  12. JLabel text, clicked;
  13. JButton button;
  14. JPanel panel;
  15. JTextField textField;
  16. Socket socket = null;
  17. PrintWriter out = null;
  18. BufferedReader in = null;
  19.  
  20. SocketClient(){ //Begin Constructor
  21. text = new JLabel("Text to send over socket:");
  22. textField = new JTextField(20);
  23. button = new JButton("Click Me");
  24. button.addActionListener(this);
  25.  
  26. panel = new JPanel();
  27. panel.setLayout(new BorderLayout());
  28. panel.setBackground(Color.white);
  29. getContentPane().add(panel);
  30. panel.add("North", text);
  31. panel.add("Center", textField);
  32. panel.add("South", button);
  33. } //End Constructor
  34.  
  35. public void actionPerformed(ActionEvent event){
  36. Object source = event.getSource();
  37.  
  38. if(source == button){
  39. //Send data over socket
  40. String text = textField.getText();
  41. out.println(text);
  42. textField.setText(new String(""));
  43. //Receive text from server
  44. try{
  45. String line = in.readLine();
  46. System.out.println("Text received :" + line);
  47. } catch (IOException e){
  48. System.out.println("Read failed");
  49. System.exit(1);
  50. }
  51. }
  52. }
  53.  
  54. public void listenSocket(){
  55. //Create socket connection
  56. try{
  57. socket = new Socket("124.243.213.225", 4444);
  58. out = new PrintWriter(socket.getOutputStream(), true);
  59. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  60. } catch (UnknownHostException e) {
  61. System.out.println("Unknown host: kq6py.eng");
  62. System.exit(1);
  63. } catch (IOException e) {
  64. System.out.println("No I/O");
  65. System.exit(1);
  66. }
  67. }
  68.  
  69. public static void main(String[] args){
  70. SocketClient frame = new SocketClient();
  71. frame.setTitle("Client Program");
  72. WindowListener l = new WindowAdapter() {
  73. public void windowClosing(WindowEvent e) {
  74. System.exit(0);
  75. }
  76. };
  77.  
  78. frame.addWindowListener(l);
  79. frame.pack();
  80. frame.setVisible(true);
  81. frame.listenSocket();
  82. }
  83. }
  84.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,469
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Problem: Instant Messenger

  #2  
Jul 17th, 2008
And your question is?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Problem: Instant Messenger

  #3  
Jul 17th, 2008
Hmm I posted my question, but somehow, something went wrong. Okay, so anyways,

i run the client, but it keeps aborting the Operation.

There are no errors, but console displays" NO I/O"
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 798
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 78
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: Problem: Instant Messenger

  #4  
Jul 17th, 2008
An I/O Exception is being thrown.

You may find it useful to look up the Socket class in the java.net API

http://java.sun.com/javase/6/docs/ap...et/Socket.html

Here's a quote from the Socket constructor you're using--

Socket

public Socket(String host,
              int port)
       throws UnknownHostException,
              IOException

    Creates a stream socket and connects it to the specified port number on the named host.

    If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

    If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

    If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

    Parameters:
        host - the host name, or null for the loopback address.
        port - the port number. 
    Throws:
        UnknownHostException - if the IP address of the host could not be determined. 
        IOException - if an I/O error occurs when creating the socket. 
        SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
    See Also:
        setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)


You may also want to try looking at PrintWriter and BufferedReader classes, or simply print a stack trace of the error to see where it was thrown and what is responsible for throwing it.
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 798
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 78
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: Problem: Instant Messenger

  #5  
Jul 17th, 2008
By the way, you may want to use a Port number greater than 5000, as suggested by my Instructor.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,469
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Problem: Instant Messenger

  #6  
Jul 17th, 2008
Correct. Add an e.printStackTrace() to the catch block printing that message so you can see what type of IO Exception you're getting. Also, as long as the port is above 1024, anyone can use, but a very large number of ports under 5000 are used by widespread programs, and so there is a large chance of a conflict, but if the server is actually able to bind it's listening sockt, that is not the problem.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Problem: Instant Messenger

  #7  
Jul 17th, 2008
Yeah the server has no problem running, so that means the port is fine, right?

Okay I added that e.printStackTrace();

Now these are the Errors I get.

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:179)
at SocketClient.listenSocket(SocketClient.java:61)
at SocketClient.main(SocketClient.java:86)
No I/O

*I only started Java a week ago by learning through online tutorials, so I don't really understand what all these errors are telling me*
Last edited by bloody_ninja : Jul 17th, 2008 at 3:11 am.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,469
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Problem: Instant Messenger

  #8  
Jul 17th, 2008
Is there a server running?

To use a client you have to have something to connect to, and that message means (usually) that there is nothing listening on that port, at that ip.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Jul 2008
Posts: 92
Reputation: bloody_ninja is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
bloody_ninja bloody_ninja is offline Offline
Junior Poster in Training

Re: Problem: Instant Messenger

  #9  
Jul 17th, 2008
Oh I fixed the problem. That was annoying.

There was something wrong with this line.
  1. socket = new Socket("124.243.213.225", 4444);

Apparently, I wasn't supposed to put my IP there, but rather my computer name. Problem solved, it all works now

So I fixed it like this

  1. socket = new Socket("Tony_Arcos.", 5555);

*At first, I changed the ports when you guys said there could be some confliction there, but it was actually the stupid host name *
Last edited by bloody_ninja : Jul 17th, 2008 at 3:23 am.
Reply With Quote  
Join Date: Jul 2008
Location: Amsterdam, The Netherlands
Posts: 6
Reputation: rkrijgsheld is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
rkrijgsheld rkrijgsheld is offline Offline
Newbie Poster

Re: Problem: Instant Messenger

  #10  
Jul 17th, 2008
hi,

print the stacktrace that occurs at the "NO I/O" line. That might give more info.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC