用Java實現(xiàn)聊天程序
利用Java編寫聊天程序,供大家參考,具體內(nèi)容如下
首先計算機網(wǎng)絡(luò)有兩種傳輸層協(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為自己設(shè)置的端口號
? ? ? ? ? ? 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());
? ? ? ? ? ? // 客戶端接收服務(wù)端返回的消息(輸入流)
? ? ? ? ? ? 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.服務(wù)端
package cn.com;
/**
?* 基于TCP
?* 聊天系統(tǒng)服務(wù)端
?*/
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("已接收客戶端內(nèi)容-->" + s);
? ? ? ? ? ? // 給客戶端返回數(shù)據(jù)
? ? ? ? ? ? OutputStream os = accept.getOutputStream();
? ? ? ? ? ? String content = "客戶端接收成功";
? ? ? ? ? ? os.write(content.getBytes());
? ? ? ? ? ? os.close();
? ? ? ? ? ? is.close();
? ? ? ? ? ? accept.close();
? ? ? ? }
? ? }
}3.還是先打開服務(wù)端,再打開客戶端,發(fā)送信息
客戶端發(fā)送:“今天星期四”,“天氣很好” 兩條信息。

服務(wù)端收到信息:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java中UDP簡單聊天程序?qū)嵗a
- 詳解基于java的Socket聊天程序——客戶端(附demo)
- java網(wǎng)絡(luò)編程學(xué)習(xí)java聊天程序代碼分享
- java基于TCP協(xié)議實現(xiàn)聊天程序
- java基于C/S模式實現(xiàn)聊天程序(客戶端)
- 詳解基于java的Socket聊天程序——服務(wù)端(附demo)
- java實現(xiàn)基于Tcp的socket聊天程序
- 詳解基于java的Socket聊天程序——初始設(shè)計(附demo)
- java實現(xiàn)簡單TCP聊天程序
- 基于Java的Socket多客戶端Client-Server聊天程序的實現(xiàn)
相關(guān)文章
java動態(tài)構(gòu)建數(shù)據(jù)庫復(fù)雜查詢教程
這篇文章主要介紹了java動態(tài)構(gòu)建數(shù)據(jù)庫復(fù)雜查詢的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11
SpringBoot+VUE實現(xiàn)數(shù)據(jù)表格的實戰(zhàn)
本文將使用VUE+SpringBoot+MybatisPlus,以前后端分離的形式來實現(xiàn)數(shù)據(jù)表格在前端的渲染,具有一定的參考價值,感興趣的可以了解一下2021-08-08
SpringBoot啟動多數(shù)據(jù)源找不到合適的驅(qū)動類問題
這篇文章主要介紹了SpringBoot啟動多數(shù)據(jù)源找不到合適的驅(qū)動類問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

