1.IP地址
网络中host的标识不易记忆,可用主机名或域名
本地回环地址,localhost:127.0.0.1
存在于 :
java.net.InetAddress
常见方法:
//获取本地主机ip地址对象
InetAddress ip=InetAddress
.getLocalHost()
System
.out.println(ip
.getHostName())
System
.out.println(ip
.getHostAddress())
//获取其他主机 ip地址对象
InetAddress ip2=InetAddress
.getByName(
"www.baidu.com")
System
.out.println(ip2
.getHostName())
System
.out.println(ip2
.getHostAddress())
2.Port number
用于标识进程的逻辑地址有效端口号:0-65535系统保留端口号:0-1024
3.传输协议
-通讯的规则 常见协议:HTTP,TCP,UDP,RTP,SMTP
1. UDP:
1.将 数据、源、目的封装成数据包,不需要建立连接
2.每个数据包大小限制在64kb
3.无连接不可靠
4.速度快
2. TCP
1。建立连接,形成数据传输通道
2。在连接中进行大量数据传输
3.通过三次握手建立连接,是可靠协议。
(三次握手:1.request for connection 2.response to connection 3.acknowledge for response)
4.socket
建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。
udp传输:
使用DatagramSocket和DatagramPacket建立发送端,接收端调用socket发送接收方法关闭socket
package socket.udp;
import java.io.IOException;
import java.net.*;
/**
* 创建UDP传输的发送端
* / *思路:
* / 1.建立UDP的socket服务
* / 2.将要发送的数据封装到datagram中
* / 3.通过UDP的socket服务将datagram发送出去
* / 4.关闭socket服务
*/
public class UdpSender {
public static void main(String[] args)
throws IOException {
DatagramSocket ds=
new DatagramSocket();
String str=
"this is a text to send";
byte[] buf=str.getBytes();
DatagramPacket dp=
new DatagramPacket(buf, buf.length, InetAddress.getByName(
"10.0.0.3"),
10000);
ds.send(dp);
ds.close();
System.out.println(
"sender closed");
}
}
public class UdpReceiver {
public static void main(String[] args) throws IOException {
System.
out.println(
"receiving.......");
DatagramSocket ds =
new DatagramSocket(
10000);
byte[] buf =
new byte[
1024];
DatagramPacket dp =
new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
int port=dp.getPort();
String text=
new String(dp.getData(),
0,dp.getLength());
System.
out.println(ip+
":"+port+
":"+text);
ds.close();
System.
out.println(
"receiver closed");
}
}
TCP传输
**
*
*
* TCP 传输,客户端建立的过程:
*
1.创建客户端socket对象,建议对象创建时就明确目的地
2.如果连接建立成功,说明数据通道(socket流)已建立,
可以通过getInputStream或getOutputStream来获取。
3.用输出流将数据写出
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket socket=
new Socket(
"10.0.0.3",
9999);
OutputStream
out = socket.getOutputStream();
out.write(
"第一条tcp发送的数据".getBytes());
InputStream
in=socket.getInputStream();
byte[] buf=
new byte[
1024];
int len=
in.read(buf);
String text=
new String(buf,
0,len);
System.
out.println(text);
socket.close();
}
}
/**
*
* 建立tcp服务端的思路:
* 1.通过Serversocket创建服务端socket服务
* 2.服务端必须对外提供一个端口
* 3.获取连接的客户端对象
* 4.通过客户端对象获取socket流,读取客户端发来的数据
* 5.关闭资源,关闭客户端,服务端
*/
public class Server {
public static void main(String[] args)
throws IOException {
ServerSocket ss=
new ServerSocket(
9999);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
InputStream in=s.getInputStream();
byte[] buf=
new byte[
1024];
int len=in.read(buf);
String text=
new String(buf,
0,len);
System.out.println(ip+
"server:"+text);
OutputStream out=s.getOutputStream();
out.write(
"gocha".getBytes());
s.close();
}
}
转载请注明原文地址: https://ju.6miu.com/read-17731.html