本文為大家分享了java實現(xiàn)簡單聊天軟件的具體代碼,供大家參考,具體內(nèi)容如下
為華容等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及華容網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、華容網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
介紹
這是我自己學(xué)習(xí)Socket時寫的,用到了多線程,Socket的知識,還有Java的IO技術(shù),并且做了一個Java的UI,廢話不多說,先上效果圖
SendThread
這個類是用來實現(xiàn)數(shù)據(jù)的發(fā)送的,當(dāng)按鍵按下時,會調(diào)用sendMessage方法
public class SendThread { ChatWindow chatWindow; private String remoteIP = ""; private int port = 0; private String message = ""; public SendThread(int port,ChatWindow window) { chatWindow = window; } public void notRun() { InetSocketAddress isa = new InetSocketAddress(remoteIP, port); try { Socket socket = new Socket(); socket.connect(isa); OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream()); writer.write(message); writer.flush(); writer.close(); System.out.println("將數(shù)據(jù)寫入到流中"); } catch (IOException e) { e.printStackTrace(); }finally{ message = ""; } } public void senMessage(String host,int port,String message){ remoteIP = host; this.port = port; this.message = message; notRun(); } }
ReceiveThread
這個類實現(xiàn)的功能是維護一個阻塞線程,來對端口進行監(jiān)聽,接受收到的信息,每當(dāng)收到信息時,都會顯示。
public class ReceiveThread extends Thread { private ServerSocket server; private ChatWindow window; public ReceiveThread(ChatWindow window) { this.window = window; try { server = new ServerSocket(0); window.setLocalPort(server.getLocalPort()); start(); } catch (IOException e) { // TODO Auto-generated catch block window.printError("連接出錯"); } } @Override public void run() { // TODO Auto-generated method stub while(true){ Socket socket; try { socket = server.accept(); InputStreamReader reader = new InputStreamReader(socket.getInputStream()); int c; StringBuilder sb = new StringBuilder(); while((c = reader.read()) != -1){ sb.append((char)c); } window.setReceive(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("線程將接受到的數(shù)據(jù)寫入對話框出錯"); } } } }
ChatWindow
這個類是UI的實現(xiàn),通過綁定各個按鈕的操作來實現(xiàn)別的功能
public class ChatWindow extends JFrame { private JButton sendButton; private JButton cancelButton; private JButton receiveButton; private JTextArea receiveText; private JTextField sendText; private JLabel localPort; private JTextField remoteAddress; private JTextField remotePort; private SendThread sendThread; private ReceiveThread receiveThread; private void GUIini(){ Container c = this.getContentPane(); c.setLayout(new BorderLayout()); setSize(400, 500); //第一個panel JPanel panel1 = new JPanel(new GridLayout(4, 2)); // JPanel panel1 = new JPanel(new (4, 2)); panel1.setSize(400, 75); panel1.add(new JLabel("當(dāng)前機器的IP地址是:")); try { panel1.add(new JLabel(InetAddress.getLocalHost().getHostAddress())); } catch (UnknownHostException e) { // TODO Auto-generated catch block panel1.add(new JLabel("當(dāng)前機器的IP地址是:" + "Unknown")); } panel1.add(new JLabel("當(dāng)前對話的端口是:")); panel1.add(localPort = new JLabel(""+0)); panel1.add(new JLabel("Remote Host IP Address:")); remoteAddress = new JTextField(); remoteAddress.setColumns(0); panel1.add(remoteAddress); panel1.add(new JLabel("Remote Host Port:")); remotePort = new JTextField(); remotePort.setColumns(0); panel1.add(remotePort); c.add(panel1,BorderLayout.NORTH); //第二個panel JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)); panel2.setSize(400,200); panel2.add(new JLabel("收到的內(nèi)容")); receiveText = new JTextArea(15,30); receiveText.setEditable(false); receiveText.setAutoscrolls(true); JScrollPane jsp = new JScrollPane(receiveText); panel2.add(jsp); //第三個panel // JPanel panel3 = new JPanel(new GridLayout(2, 1)); panel2.add(new JLabel("請輸入發(fā)送的內(nèi)容")); sendText = new JTextField(30); sendText.setAutoscrolls(true); panel2.add(sendText); c.add(panel2,BorderLayout.CENTER); //c.add(panel3); //第四個panel JPanel panel4 = new JPanel(new GridLayout(1, 0)); panel4.setSize(400,20); receiveButton = new JButton("開始接受數(shù)據(jù)"); sendButton = new JButton("發(fā)送"); cancelButton = new JButton("取消"); panel4.add(receiveButton); panel4.add(cancelButton); panel4.add(sendButton); c.add(panel4,BorderLayout.SOUTH); //四個面板內(nèi)容設(shè)置完畢 setVisible(true); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void ActionIni() { //鍵盤動作 this.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode() == KeyEvent.VK_ENTER){ //發(fā)送文本 sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText()); receiveText.setText(receiveText.getText() + "\n" + "發(fā)送:" + sendText.getText()); } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } }); //取消按鈕的動作 cancelButton.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub sendText.setText(""); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); sendButton.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { //發(fā)送文本 sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText()); receiveText.setText(receiveText.getText() + "發(fā)送:" + sendText.getText()+ "\n"); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); } private void ThreadIni() { // TODO Auto-generated method stub sendThread = new SendThread(0, this); receiveThread = new ReceiveThread(this); } //構(gòu)造函數(shù) public ChatWindow() { GUIini(); ActionIni(); ThreadIni(); } public void printError(String err){ System.out.println("Error occur:" + err); } //回調(diào)函數(shù),用于接受從線程中返回的數(shù)據(jù) public void setReceive(String receive){ receiveText.setText(receiveText.getText() + "收到:" + receive+ "\n" ); } //當(dāng)接受數(shù)據(jù)的線程開始工作以后,就調(diào)用該回調(diào)函數(shù),設(shè)置當(dāng)前聊天窗口使用的端口是哪個 public void setLocalPort(int localPortText){ localPort.setText(""+localPortText); } //放遠,這個是整個程序的起點 public static void main(String[] args) { // TODO Auto-generated method stub new ChatWindow(); } }
如何使用
將工程導(dǎo)入到Eclipse中以后,運行兩次,因為這個程序不屬于單例模式,所以可以出現(xiàn)兩個窗口,在遠程端口,遠程地址那個輸入框中分別輸入另一個窗口中顯示的IP和端口號,接下來在輸入框中輸入要發(fā)送的文字就可以了
源碼下載:java實現(xiàn)簡單聊天軟件
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。