网络编程学习2

在接着上部分的内容进行学习网络编程的学习记录-优快云博客,现在有一个简单案例:通过控制台键入内容message,将message使用send发送并且使用recieve接收,最后在控制台打印输出message的内容。

我最开始创建了4个类:包含Paragram、Send、Recieve、test

package hxy.test;

import java.net.InetSocketAddress;

public class Paragram {
    String message;
    int port;
    String ip;

    public Paragram(String message, int port, String ip) {
        this.message = message;
        this.port = port;
        this.ip = ip;
    }
    public InetSocketAddress getInetSocketAddress() {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
        return inetSocketAddress;
    }
}
package hxy.test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class Recieve {
    InetSocketAddress inetSocketAddress;
    int port;
    public Recieve(InetSocketAddress inetSocketAddress) {
        this.inetSocketAddress = inetSocketAddress;
        this.port = inetSocketAddress.getPort();
    }
    public void receive_method() throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(port);
        DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024);
        while (true) {
            datagramSocket.receive(datagramPacket);
            byte[] message_byte = datagramPacket.getData();
            int length = datagramPacket.getLength();
            String message = new String(message_byte,0,length);
            System.out.println("输出内容:"+message);
        }

    }
}
package hxy.test;

import java.io.IOException;
import java.net.*;

public class Send {
    String message;
    InetSocketAddress inetSocketAddress;
    public Send(String message, InetSocketAddress inetSocketAddress) {
        this.message = message;
        this.inetSocketAddress = inetSocketAddress;
    }

    public void send_method() throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();
        byte[] message_byte = message.getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(message_byte, message_byte.length, inetSocketAddress.getAddress(), inetSocketAddress.getPort());
        datagramSocket.send(datagramPacket);
        datagramSocket.close();
    }
}
package hxy.test;

import java.io.IOException;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int i =100;
        Paragram para = new Paragram("null", 8080, "127.0.0.1");
        while(i!=0){
            //用户输入
            System.out.println("keay in");
            String s = scanner.nextLine();
            para.message=s;
//            System.out.println(para.message);
            //退出循环条件
            if(s.equals("886")) break;
            //发送
            Send send1 = new Send(para.message, para.getInetSocketAddress());
            send1.send_method();
            //接收
            Recieve recieve1 = new Recieve(para.getInetSocketAddress());
            recieve1.receive_method();

        }
    }
}

但是此时无法完整执行test类中的内容,会在输出第一条消息的时候卡住,原因如下:

如果我在一个测试类中先执行send,再执行recieve,那么就会出现send发送完了,recieve的还未执行的情况,当recieve开始执行时,已经没有内容传输过来,此时的recieve过程就会等待send。此时就会陷入死锁。

如果我先执行recieve再执行send那么同样的,recieve会卡住,无法继续向下执行。

所以我写了两个测试类,分别进行send和recieve,正确执行。

package hxy.test;

import java.io.IOException;

public class test_recieve {
    public static void main(String[] args) throws IOException {
        Paragram para = new Paragram("null", 8080, "127.0.0.1");
        Recieve recieve1 = new Recieve(para.getInetSocketAddress());
        recieve1.receive_method();
    }
}
package hxy.test;

import java.io.IOException;
import java.util.Scanner;

public class test_send {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int i = 100;
        Paragram para = new Paragram("null", 8080, "127.0.0.1");
        while (i != 0) {
            //用户输入
            System.out.println("keay in");
            String s = scanner.nextLine();
            para.message = s;
//            System.out.println(para.message);
            //退出循环条件
            if (s.equals("886")) break;
            Send send1 = new Send(para.message, para.getInetSocketAddress());
            send1.send_method();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值