1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      JAVA聊天服務(wù)器代碼 java服務(wù)端編程

      為java聊天室代碼加詳細注釋,并說明設(shè)計思路。好的加100分。

      import java.io.*;

      創(chuàng)新互聯(lián)建站專注骨干網(wǎng)絡(luò)服務(wù)器租用十載,服務(wù)更有保障!服務(wù)器租用,托管服務(wù)器 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

      import java.net.*;

      import java.awt.*;

      import javax.swing.*;

      import java.awt.event.*;//引入包。

      public class ChatClient {

      public static void main(String[] args) {

      ChatClient cc = new ChatClient();

      cc.receive();

      }

      JTextField jtf; // 文本條

      JTextArea jta; //文本域。

      Socket s; //客戶端

      PrintWriter out; //輸出流

      BufferedReader in; //輸入流

      public ChatClient() {

      JFrame frame = new JFrame("ChatClient");//窗口

      frame.setSize(400, 300); //大小

      jta = new JTextArea(); //文本域

      jta.setEditable(false); //不可編輯

      jtf = new JTextField();//文件條

      jtf.addActionListener(new ActionListener() { //添加監(jiān)聽。

      public void actionPerformed(ActionEvent arg0) {

      send(); //調(diào)用send()方法

      }

      });

      frame.getContentPane().add(new JScrollPane(jta)); //添加滾動條

      frame.getContentPane().add(jtf, "South"); //添加文本條

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口。

      frame.setVisible(true); //可顯示的。

      try {

      s = new Socket("127.0.0.1", 9000); //連接服務(wù)端 socket("主機名",端口號);

      in = new BufferedReader(new InputStreamReader(s.getInputStream())); //建立輸入流

      out = new PrintWriter(s.getOutputStream());//輸出流

      } catch (UnknownHostException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      }

      public void receive() { //接受服務(wù)端發(fā)來別的客戶端的信息。

      while (true) {

      try {

      String text = in.readLine(); //讀一行

      this.jta.append(text + "\n"); //jta 添加上讀入的。

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      return;

      }

      }

      }

      public void send() { //發(fā)送消息

      String text = this.jtf.getText(); //得到你輸入的消息

      this.jtf.setText(""); //在文本域中顯示你輸入的消息。

      out.println(text); //打印出。

      out.flush(); //清空

      }

      }

      Server端

      import java.net.*;

      import java.io.*;

      import java.util.*;//引入包

      public class ChatServer {

      public static void main(String[] args) throws Exception {

      ServerSocket ss = new ServerSocket(9000); //建立服務(wù)端,端口號為9000

      List list = new ArrayList(); //創(chuàng)建個List集合。

      while (true) {

      Socket s = ss.accept(); //等待客戶端的請求。

      list.add(s); //把每一個client都add到集合中去。

      Thread t = new ServerThread(s, list); //線程。

      t.start(); //啟動。

      }

      }

      }

      class ServerThread extends Thread {

      Socket s;

      List list;

      BufferedReader in;

      PrintWriter out;

      public ServerThread(Socket s, List list) { //構(gòu)造。傳入socket和list。

      this.s = s;

      this.list = list;

      try {

      in = new BufferedReader(new InputStreamReader(s.getInputStream())); //輸入流

      out = new PrintWriter(s.getOutputStream()); //輸出流

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      }

      public void run() { //必須實現(xiàn)其run()方法。

      while (true) {

      try {

      String str = in.readLine(); //得到client端的message。

      if (str == null) //如果沒有消息就返回。

      return;

      Iterator it = list.iterator(); //遍歷list。

      while (it.hasNext()) { //如果list有下一個

      Socket socket = (Socket) (it.next()); //因為list中都是存的socket

      PrintWriter o = new PrintWriter(socket.getOutputStream()); //輸出流

      o.println(str); //輸出

      o.flush(); //清空

      }

      } catch (IOException e) {

      // TODO Auto-generated catch block

      // e.printStackTrace();

      return;

      }

      }

      }

      }

      編一個簡單的java聊天程序,但是運行時服務(wù)器和客戶端都出錯

      給你簡單修改了一下,你看看吧

      import java.io.*;

      import java.net.*;

      import java.util.*;

      import java.text.*;

      public class ChatClient {

      public static void main(String[] args) throws Exception {

      Socket c = new Socket("127.0.0.1", 1123);

      while (true) {

      Date now = new Date();

      DateFormat d = DateFormat.getDateTimeInstance();

      String time = d.format(now);

      PrintWriter OutputS = new PrintWriter(c.getOutputStream());

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

      String Csay = say.readLine();

      System.out.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);

      OutputS.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);

      OutputS.flush();

      BufferedReader InputS = new BufferedReader(new InputStreamReader(c.getInputStream()));

      System.out.println(InputS.readLine());

      }

      }

      }

      、、、、、、、、、、、、、、、、

      import java.io.*;

      import java.net.*;

      import java.text.*;

      import java.util.*;

      public class ChatServer {

      private static int PORTNUM = 1123;// 定義程序使用的端口號PORTNUM

      public static void main(String args[]) throws Exception {

      ServerSocket s = new ServerSocket(PORTNUM);

      while (true) {

      Socket s1 = s.accept();

      BufferedReader InputS = new BufferedReader(new InputStreamReader(s1.getInputStream()));

      System.err.println();

      System.out.println(InputS.readLine());

      Date now = new Date();

      DateFormat d = DateFormat.getDateTimeInstance();

      String time = d.format(now);

      PrintWriter OutputS = new PrintWriter(s1.getOutputStream());

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

      String Ssay = say.readLine();

      OutputS.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);

      OutputS.flush();

      System.out.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);

      }

      }

      }

      說一下你的程序出現(xiàn)的原因吧!

      你的客戶端與程序段在開始運行都在監(jiān)聽對方發(fā)來的消息,當什么都不會有了,因為對方都在等著接受消息呢,這樣就造成了死鎖

      java 聊天室 源代碼

      【ClientSocketDemo.java 客戶端Java源代碼】

      import java.net.*;

      import java.io.*;

      public class ClientSocketDemo

      {

      //聲明客戶端Socket對象socket

      Socket socket = null;

      //聲明客戶器端數(shù)據(jù)輸入輸出流

      DataInputStream in;

      DataOutputStream out;

      //聲明字符串數(shù)組對象response,用于存儲從服務(wù)器接收到的信息

      String response[];

      //執(zhí)行過程中,沒有參數(shù)時的構(gòu)造方法,本地服務(wù)器在本地,取默認端口10745

      public ClientSocketDemo()

      {

      try

      {

      //創(chuàng)建客戶端socket,服務(wù)器地址取本地,端口號為10745

      socket = new Socket("localhost",10745);

      //創(chuàng)建客戶端數(shù)據(jù)輸入輸出流,用于對服務(wù)器端發(fā)送或接收數(shù)據(jù)

      in = new DataInputStream(socket.getInputStream());

      out = new DataOutputStream(socket.getOutputStream());

      //獲取客戶端地址及端口號

      String ip = String.valueOf(socket.getLocalAddress());

      String port = String.valueOf(socket.getLocalPort());

      //向服務(wù)器發(fā)送數(shù)據(jù)

      out.writeUTF("Hello Server.This connection is from client.");

      out.writeUTF(ip);

      out.writeUTF(port);

      //從服務(wù)器接收數(shù)據(jù)

      response = new String[3];

      for (int i = 0; i response.length; i++)

      {

      response[i] = in.readUTF();

      System.out.println(response[i]);

      }

      }

      catch(UnknownHostException e){e.printStackTrace();}

      catch(IOException e){e.printStackTrace();}

      }

      //執(zhí)行過程中,有一個參數(shù)時的構(gòu)造方法,參數(shù)指定服務(wù)器地址,取默認端口10745

      public ClientSocketDemo(String hostname)

      {

      try

      {

      //創(chuàng)建客戶端socket,hostname參數(shù)指定服務(wù)器地址,端口號為10745

      socket = new Socket(hostname,10745);

      in = new DataInputStream(socket.getInputStream());

      out = new DataOutputStream(socket.getOutputStream());

      String ip = String.valueOf(socket.getLocalAddress());

      String port = String.valueOf(socket.getLocalPort());

      out.writeUTF("Hello Server.This connection is from client.");

      out.writeUTF(ip);

      out.writeUTF(port);

      response = new String[3];

      for (int i = 0; i response.length; i++)

      {

      response[i] = in.readUTF();

      System.out.println(response[i]);

      }

      }

      catch(UnknownHostException e){e.printStackTrace();}

      catch(IOException e){e.printStackTrace();}

      }

      //執(zhí)行過程中,有兩個個參數(shù)時的構(gòu)造方法,第一個參數(shù)hostname指定服務(wù)器地址

      //第一個參數(shù)serverPort指定服務(wù)器端口號

      public ClientSocketDemo(String hostname,String serverPort)

      {

      try

      {

      socket = new Socket(hostname,Integer.parseInt(serverPort));

      in = new DataInputStream(socket.getInputStream());

      out = new DataOutputStream(socket.getOutputStream());

      String ip = String.valueOf(socket.getLocalAddress());

      String port = String.valueOf(socket.getLocalPort());

      out.writeUTF("Hello Server.This connection is from client.");

      out.writeUTF(ip);

      out.writeUTF(port);

      response = new String[3];

      for (int i = 0; i response.length; i++)

      {

      response[i] = in.readUTF();

      System.out.println(response[i]);

      }

      }

      catch(UnknownHostException e){e.printStackTrace();}

      catch(IOException e){e.printStackTrace();}

      }

      public static void main(String[] args)

      {

      String comd[] = args;

      if(comd.length == 0)

      {

      System.out.println("Use localhost(127.0.0.1) and default port");

      ClientSocketDemo demo = new ClientSocketDemo();

      }

      else if(comd.length == 1)

      {

      System.out.println("Use default port");

      ClientSocketDemo demo = new ClientSocketDemo(args[0]);

      }

      else if(comd.length == 2)

      {

      System.out.println("Hostname and port are named by user");

      ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

      }

      else System.out.println("ERROR");

      }

      }

      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      【ServerSocketDemo.java 服務(wù)器端Java源代碼】

      import java.net.*;

      import java.io.*;

      public class ServerSocketDemo

      {

      //聲明ServerSocket類對象

      ServerSocket serverSocket;

      //聲明并初始化服務(wù)器端監(jiān)聽端口號常量

      public static final int PORT = 10745;

      //聲明服務(wù)器端數(shù)據(jù)輸入輸出流

      DataInputStream in;

      DataOutputStream out;

      //聲明InetAddress類對象ip,用于獲取服務(wù)器地址及端口號等信息

      InetAddress ip = null;

      //聲明字符串數(shù)組對象request,用于存儲從客戶端發(fā)送來的信息

      String request[];

      public ServerSocketDemo()

      {

      request = new String[3]; //初始化字符串數(shù)組

      try

      {

      //獲取本地服務(wù)器地址信息

      ip = InetAddress.getLocalHost();

      //以PORT為服務(wù)端口號,創(chuàng)建serverSocket對象以監(jiān)聽該端口上的連接

      serverSocket = new ServerSocket(PORT);

      //創(chuàng)建Socket類的對象socket,用于保存連接到服務(wù)器的客戶端socket對象

      Socket socket = serverSocket.accept();

      System.out.println("This is server:"+String.valueOf(ip)+PORT);

      //創(chuàng)建服務(wù)器端數(shù)據(jù)輸入輸出流,用于對客戶端接收或發(fā)送數(shù)據(jù)

      in = new DataInputStream(socket.getInputStream());

      out = new DataOutputStream(socket.getOutputStream());

      //接收客戶端發(fā)送來的數(shù)據(jù)信息,并顯示

      request[0] = in.readUTF();

      request[1] = in.readUTF();

      request[2] = in.readUTF();

      System.out.println("Received messages form client is:");

      System.out.println(request[0]);

      System.out.println(request[1]);

      System.out.println(request[2]);

      //向客戶端發(fā)送數(shù)據(jù)

      out.writeUTF("Hello client!");

      out.writeUTF("Your ip is:"+request[1]);

      out.writeUTF("Your port is:"+request[2]);

      }

      catch(IOException e){e.printStackTrace();}

      }

      public static void main(String[] args)

      {

      ServerSocketDemo demo = new ServerSocketDemo();

      }

      }


      分享文章:JAVA聊天服務(wù)器代碼 java服務(wù)端編程
      文章路徑:http://ef60e0e.cn/article/doigpch.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        垣曲县| 襄城县| 阿尔山市| 天全县| 稷山县| 砚山县| 阿巴嘎旗| 荣昌县| 武山县| 从化市| 江源县| 阜阳市| 灵璧县| 晋州市| 蓝田县| 德州市| 汾西县| 龙川县| 渭南市| 高雄县| 房产| 内乡县| 微博| 洛南县| 阿克陶县| 城步| 河池市| 林芝县| 高安市| 苍梧县| 德昌县| 葵青区| 舞钢市| 双峰县| 新津县| 理塘县| 丹寨县| 崇义县| 永济市| 胶州市| 苍南县|