网络编程

黑马程序员

                                                网络编程

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

网络通讯三要素:IP地址(InetAddress),端口,传输协议(TCPUDP

例如:获取IP地址

1. class  IPDemo  

2. {  

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

4.     {  

5.         InetAddress i = InetAddress.getLocalHost();   //获取本机IP地址  

6.         i.getHostAddress();   //获取IP地址  

7.         i.getHostName();      //获取主机名  

8.   

9.         InetAddress ia = InetAddress.getByName("192.168.0.2");  //根据主机名获取InetAddress对象  

10.    }  

11.}  

class IPDemo

{

publicstatic void main(String[]args) throws Exception

{

 InetAddressi =InetAddress.getLocalHost();   //获取本机IP地址

 i.getHostAddress();  //获取IP地址

 i.getHostName();     //获取主机名

 

 InetAddressia =InetAddress.getByName("192.168.0.2"); //根据主机名获取InetAddress对象

}

}

 传输协议(TCPUDP

两者的区别:

UDP
将数据及源和目的封装成数据包中,不需要建立连接

每个数据报的大小在限制在64k
因无连接,是不可靠协议
不需要建立连接,速度快

TCP
建立连接,形成传输数据的通道。

在连接中进行大数据量传输
通过三次握手完成连接,是可靠协议
必须建立连接,效率会稍低

 

UDP做聊天室程序

1. import java.io.*;  

2. import java.net.*;  

3. class Send implements Runnable  

4. {  

5.     private DatagramSocket ds;  

6.     public Send(DatagramSocket ds)  

7.     {  

8.         this.ds = ds;  

9.     }  

10.    public void run()  

11.    {  

12.        try  

13.        {  

14.            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));  

15.  

16.            String line = null;  

17.  

18.            while((line=bufr.readLine())!=null)  

19.            {  

20.                byte[] buf = line.getBytes();  

21. 

22.                DatagramPacket dp =   

23.                        new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10002);  

24.  

25.                ds.send(dp);  

26.  

27.                if("886".equals(line))  

28.                    break;  

29.            }  

30.        }  

31.        catch (Exception e)  

32.        {  

33.            throw new RuntimeException("发送端失败");  

34.        }  

35.    }  

36.}  

37.class Rece implements Runnable  

38.{  

39.  

40.    private DatagramSocket ds;  

41.    public Rece(DatagramSocket ds)  

42.    {  

43.        this.ds = ds;  

44.    }  

45.    public void run()  

46.    {  

47.        try  

48.        {  

49.            while(true)  

50.            {  

51.                byte[] buf = new byte[1024];  

52.                DatagramPacket dp = new DatagramPacket(buf,buf.length);  

53.  

54.                ds.receive(dp);  

55.  

56.  

57.                String ip = dp.getAddress().getHostAddress();  

58.  

59.                String data = new String(dp.getData(),0,dp.getLength());  

60.  

61.                if("886".equals(data))  

62.                {  

63.                    System.out.println(ip+"....离开聊天室");  

64.                    break;  

65.                }  

66.                System.out.println(ip+":"+data);  

67.            }  

68.        }  

69.        catch (Exception e)  

70.        {  

71.            throw new RuntimeException("接收端失败");  

72.        }  

73.    }  

74.}  

75.class  ChatDemo  

76.{  

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

78.    {  

79.        DatagramSocket sendSocket = new DatagramSocket();  

80.        DatagramSocket receSocket = new DatagramSocket(10002);  

81.  

82.        new Thread(new Send(sendSocket)).start();  

83.        new Thread(new Rece(receSocket)).start();  

84.    }  

85.}</span>  

import java.io.*;

import java.net.*;

class Send implements Runnable

{

 privateDatagramSocket ds;

 publicSend(DatagramSocket ds)

 {

  this.ds= ds;

 }

 publicvoid run()

 {

  try

  {

    BufferedReader bufr =newBufferedReader(new InputStreamReader(System.in));

 

    String line = null;

 

    while((line=bufr.readLine())!=null)

    {

      byte[] buf = line.getBytes();

 

      DatagramPacket dp =

               newDatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10002);

 

      ds.send(dp);

 

      if("886".equals(line))

        break;

    }

  }

  catch(Exception e)

  {

    throw newRuntimeException("发送端失败");

  }

 }

}

class Rece implements Runnable

{

 

 privateDatagramSocket ds;

 publicRece(DatagramSocket ds)

 {

  this.ds= ds;

 }

 publicvoid run()

 {

  try

  {

    while(true)

    {

      byte[] buf = new byte[1024];

      DatagramPacket dp = newDatagramPacket(buf,buf.length);

 

      ds.receive(dp);

 

 

      String ip=dp.getAddress().getHostAddress();

 

      String data =newString(dp.getData(),0,dp.getLength());

 

      if("886".equals(data))

      {

        System.out.println(ip+"....离开聊天室");

        break;

      }

 

 

      System.out.println(ip+":"+data);

    }

  }

  catch(Exception e)

  {

    throw newRuntimeException("接收端失败");

  }

 }

}

class ChatDemo

{

 publicstatic void main(String[]args) throws Exception

 {

  DatagramSocketsendSocket = newDatagramSocket();

  DatagramSocketreceSocket = newDatagramSocket(10002);

 

  newThread(newSend(sendSocket)).start();

  newThread(newRece(receSocket)).start();

 }

}

 

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值