pan_cras 2016-04-04 11:34 采纳率: 100%
浏览 2586

Java的网络编程(聊天小程序)

放上源程序代码
服务端:

import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class TalkServer {
public static ArrayList<Client> allClient = new ArrayList<Client>();
public static int clientNum = 0;

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(5432);
        while (true) {
            Socket socket = serverSocket.accept();
            DataOutputStream dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            DataInputStream dataInputStream = new DataInputStream(
                    socket.getInputStream());
            Client client = new Client(clientNum, dataOutputStream,
                    dataInputStream);
            allClient.add(client);
            client.start();
            clientNum++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

class Client extends Thread {
int id;
DataOutputStream dataOutputStream;
DataInputStream dataInputStream;

public Client(int id, DataOutputStream dataOutputStream,
        DataInputStream dataInputStream) {
    this.id = id;
    this.dataOutputStream = dataOutputStream;
    this.dataInputStream = dataInputStream;
}

public void run() {
    while (true) {
        try {
            String message = dataInputStream.readUTF();
            int m = TalkServer.clientNum;
            for (int i = 0; i < m; i++) {
                TalkServer.allClient.get(i).dataOutputStream
                        .writeUTF(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

客户端

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;

public class TalkClient {

/**
 * @param args
 * @throws IOException
 * @throws UnknownHostException
 */
public static void main(String[] args) throws UnknownHostException,
        IOException {
    Socket socket = new Socket("localhost", 5432);
    DataInputStream dataInputStream = new DataInputStream(
            socket.getInputStream());
    final DataOutputStream dataOutputStream = new DataOutputStream(
            socket.getOutputStream());
    Frame frame = new Frame("client");
    Panel panel = new Panel();
    final TextField input = new TextField(20);
    TextArea displayArea = new TextArea(5, 20);
    displayArea.setEditable(false);
    panel.add(input);
    panel.add(displayArea);
    frame.add(panel);
    final String name = JOptionPane.showInputDialog("输入您的昵称:");
    new receiveThread(dataInputStream, displayArea);
    input.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                dataOutputStream.writeUTF(name + ":"
                        + input.getText());
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    });
    frame.setSize(300, 300);
    frame.setVisible(true);
}

}

class receiveThread extends Thread {
DataInputStream dataInputStream;
TextArea displayArea;

public receiveThread(DataInputStream dataInputStream, TextArea displayArea) {
    this.dataInputStream = dataInputStream;
    this.displayArea = displayArea;
    this.start();
}

public void run() {
    for (;;) {
        try {
            String str = new String(dataInputStream.readUTF());
            displayArea.append(str + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

TalkServer是服务器代码
TalkClinet是客户端代码
这是一个简易的聊天程序。
(1)如何修改服务方,使用户自己发送的消息不显示在自己的文本域中。
(2)在客户方显示用户列表,可以选择将信息发送给哪些用户。
(3)如何在服务方对退出的用户进行处理,保证聊天发送的消息只发给在场的用户。

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-04-04 14:47
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
  • ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出