Edehuag

导航

Tomcat通信概念篇

在上一篇了解完网络通信的基本概念之后,本章节为了解Tomcat的基本逻辑方便以后对

UDP:(发短信,不管是否能接受成功都会发送)

//发送端
//不需要连接服务器
public static void main(string[] args)throws Exception {
//1.建立一个socket
DatagramSocket socket=new Datagramsocket();
//2.建个包
String msg ="你好啊,服务器!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
// 数据,数据的长度起始,要发送给谁
DatagramPacket, packet = new DatagramPacket(msg.getBytes(), offset: 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
//接收端
public static void main(string[] args)throws Exception {
//开放端口
Datagramsocket socket=new Datagramsocket(9090);
// 接收数据包
byte[ buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
//阻塞接收
socket.receive(packet);
system.out.printin(packet.getAddress().getHostAddress());
System.out.printin(new string(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
}

UDP:建立聊天通话(建立连接成功才能发送,但服务端只能接受消息不能回复消息)

//发送端
public static void main(string[] args)throws Exception {
DatagramSocket socket = new DatagramSocket( port: 8888);
//准备数据:控制台读取 
system.inBufferedReader reader = new BufferedReader(new InputstreamReader(System.in));
while(true){
  String data=reader.readLine();
  byte[] datas = data.getBytes();
  DatagramPacket packet = new Datagrampacket(datas, 0,datas.length,new InetsocketAddress(hostname: "localhost", 6666);
  socket.send(packet);
  if(data.equals("bye")){
    break;
  }
  socket.close();
//接收端
public static void main(string[l args)throws Exception {
Socket socket=new DatagramSocket( port:6666);
while(true){
/准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
//阻赛式接收包表
socket.receive(packet);
//断开连接 
byte[] data = packet.getData();
String receiveData = new string(data, offset: 0, data.length);
System.out.println(receiveData);
if(receiveData.equals("bye")){
  break;
}
socket.close();

UDP(打电话,双方都可发送接收,用到了多线程)

//发送端
public class TalkSend implements Runnable{
  DatagramSocket socket =null;
  BufferedReaderreader = null;
  private int fromPort;
  private string toIp;
  private int toPort;
//建立有参构造
public Talksend(int fromPort, string toIP, int toPort){
  this.fromPort = fromPort;
  this.toIp = toIP;
  this.toPort = toPort;
  try {
    socket = new DatagramSocket(fromPort);
    reader = new BufferedReader(new InputStreamReader(System.in));
  }catch(Exception e){
    e.printstackTrace();
  }
}

@0verride
public void run(){
  while(true){
    try {
    String data=reader.readLine();
    byte[] datas =data.getBytes();
    DatagramPacket packet = new Datagrampacket(datas,0,datas.length,new InetsocketAddress(this.toIp,this.toPort));
    socket.send(packet);
    if(data.equals("bye")){
      break;
    }catch(Exception e){
    e.printstackTrace();
    }
  }  
  socket.close();
}
//接收端
public class TalkReceive implements Runnable {
  DatagramSocket socket =null;
  private int port;
  private string msgFrom;
public TalkReceive(int port,string msgFrom){
  this.port = port;
  this.msgFrom = msgFrom;
  try {
  socket = new DatagramSocket(port);
  }catch(socketException e){
    e.printstackTrace();
  }
}

@Override
public void run(){
  while(true){
    //淮备接收包褰
    try{
    byte[] container =new byte[1024];
    DatagramPacket packet = new DatagramPacket(container,0,container.length);
    //阻塞式接收包表
    socket.receive(packet);
    //断开连接 bye
    byte[] data= packet.getData();
    String receiveData=new String(data,0, data.length);
    System.out.println(msgFrom +":"+receiveData);
    if(receiveData.equals("bye")){
      break;
    }catch(IoException e){
      e.printstackTrace();
    }
}
    socket.close();
}

实现A B用户之间建立通讯互相发送信息

调用A
public class Talkstudent {
public static void main(string[]args){
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
调用B
public static void main(string[] args){
new Thread(new Talksend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();

posted on 2024-08-12 10:04  饿得慌~  阅读(6)  评论(0编辑  收藏  举报