用Java實現(xiàn)聊天程序
利用Java編寫聊天程序,供大家參考,具體內容如下
首先計算機網(wǎng)絡有兩種傳輸層協(xié)議:TCP(面向連接),UDP(面向無連接)。今天就介紹基于這兩種協(xié)議的聊天程序。
先查明自己電腦的主機名
右鍵我的電腦-->屬性
一、基于UDP的聊天程序
1.基于UDP的發(fā)送端
package cn.com; /** ?* 基于UDP ?* 聊天發(fā)送端 ?*/ 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.先運行接收端,在運行發(fā)送端
發(fā)送端發(fā)送“Hello World”,“My name is Tom”。
接收端收到信息
二、基于TCP的聊天程序
1.客戶端
package cn.com; /** ?* 基于TCP ?* 聊天系統(tǒng)客戶端 ?*/ 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要在循環(huán)體中定義 ? ? ? ? ? ? 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 ?* 聊天系統(tǒng)服務端 ?*/ 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(); ? ?//循環(huán)中用到accept,所以要在循環(huán)中新建定義 ? ? ? ? ? ? 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); ? ? ? ? ? ? // 給客戶端返回數(shù)據(jù) ? ? ? ? ? ? OutputStream os = accept.getOutputStream(); ? ? ? ? ? ? String content = "客戶端接收成功"; ? ? ? ? ? ? os.write(content.getBytes()); ? ? ? ? ? ? os.close(); ? ? ? ? ? ? is.close(); ? ? ? ? ? ? accept.close(); ? ? ? ? } ? ? } }
3.還是先打開服務端,再打開客戶端,發(fā)送信息
客戶端發(fā)送:“今天星期四”,“天氣很好” 兩條信息。
服務端收到信息:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Javascript和Java語言有什么關系?兩種語言間的異同比較
雖然Javascript與Java有緊密的聯(lián)系,但卻是兩個公司開發(fā)的不同的兩個產(chǎn)品。那么js和java有什么關系,兩種語言的不同點是什么呢?介于這兩個問題,小編一起給大家解答下2016-09-09解決在for循環(huán)中remove list報錯越界的問題
這篇文章主要介紹了解決在for循環(huán)中remove list報錯越界的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12mybatis的mapper特殊字符轉移及動態(tài)SQL條件查詢小結
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會報錯的需要轉移一下,本文給大家介紹了常見的條件查詢操作,對mybatis的mapper特殊字符及動態(tài)SQL條件查詢相關知識感興趣的朋友一起看看吧2021-09-09