网络编程
1).认识网络名词
IP地址:表示网络上的一台主机,逻辑地址
MAC地址:表示网络上的一台主机,物理地址
端口:标识主机中的一个进程 0 – 65535, 1024一下为预留端口
协议:通信双方之间的约定和标准
物理层 –> 数据链路层 –> 网络层 –> 传输层 –> 会话层 –> 表示层 –> 应用层
传输层:
TCP:传输控制协议,面向连接的,是可靠的 (Transmission Control Protocol)
UDP:用户数据报协议,非面向连接的,不可靠的 (User Datagram Protocol)
2).TCP编程(Transmission Control Protocol)
TCP: Socket 编程(套接字) ServerSocket Socket (服务器端用两个)
java包:java.net.*;
用法:如下
--------------------------------------------------------------
TCP: 模拟客户端与服务器端的通信代码:先启动服务器端
客户端(Client):
public static void main(String[] args) throws Exception {
Socket s = new Socket(“192.168.77.13”, 9000); //向服务器发起连接,参数分别是ip和端口
InputStream is = new s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
for(int i = 1; i <= 20; i++) {
String str = br.readLine(); //从服务器端读
System.out.println(str);
}
s.close();
}
服务器端(Service)
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000); //服务器进程绑定端口为9000
Socket s = ss.accept();//接受访问
OutputStream os = s.getOutputStream();
PrintWriter out = new PrintWriter(os);
for(int I = 1; I <= 20; i++) {
out.println(“Hello” + i); //向客户端写
out.flush();
Thread.sleep(200);
}
s.close();
}
3).UDP编程(User Datagram Protocol)
UDP:Socket 编程 DatagramSocket DatagramPacket
java包:java.net.*;
用法:如下
--------------------------------------------------------------
UDP: 模拟客户端与服务器端的通信代码:先启动服务器端
客户端(Client):向服务器端送一封信
public static void main(String[] args) throws Exception {
//先发一封信
DatagramSocket ds = new DatagramSocket();
String str = “这里是客户端的信”;
byte[] b = str.getBytes(); //可以设置编码方式
DatagramPacket sendLetter = new DatagramPacket(b, 0, b.length, InetAddress.getLocalHost(), 9090);//发送信,前三个参数为信的内容,后两个参数分别为本地IP和送信端口端口
ds.send(sendLetter);
//接受30封信
for(int I = 0; I < 30; I++) {
DatagramPacket receiveLetter = new DatagramPacket(new byte[100], 0, 100);
ds.receive(receiveLetter);
byte[] bs = receiveLetter.getData();
int offset = receiveLetter.getOffset();
int length = receiveLetter.getLength();
String newStr = new String(bs, offset, length);//将数组转化为字符串
}
ds.close();
}
服务器端(Server):从客户端端收信,返回30封信
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(9090);
DatagramPacket receiveLetter = new DatagramPacket(new byte[100], 0 , 100);//将接收的信内容存入byte数组
ds.receive(receiveLetter);
InetAddress address = receiveLetter.getAddress(); //获得客户端地址
int port = receiveLetter.getPort();//获取来信客户端的端口
//给客户端发送30封信
forI(int I = 0; I <= 30; i++) {
String str = “这是服务器端的信”;
byte[] b = str.getBytes();
DatagramPacket sendLetter = new DatagramPacket(b, 0, b.length, address, port);
ds.send(sendLetter);
Thread.sleep(200);
}
ds.close();
}
URL编程是在应用层, 统一资源定位器
格式(字符串):协议名://主机地址:端口号/相对路径
java包 java.net.*;
用法:
public static void main(String[] args) throws Exception {
URL url = new URL(“http://192.168.77.13/corejava.txt”);//创建url
URLConnection uc = url.openConnection(); //打开连接
InputStream is = uc.getInputStream(); //读取
BufferedReader br = new BufferedReader(new InputStreamReader(is));//桥转换
while(true) {
String str = br.readLine();
if(Str == null)
break;
System.out.println(str);
}
in.close();
}