客户端java程序编写_一个Java实现的简单的多个客户端聊天程序详解

客户端

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.lang.*;

import java.net.*;

public class HeartClient extends Frame {

/*

* 成员方法出场...

*/

private TextField tfText;

private TextArea taContent;

private Socket s;

private DataOutputStream dos;

private DataInputStream dis;

/**

* 注意,入口... ^^

* @param args

*/

public static void main(String[] args) {

new HeartClient().launchFrame();

}

/**

* Loading GU

*/

public void launchFrame(){

tfText = new TextField();

taContent = new TextArea();

this.setSize(300,300);

this.setLocation(300,300);

this.tfText.addActionListener(new TFListener());

this.add(tfText,BorderLayout.SOUTH);

this.add(taContent,BorderLayout.NORTH);

this.addWindowListener(new WindowAdapter(){

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}});

this.pack();

this.connect();

this.setVisible(true);

}

/**

* 我在努力地连接服务器中...

*/

public void connect() {

try {

s = new Socket("127.0.0.1",1720);

dos = new DataOutputStream(s.getOutputStream());

dis = new DataInputStream(s.getInputStream());

new Thread(new SendThread()).start();

// dos.writeUTF("Hello,i find u!");

} catch (UnknownHostException e) {

System.out.println("UnknownHostException");

e.printStackTrace();

} catch (IOException e) {

System.out.println("IOException");

e.printStackTrace();

}finally{

//关闭啥尼...

}

}

/**

* 额不会傻等着tfText(TextField tfText的监听器类)

*/

class TFListener implements ActionListener{

private String str;

@Override

public void actionPerformed(ActionEvent e) {

str = tfText.getText().trim();

tfText.setText("");

try {

dos.writeUTF(str);

} catch (IOException e1) {

System.out.println("IOException");

e1.printStackTrace();

}

}

}

/**

* 客户端接收消息的线程呦...

*

*/

class SendThread implements Runnable{

private String str;

private boolean iConnect = false;

public void run(){

iConnect = true;

recMsg();

}

/**

* 消息,看招,哪里跑...(客户端接收消息的实现)

* @throws IOException

*/

public void recMsg() {

try {

while(iConnect){

str = dis.readUTF();

taContent.setText(str);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

服务器端

import java.io.*;

import java.net.*;

import java.util.*;

public class HeartServer {

/*

* 成员变量闪亮登场

*/

List clients = new ArrayList();

/**

* 这系入口啊,向这里看齐...

* @param args

*/

public static void main(String[] args) {

new HeartServer().start();

}

/**

* 启动服务器中...

*

*/

public void start(){

try {

boolean iConnect = false;

ServerSocket ss = new ServerSocket(1720);

iConnect = true;

while(iConnect){

System.out.println("绑定服务器端口成功!");

Socket s = ss.accept();

ClientThread currentClient = new ClientThread(s);//创建线程引用

System.out.println("发现客户端!");

clients.add(currentClient);//把当前客户端加入集合

new Thread(currentClient).start();

System.out.println("客户端进程已经启动!");

}

} catch (IOException e) {

System.out.println("IOException");

e.printStackTrace();

}

}

/**

* 给每个客户端留个家(客户端的进程)

*

*/

class ClientThread implements Runnable {

/*

* 成员变量又来啦...

*/

private Socket s;

private DataInputStream dis;

private DataOutputStream dos;

private String str;

private boolean iConnect = false;

/**

* 小构一下

*/

ClientThread(Socket s){

this.s = s;

iConnect = true;

}

public void run(){

System.out.println("run方法启动了!");

try {

while(iConnect){

System.out.println("RUN方法中的while循环启动,正在等待客户端的发送消息...");

dis = new DataInputStream(s.getInputStream());

str = dis.readUTF();

System.out.println(str);

for(int i=0; i

System.out.println("转发消息中..."+i);

ClientThread c = clients.get(i);

c.sendMsg(str);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 转发消息,我做主...

* 将送至服务器的消息发送给每个连接到的客户端

*/

public void sendMsg(String str){

try {

System.out.println("创建输出管道!");

dos = new DataOutputStream(this.s.getOutputStream());

System.out.println("正在向客户端写消息!");

dos.writeUTF(str);

System.out.println("正在向客户端写消息成功!");

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值