为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

JAVA的即时通讯源代码

2011-11-29 9页 doc 30KB 62阅读

用户头像

is_266784

暂无简介

举报
JAVA的即时通讯源代码JAVA实现即使通讯的代码 图形界面   ////////////////////////////////////////////////////////// //用 Java 编写的聊天器,可以当 服务器 或者 是客户端,一对一,自定义对方 IP 及 端口。 //虽然名为 LANChat ,但不限于局域网。对象甚至可以是某种 “服务器”。 /////////////////////////////////////////////////////////// import java.io.*; import...
JAVA的即时通讯源代码
JAVA实现即使通讯的代码 图形界面   ////////////////////////////////////////////////////////// //用 Java 编写的聊天器,可以当 服务器 或者 是客户端,一对一,自定义对方 IP 及 端口。 //虽然名为 LANChat ,但不限于局域网。对象甚至可以是某种 “服务器”。 /////////////////////////////////////////////////////////// import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; ////////////////////////////////////////////////////////// public class LANChatV12{ public static void main(String args[]){ LoginFrame lf = new LoginFrame("输入目标"); lf.show(); } } ///////////////////////////////////////////////////////// class LoginFrame extends JFrame{ JButton BOK; JLabel LdesAddr, Lport; JTextField TFdesAddr, TFport; String desAddr, port; LoginFrame(){} LoginFrame(String title){ super(title); Frame t = this; BOK = new JButton("确定"); LdesAddr = new JLabel("目标 IP"); Lport = new JLabel("端口 "); TFdesAddr = new JTextField(desAddr,12); TFport = new JTextField(port,12); //TFpassword.setEchoChar('*'); BOK.addActionListener(new BOKListener(t)); setBackground(Color.blue); setBounds(350,250,200,128); setLayout(new FlowLayout(FlowLayout.CENTER,5,7)); add(LdesAddr); add(TFdesAddr); add(Lport); add(TFport); add(BOK); setResizable(false); //setVisible(true); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } class BOKListener implements ActionListener{ Frame t; BOKListener(){} BOKListener(Frame t){ this.t = t; } public void actionPerformed(ActionEvent e){ desAddr = TFdesAddr.getText(); port = TFport.getText(); t.setVisible(false); Messenger m = new Messenger(desAddr, port); m.start(); } } } ///////////////////////////////////////////////////// class Messenger extends Thread{ String desAddr; String port; int iport; TextArea content, send; JButton Bsend; ChatFrame cf; String title; Socket client; ServerSocket ss; OutputStreamWriter osw = null; InputStreamReader isr = null; BufferedReader br; String line; boolean flag;//端口号是否正确 boolean cbc; //can be client ? int tryTurns = 3; //客户方式尝试的次数 Messenger(){} Messenger(String desAddr, String port){ super("LANChatMessenger"); content = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); send = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); Bsend = new JButton("发送"); Bsend.setEnabled(false); title = "与 " + desAddr +" 聊天"; flag = true; cbc = true; cf = new ChatFrame(title, content, send, Bsend); cf.show(); this.desAddr = desAddr; this.port = port; try{ iport = Integer.parseInt(port); }catch(NumberFormatException nfe){ content.append("非法的 端口,程序 4 秒后关闭。\n"); flag = false; } } public void run(){ if(! flag){ try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } content.append("正在以客户端方式进行连接.....\n"); do{ cbc = true; try{ client = new Socket(desAddr,iport); }catch(Exception e){ content.append("错误,无法连接地址:" + desAddr +":"+ port + '\n'); content.append("等待 1 秒再连接,剩余 [ " + (tryTurns -1) + " ]次。\n"); cbc = false; try{ Thread.sleep(1000); }catch(InterruptedException ie){ System.exit(0); } } --tryTurns; }while((tryTurns > 0) && ! cbc); if(cbc){ content.append("连接成功,可以开始了。" + "\n\n"); cf.setTitle("与 " + desAddr + ":" + port + " 聊天"); send.requestFocus(); } else{ content.append("客户端方式失败,现在启动服务器并等待连接。" + '\n'); try{ ss = new ServerSocket(iport); }catch(IOException ioe){ content.append("\n无法创建服务,程序将在 4 秒后退出。\n"); try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } try{ cf.setTitle("等待连接中....在端口:" + port); client = ss.accept(); }catch(Exception e){ content.append("\nss.accept() 方法失败,程序将在 4 秒后退出。\n"); try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } content.append("连接成功,可以开始。" + client.getInetAddress().toString() + "\n\n"); cf.setTitle("与 " + client.getInetAddress().toString() + ":" + port + " 聊天"); send.requestFocus(); } try{ osw = new OutputStreamWriter(client.getOutputStream()); isr = new InputStreamReader(client.getInputStream()); br= new BufferedReader(isr); //content.append("\nbr created\n"); }catch(IOException ioe){ content.append("创建流错误,程序将在 4 秒后退出。\n"); try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } Bsend.setEnabled(true); Bsend.addActionListener(new BsendListener(content, send, osw)); try{ line = br.readLine(); }catch(IOException ioe){ content.append("流读取错误,程序将在 4 秒后退出。\n"); try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } while(true){ content.append("他说 : " + line + '\n'); try{ Thread.sleep(1000); }catch(InterruptedException ie){ System.exit(0); } try{ line = br.readLine(); }catch(IOException ioe){ content.append("流读取错误,程序将在 4 秒后退出。\n"); try{ Thread.sleep(4000); }catch(InterruptedException ie){ System.exit(0); } System.exit(0); } } } class BsendListener implements ActionListener{ TextArea content; TextArea send; OutputStreamWriter osw; BsendListener(TextArea content, TextArea send, OutputStreamWriter osw){ this.content = content; this.send = send; this.osw = osw; } public void actionPerformed(ActionEvent e){ String input; input = send.getText(); if(input.length() > 0){ content.append("我说 : " + input + '\n'); try{ osw.write(input + '\n', 0, input.length() + 1); osw.flush();// 晕 }catch(Exception ee){ content.append("不能发送 \"" + input + "\" , 发生了错误 : " +ee.getMessage() + '\n'); } send.setText(""); send.requestFocus(); } } } } /////////////////////////////////////////////////////////////// class ChatFrame extends JFrame{ TextArea content; TextArea send; JButton Bsend; ChatFrame(){} ChatFrame(String frameTitle,TextArea content, TextArea send, JButton Bsend){ super(frameTitle); this.content = content; this.send = send; this.Bsend = Bsend; this.send.setEditable(true); setLayout(null); setBounds(300,100,510,500); add(content); content.setBackground(Color.WHITE); content.setBounds(2,0,500,340); content.setFocusable(true); add(send); send.setBounds(2,355,500,70); send.setFocusable(true); add(Bsend); Bsend.setBounds(410,432,60,30); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); } }
/
本文档为【JAVA的即时通讯源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索