您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java基于TCP怎么實(shí)現(xiàn)簡(jiǎn)單聊天程序”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java基于TCP怎么實(shí)現(xiàn)簡(jiǎn)單聊天程序”吧!
要實(shí)現(xiàn)TCP通信需要?jiǎng)?chuàng)建一個(gè)服務(wù)器端程序和一個(gè)客戶端程序,為了保證數(shù)據(jù)傳輸?shù)陌踩?,首先需要?shí)現(xiàn)服務(wù)器端程序,然后在編寫客戶端程序。
在本機(jī)運(yùn)行服務(wù)器端程序,在遠(yuǎn)程機(jī)運(yùn)行客戶端程序
本機(jī)的IP地址:192.168.129.222
遠(yuǎn)程機(jī)的IP地址:192.168.214.213
在net.hw.network包里創(chuàng)建Server類
package net.hw.network; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * 功能:服務(wù)器端 * 作者:華衛(wèi) * 日期:2022年03月18日 */ public class Server extends JFrame { static final int PORT = 8136; static final String HOST_IP = "192.168.129.222"; private JPanel panel1, panel2; private JTextArea txtContent, txtInput, txtInputIP; private JScrollPane panContent, panInput; private JButton btnClose, btnSend; private ServerSocket serverSocket; private Socket socket; private DataInputStream netIn; private DataOutputStream netOut; public static void main(String[] args) { new Server(); } public Server() { super("服務(wù)器"); //創(chuàng)建組件 panel1 = new JPanel(); panel2 = new JPanel(); txtContent = new JTextArea(15, 60); txtInput = new JTextArea(3, 60); panContent = new JScrollPane(txtContent, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panInput = new JScrollPane(txtInput, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); btnClose = new JButton("關(guān)閉"); btnSend = new JButton("發(fā)送"); //添加組件 getContentPane().add(panContent, "Center"); getContentPane().add(panel1, "South"); panel1.setLayout(new GridLayout(0, 1)); panel1.add(panInput); panel1.add(panel2); panel2.add(btnSend); panel2.add(btnClose); //設(shè)置組件屬性 txtContent.setEditable(false); txtContent.setFont(new Font("宋體", Font.PLAIN, 13)); txtInput.setFont(new Font("宋體", Font.PLAIN, 15)); txtContent.setLineWrap(true); txtInput.setLineWrap(true); txtInput.requestFocus(); setSize(450, 350); setLocation(50, 200); setResizable(false); setVisible(true); //等候客戶請(qǐng)求 try { txtContent.append("服務(wù)器已啟動(dòng)...\n"); serverSocket = new ServerSocket(PORT); txtContent.append("等待客戶請(qǐng)求...\n"); socket = serverSocket.accept(); txtContent.append("連接一個(gè)客戶。\n" + socket + "\n"); netIn = new DataInputStream(socket.getInputStream()); netOut = new DataOutputStream(socket.getOutputStream()); } catch (IOException e1) { e1.printStackTrace(); } / //注冊(cè)監(jiān)聽器,編寫事件代碼 txtContent.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayClientMsg(); } }); txtInput.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayClientMsg(); } }); panel2.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayClientMsg(); } }); txtInput.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { displayClientMsg(); } }); txtInput.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { displayClientMsg(); } }); btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String serverMsg = txtInput.getText(); if (!serverMsg.trim().equals("")) { txtContent.append("服務(wù)器>" + serverMsg + "\n"); netOut.writeUTF(serverMsg); } else { JOptionPane.showMessageDialog(null, "不能發(fā)送空信息!", "服務(wù)器", JOptionPane.WARNING_MESSAGE); } txtInput.setText(""); txtInput.requestFocus(); } catch (IOException ie) { ie.printStackTrace(); } } }); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { netIn.close(); netOut.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { netIn.close(); netOut.close(); socket.close(); serverSocket.close(); } catch (IOException ie) { ie.printStackTrace(); } System.exit(0); } public void windowActivated(WindowEvent e) { txtInput.requestFocus(); } }); } //顯示客戶端信息 void displayClientMsg() { try { if (netIn.available() > 0) { String clientMsg = netIn.readUTF(); txtContent.append("客戶端>" + clientMsg + "\n"); } } catch (IOException e1) { e1.printStackTrace(); } } }
在net.hw.network包里創(chuàng)建Client類
package net.hw.network; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; /** * 功能:客戶端 * 作者:華衛(wèi) * 日期:2022年03月18日 */ public class Client extends JFrame { private JPanel panel1, panel2; private JTextArea txtContent, txtInput; private JScrollPane panContent, panInput; private JButton btnClose, btnSend; private Socket socket; private DataInputStream netIn; private DataOutputStream netOut; public static void main(String[] args) { new Client(); } public Client() { super("客戶端"); //創(chuàng)建組件 panel1 = new JPanel(); panel2 = new JPanel(); txtContent = new JTextArea(15, 60); txtInput = new JTextArea(3, 60); panContent = new JScrollPane(txtContent, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panInput = new JScrollPane(txtInput, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); btnClose = new JButton("關(guān)閉"); btnSend = new JButton("發(fā)送"); //添加組件 getContentPane().add(panContent, "Center"); getContentPane().add(panel1, "South"); panel1.setLayout(new GridLayout(0, 1)); panel1.add(panInput); panel1.add(panel2); panel2.add(btnSend); panel2.add(btnClose); //設(shè)置組件屬性 txtContent.setEditable(false); txtContent.setFont(new Font("宋體", Font.PLAIN, 13)); txtInput.setFont(new Font("宋體", Font.PLAIN, 15)); txtContent.setLineWrap(true); txtInput.setLineWrap(true); txtInput.requestFocus(); setSize(450, 350); setLocation(550, 200); setResizable(false); setVisible(true); //連接服務(wù)器 try { txtContent.append("連接服務(wù)器...\n"); socket = new Socket(Server.HOST_IP, Server.PORT); txtContent.append("連接服務(wù)器成功。\n" + socket + "\n"); netIn = new DataInputStream(socket.getInputStream()); netOut = new DataOutputStream(socket.getOutputStream()); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "服務(wù)器連接失?。n請(qǐng)先啟動(dòng)服務(wù)器程序!", "客戶端", JOptionPane.ERROR_MESSAGE); System.exit(1); } / //注冊(cè)監(jiān)聽器,編寫事件代碼 txtContent.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayServerMsg(); } }); txtInput.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayServerMsg(); } }); panel2.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { displayServerMsg(); } }); txtInput.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { displayServerMsg(); } }); txtInput.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { displayServerMsg(); } }); btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String clientMsg = txtInput.getText(); if (!clientMsg.trim().equals("")) { netOut.writeUTF(clientMsg); txtContent.append("客戶端>" + clientMsg + "\n"); } else { JOptionPane.showMessageDialog(null, "不能發(fā)送空信息!", "客戶端", JOptionPane.WARNING_MESSAGE); } txtInput.setText(""); txtInput.requestFocus(); } catch (IOException ie) { ie.printStackTrace(); } } }); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { netIn.close(); netOut.close(); socket.close(); } catch (IOException ie) { ie.printStackTrace(); } System.exit(0); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { netIn.close(); netOut.close(); socket.close(); } catch (IOException ie) { ie.printStackTrace(); } System.exit(0); } public void windowActivated(WindowEvent e) { txtInput.requestFocus(); } }); } //顯示服務(wù)端信息 void displayServerMsg() { try { if (netIn.available() > 0) { String serverMsg = netIn.readUTF(); txtContent.append("服務(wù)器>" + serverMsg + "\n"); } } catch (IOException e1) { e1.printStackTrace(); } } }
在本機(jī)[192.168.129.222]上啟動(dòng)服務(wù)器端
在遠(yuǎn)程機(jī)[192.168.214.213]上啟動(dòng)客戶端
顯示連接服務(wù)器[192.168.129.222]成功,切換到服務(wù)器端查看,顯示連接了一個(gè)客戶[192.168.214.213]
此時(shí),服務(wù)器端和客戶端就可以相互通信了
其實(shí),很多服務(wù)器端程序都是允許被多個(gè)應(yīng)用程序訪問的,例如門戶網(wǎng)站可以被多個(gè)用戶同時(shí)訪問,因此服務(wù)器端都是多線程的。
感謝各位的閱讀,以上就是“Java基于TCP怎么實(shí)現(xiàn)簡(jiǎn)單聊天程序”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java基于TCP怎么實(shí)現(xiàn)簡(jiǎn)單聊天程序這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。