安卓(Android)毕设局域网聊天

  1.      开发环境                                                                                                  


    开发环境 :AndroidStudio3.0.1

    语音    :   JAVA

    JDK    :   1.8

    支持版本  :Android4.1以上

     

  2. 效果图

3.检测同一局域网下的客户端的上下线链接


	public void noticeOnline(){	// 发送上线广播
		IpMessageProtocol ipmsgSend = new IpMessageProtocol();
		ipmsgSend.setVersion(String.valueOf(IpMessageConst.VERSION));
		ipmsgSend.setSenderName(selfName);
		ipmsgSend.setSenderHost(selfGroup);
		ipmsgSend.setCommandNo(IpMessageConst.IPMSG_BR_ENTRY);	//上线命令
		ipmsgSend.setAdditionalSection(selfName + "\0" );	//附加信息里加入用户名和分组信息

		InetAddress broadcastAddr;
		try {
			broadcastAddr = InetAddress.getByName("255.255.255.255");	//广播地址
			sendUdpData(ipmsgSend.getProtocolString()+"\0", broadcastAddr, IpMessageConst.PORT);	//发送数据
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(TAG, "noticeOnline()....广播地址有误");
		}

	}

	public void noticeOffline(){	//发送下线广播
		IpMessageProtocol ipmsgSend = new IpMessageProtocol();
		ipmsgSend.setVersion(String.valueOf(IpMessageConst.VERSION));
		ipmsgSend.setSenderName(selfName);
		ipmsgSend.setSenderHost(selfGroup);
		ipmsgSend.setCommandNo(IpMessageConst.IPMSG_BR_EXIT);	//下线命令
		ipmsgSend.setAdditionalSection(selfName + "\0" + selfGroup);	//附加信息里加入用户名和分组信息

		InetAddress broadcastAddr;
		try {
			broadcastAddr = InetAddress.getByName("255.255.255.255");	//广播地址
			sendUdpData(ipmsgSend.getProtocolString() + "\0", broadcastAddr, IpMessageConst.PORT);	//发送数据
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(TAG, "noticeOnline()....广播地址有误");
		}

	}

