2 pan cras Pan_cras 于 2016.04.04 19:34 提问

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)如何在服务方对退出的用户进行处理,保证聊天发送的消息只发给在场的用户。

3个回答

CSDNXIAON
CSDNXIAON   2016.04.04 19:41
已采纳

Java网络编程以及简单的聊天程序
JAVA网络编程——简单的聊天程序
JAVA 网络编程 UDP(聊天程序)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

caozhy
caozhy   Ds   Rxr 2016.04.04 22:47
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.04.05 09:29

你试试环信吧,百度imgeek即可

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!