程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]051、數據報通訊:聊天吧

[JAVA100例]051、數據報通訊:聊天吧

編輯:關於JAVA

//文件名:ChatClient.java
import java.net.*;
import java.io.*;
/**
* <p>Title: 網絡聊天吧</p>
* <p>Description: 這是一個使用數據報通訊方式的聊天程序的客戶端</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ChatClient.java</p>
* @version 1.0
*/
public class ChatClient{
private DatagramSocket s;
private InetAddress hostAddress;
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf,buf.length);
/**
*<br>方法說明:構造器,這裡實現接收用戶輸入和與服務器通訊
*<br>輸入參數:
*<br>返回類型:
*/
 public ChatClient(){
  try{
    //使用構造器,創建使用本機任何可用端口的數據包Socket
    s = new DatagramSocket();
    //獲取本地IP
    hostAddress = InetAddress.getByName("localhost");
    System.out.println("Client start............");
    while(true){
     String outMessage ="";
    //讀取輸入
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     try{
      outMessage = stdin.readLine();
     }catch(IOException ie){
      System.err.println("IO error!");
     }
     //如果輸入“bye”則表示退出程序
     if(outMessage.equals("bye")) break;
     String outString = "Client say: "+ outMessage;
     byte[] buf = outString.getBytes();
     //打包數據,發送數據
     DatagramPacket out = new DatagramPacket(buf,buf.length,hostAddress,ChatServer.PORT);
     s.send(out);
     //等待服務器返回
     s.receive(dp);
     String rcvd = "rcvd from "+ dp.getAddress() + ", " + dp.getPort() +
     ": "+ new String(dp.getData(),0,dp.getLength());
     System.out.println(rcvd);

   }
   }catch(UnknownHostException e){
    System.out.println("Can;t open socket");
    System.exit(1);
   }catch(SocketException e){
    System.out.println("Can;t open socket");
    e.printStackTrace();
    System.exit(1);
   }catch(IOException e){
    System.err.println("Communication error");
    e.printStackTrace();
    System.exit(1);
   }catch(Exception e){
    System.err.println("Communication error");
    e.printStackTrace();
    System.exit(1);
   }
   System.out.println("ChatClient over");
}
/**
*<br>方法說明:主方法
*<br>輸入參數:
*<br>返回類型:
*/
public static void main(String[] args){
  new ChatClient();
}
}
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: 網絡聊天吧</p>
* <p>Description: 使用數據報創建的聊天服務器</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ChatServer.java</p>
* @version 1.0
*/
public class ChatServer{
static final int PORT = 4000;//設置服務端口
private byte[] buf = new byte[1000];
private DatagramPacket dgp =new DatagramPacket(buf,buf.length);
private DatagramSocket sk;
/**
*<br>方法說明:服務端構造器,實現讀取用戶輸入和通訊
*<br>輸入參數:
*<br>返回類型:
*/
public ChatServer(){
  try{
   //實例化數據報
   sk = new DatagramSocket(PORT);
   System.out.println("Server start.................");
   while(true){
    //等待接收
    sk.receive(dgp);
    //獲取接收信息
    String rcvd = new String(dgp.getData(),0,dgp.getLength())+
     ", from address: "+ dgp.getAddress()+
     ", port: "+ dgp.getPort();
    System.out.println(rcvd);
    String outMessage ="";
    //讀取輸入
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     try{
      outMessage = stdin.readLine();
     }catch(IOException ie){
      System.err.println("IO error!");
     }
    String outString = "Server say: "+ outMessage;
    //拷貝字符到緩存
    byte[] buf = outString.getBytes();
    //打包數據,發送回信息。
    DatagramPacket out = new DatagramPacket(buf,buf.length,dgp.getAddress(),dgp.getPort());
    sk.send(out);
   }
  }catch(SocketException e){
   System.err.println("Can´t open socket");
   System.exit(1);
  }catch(IOException e){
   System.err.println("Communication error");
   e.printStackTrace();
   System.exit(1);
  }
}
public static void main(String[] args){
  new ChatServer();
}
}

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