4.发送文字,图片,语音,表情,大文件

  /**
     * 发送文字
     */
    @Override
    protected void sendMessage() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                String content = mEditTextContent.getText().toString();
                if (!receiverIp.equals(getLocalIpAddress())){
                    //发送文字
                    sendAndAddMessage(content);
                }
                tblist.add(getTbub(userName, ChatListViewAdapter.TO_USER_MSG, content, null, null,
                        null, null, null, 0f, ChatConst.COMPLETED));
                sendMessageHandler.sendEmptyMessage(SEND_OK);
                ListViewChatActivity.this.content = content;
                // receriveHandler.sendEmptyMessageDelayed(0, 1000);
            }
        }).start();
    }

    /**
     * 接收文字
     */
    String content = "";

    private void receriveMsgText(final String content) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String message = "回复:" + content;
                ChatMessageBean tbub = new ChatMessageBean();
                tbub.setUserName(userName);
                String time = returnTime();
                tbub.setUserContent(message);
                tbub.setTime(time);
                tbub.setType(ChatListViewAdapter.FROM_USER_MSG);
                tblist.add(tbub);
                sendMessageHandler.sendEmptyMessage(RECERIVE_OK);
                mChatDbManager.insert(tbub);
            }
        }).start();
    }

   
    //发送图片
    private void senImage(String filePath) {

        String[] filePathArray = filePath.split("\0");
        //发送传送文件UDP数据报
        IpMessageProtocol sendPro = new IpMessageProtocol();
        sendPro.setVersion("" + IpMessageConst.VERSION);
        sendPro.setCommandNo(IpMessageConst.IPMSG_SENDMSG | IpMessageConst.IPMSG_FILEATTACHOPT);
        sendPro.setSenderName("自己0");
        sendPro.setSenderHost("cc");
        String msgStr = "";    //发送的消息

        StringBuffer additionInfoSb = new StringBuffer();    //用于组合附加文件格式的sb
        for (String path : filePathArray) {
            File file = new File(path);
            additionInfoSb.append("0:");
            additionInfoSb.append(file.getName() + ":");
            additionInfoSb.append(Long.toHexString(file.length()) + ":");        //文件大小十六进制表示
            additionInfoSb.append(Long.toHexString(file.lastModified()) + ":");    //文件创建时间,现在暂时已最后修改时间替代
            additionInfoSb.append(IpMessageConst.IPMSG_FILE_REGULAR + ":");
            byte[] bt = {0x07};        //用于分隔多个发送文件的字符
            String splitStr = new String(bt);
            additionInfoSb.append(splitStr);
        }

        sendPro.setAdditionalSection(msgStr + "\0" + additionInfoSb.toString() + "\0");

        InetAddress sendto = null;
        try {
            sendto = InetAddress.getByName(receiverIp);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("MyFeiGeChatActivity", "发送地址有误");
        }
        if (sendto != null)
            netThreadHelper.sendUdpData(sendPro.getProtocolString(), sendto, IpMessageConst.PORT);

//        //监听2425端口,准备发送TCP连接请求

        Thread netTcpFileSendThread =  new Thread(new NetTcpFileSendThread(filePathArray));
        netTcpFileSendThread.start();





    }
    //发送语音
    private void senAudio(float seconds,String filePath) {

        String[] filePathArray = filePath.split("\0");
        //发送传送文件UDP数据报
        IpMessageProtocol sendPro = new IpMessageProtocol();
        sendPro.setVersion("" + IpMessageConst.VERSION);
        sendPro.setCommandNo(IpMessageConst.IPMSG_SENDMSG | IpMessageConst.IPMSG_FILEATTACHOPT);
        sendPro.setSenderName("0自己"+seconds);
        sendPro.setSenderHost("cc");
        String msgStr = "";    //发送的消息

        StringBuffer additionInfoSb = new StringBuffer();    //用于组合附加文件格式的sb
        for (String path : filePathArray) {
            File file = new File(path);
            additionInfoSb.append("0:");
            additionInfoSb.append(file.getName() + ":");
            additionInfoSb.append(Long.toHexString(file.length()) + ":");        //文件大小十六进制表示
            additionInfoSb.append(Long.toHexString(file.lastModified()) + ":");    //文件创建时间,现在暂时已最后修改时间替代
            additionInfoSb.append(IpMessageConst.IPMSG_FILE_REGULAR + ":");
            byte[] bt = {0x07};        //用于分隔多个发送文件的字符
            String splitStr = new String(bt);
            additionInfoSb.append(splitStr);
        }

        sendPro.setAdditionalSection(msgStr + "\0" + additionInfoSb.toString() + "\0");

        InetAddress sendto = null;
        try {
            sendto = InetAddress.getByName(receiverIp);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("MyFeiGeChatActivity", "发送地址有误");
        }
        if (sendto != null)
            netThreadHelper.sendUdpData(sendPro.getProtocolString(), sendto, IpMessageConst.PORT);

        //监听2425端口,准备接受TCP连接请求
        Thread netTcpFileSendThread = new Thread(new NetTcpFileSendThread(filePathArray));
        netTcpFileSendThread.start();    //启动线程
    }

    /**
     * 接收图片
     */
    String filePath = "";

    private void receriveImageText(final String filePath) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ChatMessageBean tbub = new ChatMessageBean();
                tbub.setUserName(userName);
                String time = returnTime();
                tbub.setTime(time);
                tbub.setImageLocal(filePath);
                tbub.setType(ChatListViewAdapter.FROM_USER_IMG);
                tblist.add(tbub);
                imageList.add(tblist.get(tblist.size() - 1).getImageLocal());
                imagePosition.put(tblist.size() - 1, imageList.size() - 1);
                sendMessageHandler.sendEmptyMessage(RECERIVE_OK);
                mChatDbManager.insert(tbub);
            }
        }).start();
    }

    /**
     * 发送语音
     */
    @Override
    protected void sendVoice(final float seconds, final String filePath) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!receiverIp.equals(getLocalIpAddress())) {
                    senAudio(seconds, filePath);
                }
                ChatMessageBean tbub = new ChatMessageBean();
                tbub.setUserName(userName);
                String time = returnTime();
                tbub.setTime(time);
                tbub.setUserVoiceTime(seconds);
                tbub.setUserVoicePath(filePath);
                tbAdapter.unReadPosition.add(tblist.size() + "");
                tbub.setType(ChatListViewAdapter.TO_USER_VOICE);
                tblist.add(tbub);
//                tblist.add(getTbub(userName, ChatListViewAdapter.TO_USER_VOICE, null, null, null, null, filePath,
//                        null, seconds, ChatConst.SENDING));
                sendMessageHandler.sendEmptyMessage(SEND_OK);
                ListViewChatActivity.this.seconds = seconds;
                voiceFilePath = filePath;
                 //receriveHandler.sendEmptyMessageDelayed(2, 0);
            }
        }).start();
    }

    /**
     * 接收语音
     */
    float seconds = 0.0f;
    String voiceFilePath = "";

    private void receriveVoiceText(final float seconds, final String filePath) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ChatMessageBean tbub = new ChatMessageBean();
                tbub.setUserName(userName);
                String time = returnTime();
                tbub.setTime(time);
                tbub.setUserVoiceTime(seconds);
                tbub.setUserVoicePath(filePath);
                tbAdapter.unReadPosition.add(tblist.size() + "");
                tbub.setType(ChatListViewAdapter.FROM_USER_VOICE);
                tblist.add(tbub);
                sendMessageHandler.sendEmptyMessage(RECERIVE_OK);
                mChatDbManager.insert(tbub);
            }
        }).start();
    }

熬夜不容易,需要点击右侧:点我下载

  • 5
    点赞
  • 17
    收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论 2

打赏作者

码大大

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值