Java聊天程序
GYN1996 人气:0利用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.还是先打开服务端,再打开客户端,发送信息
客户端发送:“今天星期四”,“天气很好” 两条信息。
服务端收到信息:
加载全部内容