java18章网络编程

OSI7层模型解释:

1:物理层:数据作为原始的比特流(bit),典型的设备包括集线器(Hub)

2:数据链路层:数据链路层负责两个相邻的节点间的线路上,无差错的传送以帧(Frame)为单位的数据
如果接收方检测到所传的数据中有差错,就要通知发送方重新发送这一帧,本层的典型设备是交换机。

3:网络层:源主机和目标主机的网络地址进行通信

4:传输层:传输和取消的连接功能以可靠方式传输的数据单位称为段,以不可靠的传输单位称为数据报

5:会话层:会话层管理进程之间的会话过程 负责建立,管理和连接进程之间的会话,比如说QQ我跟你说话就表示在会话这一层.

6:表示层:表示层对上层的数据进行转换,以保证一个主机的应用层的数据可以被另一个主机的应用层理解,表示层包括,加密-解密-压缩-解压和格式转换
7:应用层:应用层确定进程之间通信的实际用途,以满足用户的实际请求,例如浏览Web站点 收发E-mail,上传或下载文件等.

TCP/IP模型:程序员应要做的就是在应用这一层里面
TCP协议是面向连接的可靠的通信协议,
而UDP是非连接的不可靠的通信协议,
这两个协议位于TCP/IP模型的传输层,要在网络上找到另一台计算机的程序进行通讯需要提供IP地址和端口号.

Socke是客户端ServerSocke是服务器端,Socket和ServerSocke用于在两个java程序之间创建一个TCP/IP连接,程序之间的通讯通过IO类来执行

下面给大家贴一段代码f是服务器端f1是客服端

服务器端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ccc;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class f extends JFrame implements Runnable {
ServerSocket svr;
JTextArea w;
public f(int port) throws IllegalArgumentException, IOException {
Container m = this.getContentPane();
m.setLayout(new GridLayout(3, 1));
this.setSize(300, 450);
this.setTitle("服务端");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w = new JTextArea(100, 200);
this.add(w);
svr = new ServerSocket(port);
}
public void run() {
while (true) {
System.out.println("在端口" + svr.getLocalPort() + "上等待客户端...");
Socket server;
try {
server = svr.accept(); //服务器等待方法
System.out.println("刚连接" + server.getReuseAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
w.setText(in.readUTF());
server.close();
} catch (SocketTimeoutException e) {
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
try {
f x = new f(8088);
Thread f = new Thread(x);
f.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

客服端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ccc;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class f1 extends JFrame {
JTextField s;
JButton an;
public f1() {

Container m = this.getContentPane(); //内容面板
m.setLayout(new GridLayout(2, 1)); //布局
this.setSize(300, 450); //大小
this.setTitle("客户端"); //名称
this.setVisible(true); //可见
this.setLocationRelativeTo(null); //居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭
s = new JTextField();
an = new JButton("提交");
this.add(s);
m.add(an);
s.setBounds(10, 10, 150, 200);
an.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ke();
}
});
}
public void ke() {
String serverNanme = "localhost";//客户端进行连接服务端
int prot = 8088; //端口
System.out.println("端口" + prot + "连接" + serverNanme);
try {
Socket ck = new Socket(serverNanme, prot);//NEW了一个Socket对象
System.out.println("本地主机:" + ck.getRemoteSocketAddress());
OutputStream ccc = ck.getOutputStream();
DataOutputStream cc1 = new DataOutputStream(ccc);
cc1.writeUTF(s.getText());
ck.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args) {
new f1();
}
}