网络编程第二节
单向通信
这算时间在不断学习,发现越发内卷,都说程序员是新一代农民工,现在只能多搬搬砖了…
我们几乎每天都在上网,普通人只知道上网,却不知道设备与设备之间是怎样沟通的,巧了之前我也是一个小白,现在斗胆来画个图(简易版的)
两台机器之间的通信----
差不多了,在扯多了就圆不回来了.
下面写一个两台机器之间的通信原理-----利用套接字让两台设备进行单向沟通
public class tesTip {
public static void main(String[] args) {
Socket client =null;//开辟客户端,像服务器发送
DataOutputStream dos=null;
try {
client= new Socket("localhost",8888);
OutputStream outputStream = client.getOutputStream();//客户端向服务器发数据(要知道服务器的地址和端口号)
dos= new DataOutputStream(outputStream);
dos.writeUTF("尔曹身与名俱灭,");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dos.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Iptest001 {
public static void main(String[] args) {
ServerSocket server=null;
Socket client= null;
DataInputStream dis=null;
try {
server= new ServerSocket(8888);//服务器在8888端口等候操作
client=server.accept();
dis=new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
dis.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在单向通信的基础上进行双向通信----
双向通信----
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Iptest001 {
public static void main(String[] args) {
ServerSocket server=null;
Socket client= null;
DataInputStream dis=null;
DataOutputStream dos= null;
try {
//接收客户端发送的数据
server= new ServerSocket(8888);//服务器在8888端口等候操作
System.out.println("等待客户端的鏈接.....");
client=server.accept();//等待客户端的连接,
dis=new DataInputStream(client.getInputStream());//连接上之后接收输入的流
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("客户端发送的数据--"+dis.readUTF());
//向客户端反馈数据
dos= new DataOutputStream(client.getOutputStream());
dos.writeUTF("不废江河万古流.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
dis.close();
dos.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.Socket;
/**
* @author : Gavin
* @date: 2021/8/19 - 08 - 19 - 15:12
* @Description: IpTest
* @version: 1.0
*/
public class tesTip {
public static void main(String[] args) {
Socket client = null;//开辟客户端,像服务器发送
DataOutputStream dos = null;
DataInputStream dis = null;
try {
//向服务器发送数据----
client = new Socket("localhost", 8888);
OutputStream outputStream = client.getOutputStream();//客户端向服务器发数据(要知道服务器的地址和端口号)
dos = new DataOutputStream(outputStream);
dos.writeUTF("尔曹身与名俱灭,");
try {
System.out.println("等待服務器反饋.....");
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收服务器发送来的反馈数据
InputStream inputStream = client.getInputStream();
dis = new DataInputStream(inputStream);
System.out.println("服务器返回的数据--" + dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上就是简单的单向和双向通信 最主要的是整明白getOutputStream 和getInputStream 的区别;
较复杂的双向通信----
实际运用的时候并没有以上这么简单,举个稍微比上述复杂点的,
模拟APP登录-----输入用户名和密码;
实现原理就是—
1,客户端登陆时检查与服务器的通信,
2,连接成功后执行登录,输入用户名和密码,并将数据发送给服务器
3,服务器端检测用户名和密码,并返回一个结果给客户端,
4,客户端根据结果来决定是否进入主页面;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
* @author : Gavin
* @date: 2021/8/19 - 08 - 19 - 15:12
* @Description: IpTest
* @version: 1.0
*/
public class tesTip {
public static void main(String[] args) {
Socket client = null;//开辟客户端,接收服务器发来的数据
DataOutputStream dos=null;
ObjectOutputStream oos = null;
DataInputStream dis= null;
ObjectInputStream ois = null;
String name = null;
String passWord = null;
User user = new User(name, passWord);
try {
//向服务器发送请求连接----
client = new Socket("localhost", 8888);//发送地址
OutputStream outputStream = client.getOutputStream();
dos = new DataOutputStream(outputStream);
//向服务器发送数据
dos.writeUTF("客户端正在请求连接服务器........");
try {
System.out.println("等待服务器反馈.....");
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收服务器发送来的反馈数据
InputStream inputStream = client.getInputStream();
dis = new DataInputStream(inputStream);
String s = dis.readUTF();
System.out.println("服务器返回的数据--" + s);
//根据服务器反馈选择是否要出现登录界面
if ("是".equals(s)) {
Scanner sc = null;
System.out.println("请输入账号----");
sc = new Scanner(System.in);
//name=sc.next();
user.setAdminName(sc.next());//接收用户名;
System.out.println("请输入账号密码----");
sc = new Scanner(System.in);
//passWord=sc.next();
user.setPassWord(sc.next());//接收密码
//接收完毕之后向服务器发送数据
oos=new ObjectOutputStream(client.getOutputStream());
oos.writeObject(user);//
//发送完数据后等待接收服务器的反馈
System.out.println("正在连接服務器.....");
try {
Thread.sleep(1000);//模拟等待时间
} catch (InterruptedException e) {
e.printStackTrace();
}
//接收服务器的反馈
dis=new DataInputStream(client.getInputStream());
String s1 = dis.readUTF();
if("是".equals(s1)){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
oos.close();
dis.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package IpTest;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Iptest001 {
public static void main(String[] args) {
ServerSocket server=null;
Socket client= null;
DataInputStream dis=null;
ObjectInputStream ois=null;
DataOutputStream dos= null;
ObjectOutputStream oos= null;
try {
//接收客户端发送的数据
server= new ServerSocket(8888);//服务器在8888端口等候操作
System.out.println("等待客户端的鏈接.....");
client=server.accept();//等待客户端的连接,
dis=new DataInputStream(client.getInputStream());//连接上之后接收输入的流
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("客户端请求的数据--"+dis.readUTF());
//向客户端反馈数据
dos= new DataOutputStream(client.getOutputStream());
dos.writeUTF("是");
//接着接收客户端发来的数据
try {//获取数据流
ois=new ObjectInputStream(client.getInputStream());
User user=(User)ois.readObject();
if("zhangsan".equals(user.getAdminName())&"123456".equals(user.getPassWord())) {
dos.writeUTF("是");
}else {
dos.writeUTF("否");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
dis.close();
dos.close();
ois.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}