java第二十二天 网络编程 UDP TCP
1 什么叫网络编程
就是用来实现网络互连的不同计算机上运行的程序间可以进行数据交换
网络模型7层概述:
1.物理层
2. 数据链路层
3. 网络层
4. 传输层
5.会话层
6.表示层
7.应用层
2 网络编程三要素
A:IP地址:InetAddress: 网络中设备的标识,不易记忆,可用主机名
B:端口号: 用于标识进程的逻辑地址,不同进程的标识
C:传输协议: 通讯的规则常见协议:TCP,UDP
3 InetAddress类
public static InetAddress getByName(String host)
public String getHostAddress()//获取IP
public String getHostName()//获取主机名
4 UDP与TCP
UDP
将数据源和目的封装成数据包中,不需要建立连接;
每个数据报的大小在限制在64k;
因无连接,是不可靠协议;
不需要建立连接,速度快
TCP
建立连接,形成传输数据的通道;
在连接中进行大数据量传输;
需要连接所以是可靠协议;
必须建立连接,效率会稍低
5 Socket通信原理
Socket=IP+端口号
A:Socket套接字概述:
网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字。
B:Socket原理机制:
通信的两端都有Socket。
网络通信其实就是Socket间的通信。
数据在两个Socket间通过IO传输。
6 模板总结
通过UDP协议,完成UDP,不断的将键盘录入的数据发送给接收端,不断的打印出发送端键盘录入的数据,并测试
public class Udpclint {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入信息:");
String s = br.readLine();
if("886".equals(s)){
break;
}
byte[] bytes = s.getBytes();
int len = bytes.length;
int port = 8869;
DatagramPacket dp =new DatagramPacket(bytes,len, InetAddress.getByName("DEEP-1809011945"),port);
ds.send(dp);
}
ds.close();
}
}
public class UdpService {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(8869);
while(true){
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
byte[] data = dp.getData();
InetAddress address = dp.getAddress();
int len1 = dp.getLength();
String s = new String(data,0,len1);
System.out.println("发送方是:"+address.getHostAddress()+"内容是:"+s);
}
}
}
通过UDP协议,完成多线程版本的聊天室程序
1
public class ClientThread extends Thread {
public DatagramSocket ds;
public ClientThread(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
while(true) {
System.out.println("请输入信息:");
String s = null;
s = br.readLine();
if ("886".equals(s)) {
break;
}
byte[] bytes = s.getBytes();
int len = bytes.length;
int port = 8869;
DatagramPacket dp = new DatagramPacket(bytes, len, InetAddress.getByName("DEEP-1809011945"), port);
ds.send(dp);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
2
public class ServerThread extends Thread {
public DatagramSocket ds;
public ServerThread(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
try {
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
byte[] data = dp.getData();
InetAddress address = dp.getAddress();
int len1 = dp.getLength();
String s = new String(data, 0, len1);
System.out.println("发送方是:" + address.getHostAddress() + "内容是:" + s);
}
}catch(IOException e){
e.printStackTrace();;
}
}
}
3
public class Test {
public static void main(String[] args) throws SocketException {
DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds = new DatagramSocket(8869);
new ServerThread(ds).start();
new ClientThread(ds1).start();
}
}
通过TCP协议,完成客户端键盘录入,服务器端控制台输出
1
public class Tcpclient {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getByName("DEEP-1809011945"),8897);
OutputStream os = s.getOutputStream();
String s1= "hello,我是陈本豪,我在家没事干";
os.write(s1.getBytes());
s.close();
}
}
2
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8897);
Socket ss = s.accept();
InputStream is = ss.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
System.out.println(new String(bytes,0,len));
ss.close();
}
}
通过TCP协议,完成客户端键盘录入数据存储到服务器端文件中
1
public class Tcpclient {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getByName("DEEP-1809011945"),8897);
OutputStream os = s.getOutputStream();
String s1= "hello,我是陈本豪,我在家没事干";
os.write(s1.getBytes());
s.close();
}
}
2
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8897);
Socket ss = s.accept();
InputStream is = ss.getInputStream();
OutputStream os = ss.getOutputStream();
BufferedWriter bw = new BufferedWriter(new FileWriter("aa.txt"));
byte[] bytes = new byte[1024];
int len = is.read(bytes);
bw.write(new String(bytes,0,len));
bw.flush();
ss.close();
}
}
通过TCP协议,完成客户端读取文本文件内容,服务器端控制台输出
1
public class Tcpclient {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getByName("DEEP-1809011945"),8897);
OutputStream os = s.getOutputStream();
//String s1= "hello,我是陈本豪,我在家没事干";
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new FileReader("aa.txt"));
//os.write(s1.getBytes());
String line;
while((line=br.readLine())!=null){
os.write(line.getBytes());
}
s.close();
}
}
2
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8897);
Socket ss = s.accept();
InputStream is = ss.getInputStream();
//OutputStream os = ss.getOutputStream();
//BufferedWriter bw = new BufferedWriter(new FileWriter("aa.txt"));
byte[] bytes = new byte[1024];
int len = is.read(bytes);
//bw.write(new String(bytes,0,len));
// bw.flush();
System.out.println(new String(bytes,0,len));
ss.close();
}
}
通过TCP协议,完成客户端上传文件到服务器端
1
public class Tcpclient {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getByName("DEEP-1809011945"),8897);
OutputStream os = s.getOutputStream();
//String s1= "hello,我是陈本豪,我在家没事干";
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new FileReader("aa.txt"));
//os.write(s1.getBytes());
String line;
while((line=br.readLine())!=null){
os.write(line.getBytes());
}
s.close();
}
}
2
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(8897);
Socket ss = s.accept();
InputStream is = ss.getInputStream();
OutputStream os = ss.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter("bb.txt"));
// byte[] bytes = new byte[1024];
//
// int len = is.read(bytes);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.flush();
}
//bw.write(new String(bytes,0,len));
// bw.flush();
// System.out.println(new String(bytes,0,len));
ss.close();
}
}
通过TCP协议,完成多线程版本客户端上传文件到服务器端
1
public class TcpclientThread extends Thread {
Socket s;
{
try {
s = new Socket(InetAddress.getByName("DEEP-1809011945"),8897);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
OutputStream os = s.getOutputStream();
//String s1= "hello,我是陈本豪,我在家没事干";
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new FileReader("aa.txt"));
//os.write(s1.getBytes());
String line;
while ((line = br.readLine()) != null) {
os.write(line.getBytes());
}
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
2
public class TcpserverThread extends Thread{
ServerSocket s;
{
try {
s = new ServerSocket(8897);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
Socket ss = s.accept();
InputStream is = ss.getInputStream();
OutputStream os = ss.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter("dd.txt"));
// byte[] bytes = new byte[1024];
//
// int len = is.read(bytes);
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.flush();
}
//bw.write(new String(bytes,0,len));
// bw.flush();
// System.out.println(new String(bytes,0,len));
ss.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
3
public class Test {
public static void main(String[] args) throws SocketException {
new TcpserverThread().start();
new TcpclientThread().start();
}
}
7 注意(当文件发送完了给个标记,接收端接收完了也给个标记)