目的:
无线电台传播交流信息数据交换通信 javaweb:网页编程 B/S架构网络编程:TCP/IP C/S架构网络通信要素
网络通信协议IP和端口号 规则:网络通信的协议:http,ftp,smtp,tcp,udp等TCP/IP四层概念模型两个主要问题:
如何准确定位到网络上的一台或者多台主机找到主机之后如何进行通信 IP类
class InetAddress: 此类表示Internet协议IP地址
唯一定位一台网络上的计算机**127.0.0.1:本机 localhost **IP地址分类:
ipv4/ipv6
ipv4 127.0.0.1 4个字节组成, 0-255 42亿**ipv6 ** 128 位 8个无符号整数 公网(互联网)/私网(局域网)
192.168.xx.xx 专门给组织内使用的ABCD类地址
package Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestIP {
public static void main(String[] args) {
try {
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getAddress());//字节 一组地址
System.out.println(inetAddress2.getCanonicalHostName());//规范IP名字
System.out.println(inetAddress2.getHostAddress());//IP
System.out.println(inetAddress2.getHostName());//域名或者自己电脑名字
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
端口
端口表示计算机上的一个程序的进程
不同的进程有不同的端口号 用来区分软件
被规定0-65535
TCP,UDP : 65535*2 单个协议下,端口号不能冲突
端口分类
共有端口 0-1023
HTTP:80HTTPS:443FTP:21Telent: 23
程序注册端口:1024-49151,分配用户或者程序
Tomcat:8080MySQL: 3306Oracle: 1521
动态、私有:49152-65535
netstat -ano #查看所有端口 netstat -ano|findstr "5900" #查看指定端口 tasklist|findstr "8696" # 查看指定端口的进程重要通信协议
TCP:用户传输协议
连接 打电话连接 稳定三次握手:最少需要三次连接来保证稳定连接四次挥手:最少需要四次连接来保证稳定断开客户端、服务端传输完成、释放连接 效率低 UDP:用户数据报协议
不连接 发短信客户端、服务端:没有明确的界限不管有没有准备好都可以发送 TCP实现聊天
Client客户端
- 连接服务器Socket发送消息
package Address;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClient1 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
os.write("HELLO".getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Server服务器
- 建立服务的端口ServerSocket等待用户的连接accept接收用户的消息
package Address;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1) {
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(baos != null) {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}



