•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,811 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 1,934 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: 494 | Replies: 12 | Solved
![]() |
•
•
Join Date: Jul 2008
Posts: 92
Reputation:
Rep Power: 1
Solved Threads: 1
java Syntax (Toggle Plain Text)
import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class SocketClient extends JFrame implements ActionListener { JLabel text, clicked; JButton button; JPanel panel; JTextField textField; Socket socket = null; PrintWriter out = null; BufferedReader in = null; SocketClient(){ //Begin Constructor text = new JLabel("Text to send over socket:"); textField = new JTextField(20); button = new JButton("Click Me"); button.addActionListener(this); panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel); panel.add("North", text); panel.add("Center", textField); panel.add("South", button); } //End Constructor public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if(source == button){ //Send data over socket String text = textField.getText(); out.println(text); textField.setText(new String("")); //Receive text from server try{ String line = in.readLine(); System.out.println("Text received :" + line); } catch (IOException e){ System.out.println("Read failed"); System.exit(1); } } } public void listenSocket(){ //Create socket connection try{ socket = new Socket("124.243.213.225", 4444); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.out.println("Unknown host: kq6py.eng"); System.exit(1); } catch (IOException e) { System.out.println("No I/O"); System.exit(1); } } public static void main(String[] args){ SocketClient frame = new SocketClient(); frame.setTitle("Client Program"); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); frame.listenSocket(); } }
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
----------------------------------------------
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
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--
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.
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.
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
----------------------------------------------
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
•
•
Join Date: Jul 2008
Posts: 92
Reputation:
Rep Power: 1
Solved Threads: 1
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*
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.
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.
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
----------------------------------------------
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
•
•
Join Date: Jul 2008
Posts: 92
Reputation:
Rep Power: 1
Solved Threads: 1
Oh I fixed the problem. That was annoying.
There was something wrong with this line.
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
*At first, I changed the ports when you guys said there could be some confliction there, but it was actually the stupid host name
*
There was something wrong with this line.
java Syntax (Toggle Plain Text)
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
java Syntax (Toggle Plain Text)
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Windows Explorer Problem/Virus (Viruses, Spyware and other Nasties)
- creating an instant messenger program in java (Java)
- internet explorer / virus problem here (Viruses, Spyware and other Nasties)
- Classes Problem (C++)
- Need Help for DNS Problem and 'about:blank' Problem ... (Viruses, Spyware and other Nasties)
- Another about:blank problem :-x (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: setting a string to a char
- Next Thread: Help with part 2 of Inventory Program



Linear Mode