0

all right i need help to run the java applets my former teacher gave me. they were two..one was called server another was called client. you run the server one on one pc, it asks you to enter the port number. you run the client applet on another pc on the same network and enter the same port number. the both get connected and now you can send messages between these two pcs. that's how my teacher showed me. now i am trying to do that but i am getting an error "java.net.ConnectException : Connection refused : Connect "

now i know it used to work long back and the code has not been modified. but i cannot get it to work now. maybe im missing something? do i put the codes for the applets here? cus i can't find an option to attach something.


ChatClient.java code :

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatClient implements ActionListener{
    static JTextArea ta = null;
    static JButton b1=null;
    static DataInputStream in = null;
    static DataOutputStream out = null;
    static Socket s = null;
    static ServerSocket ss = null;
    public ChatClient(){
        JFrame jf = new JFrame("Chat Client");
        jf.setSize(300,300);
        jf.setLocation(150,150);
        ta = new JTextArea();
        b1 = new JButton("Send");

        jf.add("South",b1);
        jf.add("North",ta);

        b1.addActionListener(this);
        jf.setVisible(true);        
    }
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==b1){
            try{
                out.writeUTF(ta.getText());
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
    }
    public static void main(String[]args){
        new ChatClient();
        try{
            int port = Integer.parseInt(JOptionPane.showInputDialog("Enter port number"));
            s = new Socket("127.0.0.1",port);
            System.out.println("client is connected");
            in = new DataInputStream(s.getInputStream());
            out = new DataOutputStream(s.getOutputStream());
            while(true){
                ta.setText(in.readUTF());
            }   
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

ChatServer.java code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatServer implements ActionListener{
    static JTextArea ta = null;
    static JButton b1=null;
    static DataInputStream in = null;
    static DataOutputStream out = null;
    static Socket s = null;
    static ServerSocket ss = null;
    public ChatServer(){
        JFrame jf = new JFrame("Chat Server");
        jf.setSize(300,300);
        jf.setLocation(50,50);
        ta = new JTextArea();
        b1 = new JButton("Send");

        jf.add("South",b1);
        jf.add("North",ta);

        b1.addActionListener(this);
        jf.setVisible(true);        
    }
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==b1){
            try{
                out.writeUTF(ta.getText());
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
    }
    public static void main(String[]args){
        new ChatServer();
        try{
            int port = Integer.parseInt(JOptionPane.showInputDialog("Enter port number"));
            ss = new ServerSocket(port);
            System.out.println("Server start and wait...........");
            s = ss.accept();
            System.out.println("client is connected");
            in = new DataInputStream(s.getInputStream());
            out = new DataOutputStream(s.getOutputStream());
            while(true){
                ta.setText(in.readUTF());
            }   
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

i REALLY need this to work and i am all confused. please guide me.

  • 2
    "I can't get it to work" -- how does it fail? An exception? (what exception, what line...) Unexpected output? (what is the output, expected output...) – John Dvorak Feb 18 '13 at 18:28
  • The answers to [this question](http://stackoverflow.com/questions/4959760/java-networking-connection-refused-connect) might help you; that error usually suggests that your connection is misconfigured. – Jim Dagg Feb 18 '13 at 18:45
  • Change every catch from `catch(Exception e){ System.out.println(e);` to `catch(Exception e){ e.printStackTrace();` open the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) and copy/paste the output as an edit to the question. – Andrew Thompson Feb 18 '13 at 22:54
  • @JanDvorak im getting "java.net.ConnectException : Connection refused : Connect " – Gaurav Prakash Cyro Feb 20 '13 at 17:36
  • edit : i got it working. i turned off the firewall in both the computers and then tried. it worked :) thank you all for your replies. can i get some other help now? like there's a send button in both the applets. how to make it in such a way that pressing the "enter" key from the keyboard sends the message? i mean to send messages i need to press on the send button. how to do that by pressing the enter key? – Gaurav Prakash Cyro Feb 20 '13 at 18:11

1 Answers1

0

The question you asked looks a few years old. You asked another question about the send button. You can remove the send button and add a TextField. TextField has a actionListener.

Tito
  • 102
  • 9