java chat applet需要帮助

时间:2013-02-18 18:24:08

标签: java applet chat

好吧我需要帮助才能运行我以前老师给我的java applets。 他们是两个......一个叫做服务器另一个叫做客户端。你在一台电脑上运行服务器,它会要求你输入端口号。您在同一网络上的另一台PC上运行客户端小程序并输入相同的端口号。两者都连接起来,现在你可以在这两个pc之间发送消息。 这就是我的老师给我看的方式。现在我想这样做,但我收到一个错误 “java.net.ConnectException:连接被拒绝:连接”

现在我知道它曾经长时间工作并且代码没有被修改。 但我现在不能让它工作。也许我错过了什么? 我在这里放置applet的代码吗? cus我找不到附加东西的选项。


ChatClient.java代码:

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代码:

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

我真的需要这个工作,我都很困惑。请指导我。

1 个答案:

答案 0 :(得分:0)

你问的问题看起来好几年了。您问了另一个关于发送按钮的问题。您可以删除发送按钮并添加TextField。 TextField有一个actionListener。