开发者

Why does this Exception Occur? networking (Connection Refused)

*In this program i have tried to make my pc both server and client. *The error that i get is "Connection Refused"

This is my program:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    class chatboxClient {
    JFrame fr;
    JPanel p;
    JButton send;
    JTextArea ta;
    JRadioButton rb;

     chatboxClient() {
     new chatboxServer();
     fr=new JFrame("ChatBox_CLIENT");
     p=new JPanel();
     send=new JButton("send");
     ta=new JTextArea();
     ta.setRows(20);
     ta.setColumns(20);
     rb=new JRadioButton("Connect");               // action listener for connect
     rb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent开发者_如何学JAVA ae) {
       connectActionPerformed(ae); 
      }
    });
    fr.add(p);
    p.add(ta);
    p.add(rb);
    p.add(send);
    fr.setSize(500,500);
    fr.setResizable(false);
    fr.setVisible(true);
   }

    public void connectActionPerformed(ActionEvent ae) {
     try {
       InetAddress address=InetAddress.getLocalHost();
       Socket s=new Socket(address,3000); // create connection with port number 3000 of server 
        if(s.isConnected()==true) {
         JOptionPane.showMessageDialog(new JFrame(),"Connection successfully Established");
        } else {
           JOptionPane.showMessageDialog(new JFrame(),"Error Creating Connection");
          }
     } catch(Exception exc) {
        JOptionPane.showMessageDialog(new JFrame(),exc); // line A
       }
    }

    public static void main(String args[]) {
     new chatboxClient();
    }
   }

SERVER SIDE:

    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;
    class chatboxServer {
    JFrame fr;
    JPanel p;
    JTextArea ta;
    JButton send;

    chatboxServer() {
    fr=new JFrame("ChatBox_SERVER");
    p=new JPanel();
    ta=new JTextArea();
    ta.setRows(20);
    ta.setColumns(20);
    send=new JButton("send");
    fr.add(p);
    p.add(ta);
    p.add(send);
    fr.setVisible(true);
    fr.setSize(500,500);
    fr.setResizable(false);
   }

    public static void main(String args[]) {
     new chatboxServer();
    }
   }

Here I get Exception (When i press connect) corresponding to the only try statement that i have. (Labeled A) What is the reason that i am getting exception? Note: chatboxServer() is a class defined in the same directory.

Through this program i want that message typed in one window goes to another window.I have made my pc both server and client.


If you don't believe us when we say that Connection Refused means there is no server listening on that port, you can just google for it. http://www.google.co.uk/search?q=Connection+Refused 3,620,000 hits


There is no server in your code. The only code I can see is a GUI for the server. Not actual server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