---------------------- android培训、java培训、期待与您交流! ---------------------
/**
*端口:对应用程序进行数字标识,以方便对方发送数据到指定的应用程序上
*通信协议:TCP/IP
*IP:127.0.0.1:本地回环地址,192.168.1.255广播地址,局域网
*网络模型:
*应用层、表示层、会话层、传输层(TCP、UDP)、网络层、数据链路层、物理层
*描述IP的类:InetAddres
*方法:getLocalHost返回主机名和IP地址
* getHostName返回主机名
* getByName(ip)获取指定ip的主机名 *
*端口:0到65535之间的数字*
*TCP
*建立连接,形成传输数据的通道;
*在连接中进行大数据量的传输;
*通过三次握手完成连接,是可靠的协议;
*必须建立连接,效率会稍低
*
*UDP
*将数据及源和目的封装成数据包,不需要建立连接;
*每个数据报的大小限制在64k内;
*因为面向无连接,是不可靠协议;
*不需要建立连接,速度快
*/
/**
* Socket网络编程最重要的部分
* 插座,应用软件通信机制,通信的前提
*传输协议不同,建立Socket也不一样
*UDP用的插座的建立:DatagramPacket:数据报包,既能发送又能接受,用来实现投递服务
*send、receive
*
* @author shantui
*
*/
/**
* 通过UDP发送一段文字
* 1.建立updsocket服务
* 2.提供数据,并封装到数据包中
* 3.通过soceket发送出去
* 4.关闭资源
*/
基于UDP传输的UDP发送端代码
DatagramPacket(byte[] buf, int offset, int length, InetAddressaddress, int port)
构造数据报包,用来将长度为 length
偏移量为 offset
的包发送到指定主机上的指定端口号。length
参数必须小于等于 buf.length
。
1: package nettest;
2:
3: import java.io.BufferedReader;
4: import java.io.InputStreamReader;
5: import java.net.DatagramPacket;
6: import java.net.DatagramSocket;
7: import java.net.InetAddress;
8:
9: public class UdpSend
10: {
11: public static void main(String[] args)throws Exception
12: {
13: /*
14: DatagramSocket ds =new DatagramSocket(8888);
15: byte[] buf="dfaljahello".getBytes();
16: //构造数据报包,将长度为length的包发送到指定主机的指定端口上
17: DatagramPacket dp =
18: new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),20010);
19: ds.send(dp);
20: */
21: //采用键盘录入方式:读一句发一句
22: DatagramSocket ds =new DatagramSocket(8888);
23: //标准键盘录入代码
24: String line=null;
25: BufferedReader bufr=
26: new BufferedReader(new InputStreamReader(System.in));
27: while((line=bufr.readLine())!=null)
28: {
29: if("886".equals(line))
30: break;
31: byte[] buf=line.getBytes();
32: DatagramPacket dp =
33: new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10011);
34: ds.send(dp);
35: }
36: ds.close();
37:
38:
39: }
40:
41: }
接收端代码
1: package nettest;
2:
3: import java.net.DatagramPacket;
4: import java.net.DatagramSocket;
5:
6: /**
7: * 定义一个用于接受基于udp协议的数据并处理
8: * 1.定义udpsocket服务,同时指定监听的端口,只收这个端口过来的数据
9: * 2.定义一个数据包,因为要存储接受到的数据
10: * 3.通过receive将收到的数据存入定义好的数据包中
11: * 4.通过数据包对象特有功能,取出数据并打印
12: * 5.关闭资源
13: */
14: public class UdpRece
15: {
16: public static void main(String[] args) throws Exception
17: {
18: DatagramSocket ds =new DatagramSocket(10011);
19: while(true)
20: {
21: byte[] buf =new byte [1024];
22: DatagramPacket dp=new DatagramPacket(buf,buf.length);
23: ds.receive(dp);
24: String ip=dp.getAddress().getHostAddress();
25: String data= new String(dp.getData(),0,dp.getLength());
26: int port=dp.getPort();
27: System.out.println(ip+".."+data+".."+port);
28: }
29: }
30:
31: }
编写一个聊天室的程序
1: package nettest;
2:
3: import java.io.BufferedReader;
4: import java.io.InputStreamReader;
5: import java.net.DatagramPacket;
6: import java.net.DatagramSocket;
7: import java.net.InetAddress;
8:
9: /**
10: * 编写聊天程序
11: * 有接受和发送数据的部分
12: * 这两部分需要同时执行
13: * 利用多线程技术
14: * 一个线程控制收,一个线程控制发
15: * 因为收发动作不一致,所以定义两个run方法
16: * 还要封装到不同的类中
17: */
18:
19: public class ChatDemo
20: {
21:
22: public static void main(String[] args) throws Exception
23: {
24: DatagramSocket sendSocket =new DatagramSocket();
25: DatagramSocket receSocket =new DatagramSocket(10013);
26: new Thread(new Send(sendSocket)).start();
27: new Thread(new Send(receSocket)).start();
28:
29: }
30:
31: }
32: class Send implements Runnable
33: {
34: private DatagramSocket ds;
35: public Send(DatagramSocket ds)
36: {
37: this.ds=ds;
38: }
39: public void run()
40: {
41: try {
42: String line=null;
43: BufferedReader bufr=
44: new BufferedReader(new InputStreamReader(System.in));
45: while((line=bufr.readLine())!=null)
46: {
47: if("886".equals(line))
48: break;
49: byte[] buf=line.getBytes();
50: DatagramPacket dp =
51: new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10013);
52: ds.send(dp);
53: }
54: } catch (Exception e) {
55: throw new RuntimeException("发送失败");
56: }
57: }
58: }
59: class Rece implements Runnable
60: {
61: private DatagramSocket ds;
62: private Rece(DatagramSocket ds)
63: {
64: this.ds=ds;
65: }
66: public void run()
67: {
68: try {
69: while(true)
70: {
71: byte[] buf =new byte [1024];
72: DatagramPacket dp=new DatagramPacket(buf,buf.length);
73: ds.receive(dp);
74: String ip=dp.getAddress().getHostAddress();
75: String data= new String(dp.getData(),0,dp.getLength());
76: //int port=dp.getPort();
77: System.out.println(ip+".."+data);
78: }
79: } catch (Exception e) {
80: throw new RuntimeException("接收失败");
81: }
82: }
83: }
基于TCP协议,传输文字,分为:客户端和服务端
1: package nettest;
2:
3: import java.io.InputStream;
4: import java.io.OutputStream;
5: import java.net.ServerSocket;
6: import java.net.Socket;
7:
8: /*
9: * TCP分客户端和服务端
10: * Socket构造的时候,一 建立就要找对应的服务端
11: * 创建socket服务并指定要连接 的主机和端口
12: *
13: */
14: }
15: class Clinet
16: {
17: public static void main(String[] args) throws Exception
18: {
19: //建立客户端socket服务,并指定要连接 的主机和端口
20: *
21: Socket s=new Socket("192.168.1.254",10003);
22:
23: OutputStream out = s.getOutputStream();
24: out.write("fadjfdasjafasfjdsafd".getBytes());
25: s.close();
26: }
27:
28: }
29: class Server
30: {
31: public static void main(String[] args)throws Exception
32: {
33: //建立服务端socket服务,并监听一个端口
34: ServerSocket ss=new ServerSocket(10003);
35: //通过accept方法获取连接过来的客户端对象
36: Socket s= ss.accept();
37: //获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据
38: InputStream in=s.getInputStream();
39: byte[] buf=new byte[1024];
40: int len =in.read(buf);
41: System.out.println(new String(buf,0,len));
42: }
43: }