利用Java编写聊天程序

利用Java编写聊天程序

  • 首先计算机网络有两种传输层协议:TCP(面向连接),UDP(面向无连接)。今天就介绍基于这两种协议的聊天程序。

先查明自己电脑的主机名

   右键我的电脑-->属性

这里写图片描述

一、基于UDP的聊天程序

1.基于UDP的发送端

package cn.com;

/**
 * 基于UDP
 * 聊天发送端
 */
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Send {

    public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        DatagramSocket ds = new DatagramSocket();
        Scanner sc = new Scanner(System.in);
        String line = null;
        while ((line = sc.nextLine()) != null) {
            byte[] buf = line.getBytes();
            int length = buf.length;
            InetAddress address = InetAddress.getByName("1-PC21");//主机名
            DatagramPacket dp = new DatagramPacket(buf, length, address, 10086);   //10086为自己设置的端口号
            ds.send(dp);
        }
        sc.close();
    }

}

2.基于UDP的接收端

package cn.com;

/**
 * 基于UDP
 * 聊天接收端
 */
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive {

    public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        DatagramSocket ds = new DatagramSocket(10086);//端口号需一致

        while (true) {
            byte[] b = new byte[1024 * 1];
            int length = b.length;
            DatagramPacket dp = new DatagramPacket(b, length);
            ds.receive(dp);

            byte[] data = dp.getData();
            int length2 = dp.getLength();
            String hostAddress = dp.getAddress().getHostAddress();
            String s = new String(data, 0, length2);
            System.out.println(s + "来自" + hostAddress);
        }

    }

}

3.先运行接收端,在运行发送端

  • 发送端发送“Hello World”,“My name is Tom”。
    这里写图片描述
  • 接收端收到信息
    这里写图片描述

二、基于TCP的聊天程序

1.客户端

package cn.com;
/**
 * 基于TCP
 * 聊天系统客户端
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) throws IOException {
        InetAddress address = InetAddress.getByName("1-PC21");//主机名
        int port = 10089;

        Scanner sc = new Scanner(System.in);
        String line = null;
        while ((line = sc.nextLine()) != null) {
            @SuppressWarnings("resource")
            Socket socket = new Socket(address, port);   //socket要在循环体中定义
            OutputStream os = socket.getOutputStream();
            os.write(line.getBytes());

            // 客户端接收服务端返回的消息(输入流)
            InputStream is = socket.getInputStream();
            byte[] b = new byte[1024 * 1];
            int len = is.read(b);
            String s = new String(b, 0, len);
            System.out.println(s);
        }
        sc.close();

    }

}

2.服务端

package cn.com;

/**
 * 基于TCP
 * 聊天系统服务端
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        ServerSocket ss = new ServerSocket(10089);

        while (true) {
            Socket accept = ss.accept();    //循环中用到accept,所以要在循环中新建定义
            InputStream is = accept.getInputStream();
            byte[] b = new byte[1024 * 1];
            int len = is.read(b);
            String s = new String(b, 0, len);
            System.out.println("已接收客户端内容-->" + s);

            // 给客户端返回数据
            OutputStream os = accept.getOutputStream();
            String content = "客户端接收成功";
            os.write(content.getBytes());

            os.close();
            is.close();
            accept.close();
        }

    }

}

3.还是先打开服务端,再打开客户端,发送信息

  • 客户端发送:“今天星期四”,“天气很好” 两条信息。
    这里写图片描述
  • 服务端收到信息:
    这里写图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值