最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Datagram Scoket雙向通信

 更新時(shí)間:2014年04月22日 09:42:01   作者:  
這篇文章主要介紹了Datagram Scoket雙向通信,需要的朋友可以參考下

這里是兩個(gè)人進(jìn)行通信。是根據(jù)ip來判斷的,xp與xp之間沒有問題,我win7和xp有問題(已解決 關(guān)閉防火墻,如果是內(nèi)網(wǎng) 網(wǎng)段要一致)

復(fù)制代碼 代碼如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Me {

 public static void main(String[] args) throws IOException {
  new ReciveThread().start();//配置監(jiān)聽程序 必須放在前面

  new SendInfo().main(args);
 }

}

class SendInfo {

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

  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  String str = null;
  String lines = "";
  while ((str = bf.readLine()) != null) {
   lines += str;
   if (str.equals("ok")) {
    send(lines);
    lines = "";
   }

   if (str.equals("bye")) {
    bf.close(); // 必須加break 否者還會有回車信號 break;
   }

  }

 }

 static void send(String str) {
  // UDP網(wǎng)絡(luò)程序
  DatagramSocket ds = null;
  DatagramPacket dp = null;
  try {
   ds = new DatagramSocket(3000);//打開端口號
  } catch (SocketException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   byte[] ip = new byte[] { (byte) 10, 1, 1, (byte) 200 };
   dp = new DatagramPacket(str.getBytes(), str.length(),
     InetAddress.getByAddress(ip), 9000);//faso
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  try {
   ds.send(dp);
   System.out.println("send success");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  ds.close();
 }
}
class ReciveThread extends Thread {

 public void run() {
  while (true) {
   DatagramSocket ds = null;
   byte[] buf = new byte[1024];
   DatagramPacket dp = null;

   try {
    ds = new DatagramSocket(9000);//打開端口
   } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   dp = new DatagramPacket(buf, 1024);

   try {
    ds.receive(dp);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   String str = new String(dp.getData(), 0, dp.getLength()) + "from"
     + dp.getAddress().getHostAddress() + ":port" + dp.getPort();
   System.out.println(str);
   ds.close();

  }
 }
}

相關(guān)文章

最新評論

海口市| 灯塔市| 和静县| 临澧县| 民和| 黑龙江省| 南川市| 江油市| 自治县| 金山区| 博客| 湘乡市| 河南省| 锦州市| 凤山市| 衢州市| 六枝特区| 密云县| 樟树市| 昭苏县| 灵丘县| 古蔺县| 九龙县| 萨嘎县| 凉城县| 汽车| 东港市| 台中县| 彩票| 神池县| 青浦区| 会泽县| 策勒县| 美姑县| 侯马市| 海南省| 泰宁县| 宽城| 庆元县| 宝坻区| 江门市|