import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;


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");

     panel = new JPanel();
     panel.setLayout(new BorderLayout());
     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();
	  textField.setText(new String(""));
//Receive text from server
	  String line = in.readLine();
          System.out.println("Text received :" + line);
       } catch (IOException e){
	 System.out.println("Read failed");
  public void listenSocket(){
//Create socket connection
       socket = new Socket("", 4444);
       out = new PrintWriter(socket.getOutputStream(), true);
       in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     } catch (UnknownHostException e) {
       System.out.println("Unknown host: kq6py.eng");
     } catch  (IOException e) {
       System.out.println("No I/O");

   public static void main(String[] args){
        SocketClient frame = new SocketClient();
	frame.setTitle("Client Program");
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) {


And your question is?

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"

An I/O Exception is being thrown.

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

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


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

    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.

        host - the host name, or null for the loopback address.
        port - the port number. 
        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(, 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.

By the way, you may want to use a Port number greater than 5000, as suggested by my Instructor.

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 it (ports under that are only available to accounts with system admin privileges), 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.

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. Connection refused: connect
at Method)
at SocketClient.listenSocket(
at SocketClient.main(
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*

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.

Oh I fixed the problem. That was annoying.

There was something wrong with this line.

socket = new Socket("", 4444);

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

So I fixed it like this

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 :(*


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

I already said the problem was solved in like post 9 <.<

I already said the problem was solved in like post 9 <.<

And if you look closely, you will notice that this is two threads that have now been combined by the admins (since the link in my last post links to this thread).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.