程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java收集編程進修java聊天法式代碼分享

java收集編程進修java聊天法式代碼分享

編輯:關於JAVA

java收集編程進修java聊天法式代碼分享。本站提示廣大學習愛好者:(java收集編程進修java聊天法式代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java收集編程進修java聊天法式代碼分享正文



package com.neusoft.edu.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 辦事器端代碼
 * 獲得客戶端發送的信息,顯示而且前往對應的答復
 * 1、創立ServerSocket對象
 * 2、挪用accept辦法獲得客戶端銜接
 * 3、應用輸出流讀取客戶端發送的數據
 * 4、應用輸入流向客戶端寫入數據
 * 5、封閉對應的對象
 * @author L
 *
 */
public class ChatServer {
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            //1、創立ServerSocket對象,8875為自界說端標語
            ServerSocket server = new ServerSocket(8857);

            //簡略提醒
            System.out.println("期待客戶端銜接……");

            //2、獲得客戶端銜接
            Socket client = server.accept();

            //獲得客戶真個相干信息
            System.out.println(client.getInetAddress().getHostAddress() + "銜接下去了……");

            //3.1、界說輸出流和輸入流對象
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            client.getInputStream()));

            //用來獲得從掌握台輸出的數據,將該數據發送給客戶端
            BufferedReader inByServer = new BufferedReader(
                    new InputStreamReader(System.in));

            PrintWriter out = new PrintWriter(client.getOutputStream(), true);

            //讀取到的數據
            String data = null;
            String answer = null;

            //輪回和客戶端停止通訊
            do
            {
                //3.2、讀取客戶端發送的數據
                data = in.readLine();

                //在辦事器端顯示讀取到的數據
                System.out.println("客戶端發送信息:" + data);

                //獲得辦事器端要發送給客戶真個信息
                System.out.print("辦事器端答復客戶端:");
                answer = inByServer.readLine();

                //3.3、將數據寫入到客戶端
                out.println(answer);
                out.flush();
            }while(!"bye".equals(data));

            //4、封閉相干資本
            out.flush();
            in.close();
            inByServer.close();
            out.close();

            //封閉Socket對象
            client.close();
            server.close();

            System.out.println("辦事器端封閉……");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


package com.neusoft.edu.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 聊天客戶端
 * 1、創立Socket對象
 * 2、寫入數據
 * 3、讀取從辦事器端發來的數據
 * 4、封閉
 * @author L
 *
 */
public class ChatClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            //1、創立Socket對象,("192.168.1.107", 8857)分離為主機IP和端標語,兩個類端標語要分歧
            Socket client = new Socket("192.168.1.107", 8857);

            //2.1、創立輸出流和輸入流對象
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            client.getInputStream()));

            PrintWriter out = new PrintWriter(client.getOutputStream(), true);

            BufferedReader inByClient = new BufferedReader(
                    new InputStreamReader(System.in));

            //辦事器端發送的數據
            String data = null;
            //客戶端向辦事器端寫入的數據
            String answer = null;

            do
            {
                //2.2、客戶真個讀寫操作
                System.out.print("你說:");
                //獲得要發送給辦事器真個數據
                answer = inByClient.readLine();
                //向辦事器端寫入數據
                out.println(answer);
                out.flush();

                //獲得辦事器端發送的數據
                data = in.readLine();
                //輸入從辦事器端獲得的數據
                System.out.println("辦事器端前往信息是:" + data);
            }while(!"bye".equals(data));

            //3、封閉
            in.close();
            out.close();
            inByClient.close();
            client.close();

            System.out.println("客戶端封閉……");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved