最近在学习java现在尝试这做一个可以多人聊天的小程序

博主分享了使用Java SWT库创建多人在线聊天应用的过程。该程序包括一个客户端界面和服务器端,能够处理多个客户端之间的消息传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     最近在学习java,所以买了本Thinking in Java 看了有一段时间了,但是感觉光看着不练的话好多本来理解的知识点又都给忘了,所以我再试着写一个小程序。

     这个程序可以供多人在线聊天,首先我就想到了界面的搭建。想来想去,又到网上找了些资料最终决定用swt,因为听说这个比java的awt类swing类都要好。

     既然决定了使用swt那么我遍开始写,首先我先写出了一个显示的界面。考虑到也许很多地方要用到所以我单独写了个类,虽然在写的过程中发现这个想法也许并不好的,这个类写的很糟糕,但是作为我思路的记载,我还是贴出了。希望高手看见了别耻笑。

     另外衷心的希望有哪位高手在空闲之余看见了这篇文章可以给与一点指导。

 

点击打开代码折叠
1 package org.ssl.chat;
2
3  import org.eclipse.swt.SWT;
4  import org.eclipse.swt.graphics.Color;
5 import org.eclipse.swt.widgets.Composite;
6 import org.eclipse.swt.widgets.Display;
7 import org.eclipse.swt.widgets.Menu;
8 import org.eclipse.swt.widgets.MenuItem;
9 import org.eclipse.swt.widgets.Shell;
10 import org.eclipse.swt.widgets.Text;
11
12
13 public class ChatDisplay {
14 private Display cds = new Display();
15 private Shell shell = new Shell();
16 public Text rt = new Text(shell, SWT.MULTI);
17 private Text pt = new Text(shell, SWT.MULTI);
18
19 public void setChatLayout(String s, String sText) {
20 shell.setLayout(null);
21 Composite composite = new Composite(shell, SWT.NONE);
22 Menu menu = new Menu(shell, SWT.BAR);
23 shell.setText(s);
24 shell.setSize(520, 480);
25 Color color = new Color(Display.getCurrent(), 255, 0, 255);
26 shell.setBackground(color);
27 rt.setText(sText);
28 rt.setEditable(false);
29 rt.setSize(520, 280);
30 pt.setSize(520, 120);
31 pt.setBounds(0, 310, 520, 120);
32 pt.setText("OK");
33 composite.setBackground(new Color(Display.getCurrent(), 80, 140, 240));
34 composite.setBounds(0, 280, 520, 30);
35 shell.setMenuBar(menu);
36 MenuItem fileItem=new MenuItem(menu,SWT.CASCADE);
37 fileItem.setText("文件&F");
38 //t.pack();
39 shell.open();
40
41 while(!shell.isDisposed()) {
42 if(!cds.readAndDispatch()) {
43 cds.sleep();
44 }
45 }
46
47 cds.dispose();
48 }
49
50 public void addListener(PTListener p) {
51 pt.addKeyListener(p);
52 }
53 }

 

    有了窗口显示了,接下来我就开始准备写一个服务器端了

 

 

点击打开代码折叠
package org.ssl.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


public class ChatServer {

boolean started = false;
ServerSocket ss
= null;
List
<Client> clients = new ArrayList<Client>();

/**
*
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatServer().start();
}

public void start() {
try {
ss
= new ServerSocket(8888);
}
catch (IOException e) {
e.printStackTrace();
}

started
= true;
while(started) {
Socket s;
try {
s
= ss.accept();
System.out.print(
"一个客户端链接上来了!");
Client client
= new Client(s);
new Thread(client).start();
clients.add(client);
}
catch (IOException e) {
e.printStackTrace();
}

}
}


class Client implements Runnable {

private Socket soc = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean beConnected = false;

public Client(Socket s) {
soc
= s;
try {
dis
= new DataInputStream(soc.getInputStream());
dos
= new DataOutputStream(soc.getOutputStream());
beConnected
= true;
}
catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str) {
try {
dos.writeUTF(str);
}
catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
try {
while(beConnected) {
String str
= dis.readUTF();
System.out.println(str);
for(int i = 0; i < clients.size(); i++) {
Client c
= clients.get(i);
c.send(str);
}
}
}
catch (IOException e) {
//e.printStackTrace();
System.out.print("客户端离开了");
}
finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(soc != null) soc.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

}

 

客户端的正在写,之后完成会贴上来。后续有修改也会贴出来。作为自己第一次尝试的一个记录。

转载于:https://www.cnblogs.com/china_ssl/archive/2010/12/16/chat.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值