有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

socketJava基本聊天

我有两个类:聊天客户端和聊天服务器。 我必须打开服务器,然后其他人必须打开客户端。如果我们中的一方在文本字段中键入某个内容并单击“发送”,另一方将在文本区域中接收该内容。 如果我使用localhost或Ipv4地址并在本地运行它,一切都会正常工作

s = new Socket("127.0.0.1", 1300);

我打开了服务器,让一个朋友打开客户端,但什么都没用(我在Socket(主机,端口)中使用了ipv4作为第一个参数)

和你的客户聊天。爪哇:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.*;

public class chat_client extends JFrame {

    private static JPanel contentPane;
    private static JTextField textField;
    private static JTextArea textArea;

    private static DataOutputStream dos;
    private static DataInputStream dis;

    private static Socket s;


/**
 * Launch the application.
 */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    chat_client frame = new chat_client();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        try {
            s = new Socket("127.0.0.1", 1300);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {
            dis = new DataInputStream(s.getInputStream());
            dos = new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String msg = "";

        while(true)
        {
            try {
                msg = dis.readUTF();

                String text = textArea.getText().trim() + "\n" + msg;
                textArea.setText(text);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

/**
 * Create the frame.
 */
    public chat_client() {
        setTitle("Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(82, 209, 123, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnSend = new JButton("Send");
        btnSend.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String msg = textField.getText();
                try {
                    dos.writeUTF(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                textField.setText(null);
            }
        });
        btnSend.setBounds(253, 208, 89, 23);
        contentPane.add(btnSend);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(53, 27, 315, 145);
        contentPane.add(scrollPane);

        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
    }
}

聊天室服务器。爪哇:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;    
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class chat_server extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private static ServerSocket ss;
    private static Socket s;
    private static DataInputStream dis;
    private static DataOutputStream dos;
    private static JTextArea textArea;

/**
 * Launch the application.
 */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    chat_server frame = new chat_server();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });



        try {
            ss = new ServerSocket(1300);
            s = ss.accept();
            JOptionPane.showMessageDialog(null, "Client logged!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String msg = "";

        while(true)
        {
            try {
                msg = dis.readUTF();
                String text = textArea.getText().trim() + "\n" + msg.trim();
                textArea.setText(text);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

/**
 * Create the frame.
 */
    public chat_server() {
        setTitle("Server");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(53, 34, 332, 142);
        contentPane.add(scrollPane);

        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);

        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(94, 205, 123, 20);
        contentPane.add(textField);

        JButton button = new JButton("Send");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String msg = "";
                msg = textField.getText().trim();
                try {
                    dos.writeUTF(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                textField.setText("");
            }
        });
        button.setBounds(270, 204, 89, 23);
        contentPane.add(button);
    }

}

如果要测试它,请始终先运行服务器,然后运行客户端。如果工作正常,将出现一个消息框


共 (1) 个答案

  1. # 1 楼答案

    不要在客户端代码中硬编码环回ip(127.0.0.0)。您的朋友不在同一台计算机上,无法访问您的服务器