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?

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 java.net API

http://java.sun.com/javase/6/docs/api/java/net/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.

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.

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*

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

hi,

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.