81,114
社区成员
发帖
与我相关
我的任务
分享
import java.net.*;
import java.net.*;
import java.io.*;
public class Server{
private final int maxClientCount = 10;
private int clientNum = 0;
private ServerSocket ss;
private ChatThread communicationThread[];
public Server(){
//创建ServerSocket,允许10人在线
try{
ss = new ServerSocket(10000,maxClientCount);
}
catch(IOException e){
System.err.println(e.toString());
System.exit(1);
}
communicationThread = new ChatThread[maxClientCount];
//循环等待用户连接
for(int i=0;i<maxClientCount;i++){
try{
communicationThread[i] = new ChatThread(ss.accept(),i); //连接成功时启动它
communicationThread[i].start();
clientNum++;
}
catch(IOException e){
System.err.println(e.toString());
System.exit(1);
}
}
}
//线程类
private class ChatThread extends Thread{
private Socket socket;
private int clientID;
private DataInputStream br;
private DataOutputStream bw;
public ChatThread(Socket socket,int number){
this.socket = socket;
clientID = number;
try{
//从socket获得输入流和输出流
br = new DataInputStream(socket.getInputStream());
bw = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
System.err.println(e.toString());
System.exit(1);
}
}
//run()方法
public void run(){
try{
bw.writeInt(clientID);
}
catch(IOException e){
System.err.println(e.toString());
System.exit(1);
}
//循环读一用户的信息,并把它发送给其他用户
while(true){
try{
//读对应的用户发送过来的信息
String message = br.readUTF();
//发送给其他用户
for(int i=0;i<clientNum;i++){
communicationThread[i].bw.writeUTF("客户" + clientID + ":" + message);
}
if(message != null && message.equals("bye"))
break;
}
catch(IOException e){
System.err.println(e.toString());
System.exit(1);
}
}
try{
bw.close();
br.close();
socket.close();
}
catch(EOFException e){
System.err.println(e.toString());
}
catch(IOException e){
System.err.println(e.toString());
}
}
}
public static void main(String args[]){
new Server();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame implements Runnable{
private int clientID;
private String clientWord;
private JPanel topPanel;
private JLabel nameLabel;
private JTextField wordField;
private JButton submit;
private JTextArea displayField;
private Socket socket;
private DataInputStream br;
private DataOutputStream bw;
//构造面板
public Client(){
Container container = getContentPane();
container.setLayout(new BorderLayout(5,5));
topPanel = new JPanel(new FlowLayout());
nameLabel = new JLabel("");
wordField = new JTextField(20);
submit = new JButton("发送");
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
String sentence = wordField.getText();
bw.writeUTF(sentence);
}
catch(IOException e){
System.err.println(e.toString());
}
}
});
topPanel.add(nameLabel);
topPanel.add(wordField);
topPanel.add(submit);
displayField = new JTextArea();
container.add(topPanel,BorderLayout.NORTH);
container.add(displayField,BorderLayout.CENTER);
setSize(400,500);
setResizable(false);
setVisible(true);
}
//主方法
public static void main(String args[]){
Client clientWindow = new Client();
clientWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientWindow.nor();
}
//创建线程和socket连接
public void nor(){
Thread commThread = new Thread(this);
commThread.start();
try{
socket = new Socket("127.0.0.1",10000);
br = new DataInputStream(socket.getInputStream());
bw = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
System.err.println(e.toString());
}
}
//run方法
public void run(){
try{
//设置Label的文本
clientID = br.readInt();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
nameLabel.setText("客户" + clientID);
}
});
//循环读服务端发来的信息
while(true){
clientWord = br.readUTF();
displayMessage(clientWord);
}
}
catch(EOFException e){
System.err.println(e.toString());
}
catch(IOException e){
System.err.println(e.toString());
}
}
//把信息显示出来
private void displayMessage(final String message){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
displayField.append(message + "\n");
displayField.setCaretPosition(displayField.getText().length());
}
});
}
}
public void nor(){
try{
socket = new Socket("127.0.0.1",10000);
br = new DataInputStream(socket.getInputStream());
bw = new DataOutputStream(socket.getOutputStream());
Thread commThread = new Thread(this);
commThread.start();
}
catch(IOException e){
System.err.println(e.toString());
}
}
public void nor(){
Thread commThread = new Thread(this);
commThread.start(); // 启动线程
try{
socket = new Socket("127.0.0.1",10000);
br = new DataInputStream(socket.getInputStream()); // 然后才初始化br
bw = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
System.err.println(e.toString());
}
}
public void run(){
try{
//设置Label的文本
clientID = br.readInt(); // 碰运气语句