chat 聊天系统

跟着马士兵老师开始做第一个项目。

分了很多步写,涉及的知识也比较多!

1.写了个窗口出来,客户端。这里主要是 gui 一章里的。component下container,container下又分window和panel,window下份Frame和Dialay.主要是Frame,布局管理器。当然还有各种按钮,和网页的差不多。还有一点非常重要,就是事件监听机制。

2起一个服务器端,主要的是java.net部分的和java.io的。关键字有ServerSocket,Socket,字节流,数据流DataInputStream, 反正就是输入输出,这边出,那边收,反之,亦然。还有就是writeUTF方法,read方法等等。这里就是要实现客户端发一个信息,服务器那边收到。

3.这步就是要把要把每客户端发过来的信息一一转发给各个客户端。这里就要用到多线程了,每一个线程负责把发过来的信息接收过来,并把该客户放进集合(ArrayList)集合里面的客户。然后把每一条发过来的信息一一转发给集合里面的每一个客户!

这里用到的是java.util里面的知识。定义的Client是一个内部类。

4.调试,完善。程序的大体框架建起来的,但离完善还很远。程序中抛出的exception还要细致处理。一步步调试,尽量让其能处理各种情况出现的异常。完善一些小的地方,更人性化一些。

附代码:

客户端

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class ChatClient extends Frame{
Socket cs = null;
private boolean bconnect = false;
DataOutputStream dos = null;
DataInputStream dis = null;
Thread ts = null;
TextArea n1 = new TextArea("会话窗口",20,50,3); //常量字段值,只创建垂直滚动条
TextField n2 = new TextField(50);
String name = null;
TextField id = new TextField(20);

public static void main(String[] args) {
new ChatClient().useid();
}

public void launchFrame(String s){

}

public void connection(){
try {
cs = new Socket("127.0.0.1",5231);
dos = new DataOutputStream(cs.getOutputStream());
bconnect = true;
n1.setText("已连接服务器!"+'\n'+'\n'+" ");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void disconnection(){
try {
dis.close();
dos.close();
cs.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void recive(){
Server s = new Server();
ts = new Thread(s);
ts.start();
}

public void useid(){
Id i = new Id();
}

class Id extends Frame{

Id(){
setBounds(100,100,6,50);
setTitle("请输入名字");
add(id);
pack();
id.addActionListener(new IdMonitor());
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}

class IdMonitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
name = id.getText().trim();
//System.out.println(name);
Frame f = new Frame();
f.setTitle(name+"正聊天中");
f.setBounds(10,10,200,300);
f.setLayout(new BorderLayout());
f.add(n1,BorderLayout.NORTH);
f.add(n2,BorderLayout.SOUTH);
n2.addActionListener(new Tn2Monitor());
f.pack();
f.setVisible(true);
connection();
recive();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
disconnection();
System.exit(0);
}
});
}
}

class Server implements Runnable{

int count = 0;
String str7 = null;
String str8 = null;

Server(){
try {
dis = new DataInputStream(cs.getInputStream());
//System.out.println(dis);
} catch (IOException e) {
e.printStackTrace();
}
}

public void run(){
try {
while(bconnect){
String str = dis.readUTF();
if(count<9){
if(count == 7){
str7 = str;
n1.setText(n1.getText()+str+'\n'+'\n'+" ");
count++;
}else if(count == 8){
str8 = str;
n1.setText(n1.getText()+str+'\n'+'\n'+" ");
count++;
}else{
n1.setText(n1.getText()+str+'\n'+'\n'+" ");
count++;
}
}else{
count = 0;
n1.setText(" "+str7+'\n'+'\n'+" "+str8+'\n'+'\n'+" "+str+'\n'+'\n'+" ");
}

}
}catch(SocketException e){
System.out.println("客户端关闭");
}
catch (IOException e) {
e.printStackTrace();
}
}
}

private class Tn2Monitor implements ActionListener{

public void actionPerformed(ActionEvent e) {

String str = null;

str = n2.getText().trim();
n2.setText("");
str = (name+": "+str);
//System.out.println(str);
//n1.setText(str);
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

服务端

import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;

public class ChatServer {
boolean started = false;
ServerSocket ss = null;
ServerFrame sf = null;
ArrayList<Client> client = new ArrayList<Client>();

public static void main(String[] args) {
new ChatServer().start();
}

public void start(){
try {
ss = new ServerSocket(5231);
} catch (BindException e) {
System.out.println("端口已被使用!");
} catch (IOException e) {
e.printStackTrace();
}
sf = new ServerFrame("服务器");
try {
started = true;
while (started) {
Socket s = ss.accept();
System.out.println("a client connection!");
Client c = new Client(s); //起一个新的线程!
client.add(c);
Thread tc = new Thread(c);
tc.start();
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Client implements Runnable {
Socket s = null;
DataInputStream dis = null;
private boolean cstarted = false;
DataOutputStream dos =null;

Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
cstarted = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str){
try {
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}catch(SocketException e){
//e.printStackTrace();
System.out.println("退出了!");
client.remove(this);
}
catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
try {
while (cstarted) {
String str = dis.readUTF();
//System.out.println(dis.readUTF());
//sf.ts.setText(dis.readUTF());
// sf.ts.setText("");
for(int i = 0;i<client.size();i++){
Client c = client.get(i);
c.send(str);
}

/*for(Iterator<Client> it = client.iterator();it.hasNext();){
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e) {
System.out.println("客户端关闭!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

class ServerFrame extends Frame {

TextArea ts = null;

ServerFrame(String s) {
setTitle(s);
setBounds(100, 100, 320, 380);
ts = new TextArea(160, 160);
add(ts);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值