黑马程序员------网络编程

网络程序设计
网络协议规定了计算机之间链接的物理、机械(网线与网卡的连接规定)、电气(有效的电平范围)等特征
以及计算机之间的相互寻址规则、数据发送冲突的解决、长数据如何分段传送与接收等。就像不同的国家有
不同的法律一样,目前网络协议也有很多种。
Internet采用的是TCP/IP协议,在TCP/IP协议栈中,有两个高级协议是网络应用程序,分别为传输控制
协议TCP(Transmission Control Protocol)和用户数据报协议UDP(User Datagram Protocol)。
 TCP:
特点:面向连接,安全可靠,相对而言效率低
ServerSocket:用来表示服务器套接字。服务器套接字通过指定的端口来等待连接的套接字,他的主要功能是
等待来自网络上的连接“请求”。
1.创建非绑定服务器的套接字
new ServerSocket()
2.创建绑定特定端口号的服务器套接字
new ServerSocket(int port)
3.制定队列的最大长度,创建绑定到指定本地端口号的套接字
new ServerSocket(int port,int backlog)
Socket:是套接字类。使用Socket时,需要指定待连接服务器的IP地址和端口号。
1.创建到服务器连接的套接字
new Socket(String host,int port)
2.通过InetAddress类来创建
new Socket(InetAddress address,int port)
  public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub

ServerSocket server=null;
Socket client=null;
String s=null;
DataOutputStream out=null;
DataInputStream in=null;
try{
server=new ServerSocket(10000);
client=server.accept();
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
out.writeUTF("你好,我是服务器!");
while(true){
s=in.readUTF();
System.out.println("服务器收到:"+s);
Thread.sleep(1000);
}
}catch(Exception e){
e.printStackTrace();
}
}
}


public class Client{
public static void main(String[] args) {
// TODO Auto-generated method stub

Socket client=null;
String s=null;
DataOutputStream out=null;
DataInputStream in=null;
try {
client=new Socket("127.0.0.1",10000);
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
out.writeUTF("你好,我是客户端!");
s=in.readUTF();
System.out.println("客户端收到:"+s);
while(true){

out.writeUTF((new Random().nextInt(10))+"");

Thread.sleep(1000);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}


 UDP:
  特点:面向无连接,不安全,效率高
  DatagramSocket:建立UDP程序的套接字,数据包套接字是包投递服务的发送或接收点。每个在数据报套接字
  上发送或接收的包都是单独编址和路由的。
  1.创建绑定到本机主机上的任何可用的端口
  new DatagramSocket()
  2.创建绑定到本地主机上的指定端口
  new DatagramSocket(10000)
  3.创建绑定的指定本地地址,该方法适用于多块网卡和多个IP地址
new Datagramsocket(int port,InetAddress address)
DatagramPacket:表示数据报包,数据报包用来实现无连接包投递服务。每条报文仅根据该包中包含的信息,
从一台机器路由到另一台机器。从一台机器发送到另一台机器的多个包可能选择不同的路由
,也可能按不同的顺序到达。
1.接收指定的长度
new DatagramPacket(byte[] buf,int length)
2.指定数据包的内存空间和大小,同时指定数据包的目标地址和端口
new DatagramPacket(byte[] buf,int length,InetAddress address,int port)
  public class UDPDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

DatagramSocket client=new DatagramSocket(10010);
DatagramPacket packet;
while(true){
int i=new Random().nextInt(20);
byte buf[]=(i+"").getBytes();

packet=new DatagramPacket(buf, 0, buf.length, new InetSocketAddress("127.0.0.1", 10001));
Thread.sleep(2000);
client.send(packet);
if(i==8){
break;
}
}
client.close();
}
}
  class UDPDemo2{
public static void main(String[] args) throws Exception {
DatagramSocket server=new DatagramSocket(10001);
byte buf[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(buf, buf.length);
while(true){
server.receive(packet);
byte data[]=packet.getData();
System.out.println(new String(data,0,data.length));
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值