在这段java代码中如何实现两个不同IP的交流?
新博娱乐手机
新博娱乐手机
当前位置 : 新博娱乐手机 > 新博娱乐官方网站

在这段java代码中如何实现两个不同IP的交流?

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

class Send implements Runnable
{
    private DatagramSocket ds;
    public  SendDatagramSocket ds
    {
        this.ds = ds;
    }
    
    public void run
    {
        try
        {
            BufferedReader bufr = new BufferedReadernew InputStreamReaderSystem.in;
            String line = null;
            whileline = bufr.readLine!=null
            {
                if"886".equalsline
                    break;
                byte[] buf = line.getBytes;
                
                DatagramPacket dp =
                new DatagramPacketbuf,buf.length,InetAddress.getByName"192.168.1.105",10002;
                ds.senddp;
            }
        }
        catchException e
        {
            throw new RuntimeException"发送端失败";
        }
    }
}

class Rece implements Runnable
{
    private DatagramSocket ds;
    public  ReceDatagramSocket ds
    {
        this.ds = ds;
    }
    
    public void run
    {
        try
        {
            whiletrue
            {
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacketbuf,buf.length;
                ds.receivedp;
                String ip = dp.getAddress.getHostAddress;
                String data = new Stringdp.getData,0,dp.getLength;
                
                System.out.printlnip+"::"+data;
            }
        }
        catchException e
        {
            throw new RuntimeException"接收端失败";

        }
    }
}

class ChartDemo
{
    public static void mainString[] argsthrows Exception
    {
        DatagramSocket sendSocket = new DatagramSocket;
        DatagramSocket receSocket = new DatagramSocket10002;
        
        new Threadnew SendsendSocket.start;
        new Threadnew RecereceSocket.start;
    }
}

在上面这段代码里,如果要实现两台电脑的交流,需要在什么样的网络环境下进行呢?
比如我将 DatagramPacket dp =

                new DatagramPacketbuf,buf.length,InetAddress.getByName"192.168.1.105",10002;这行代码里的ip地址修改一下改成别的电脑的,在什么样的网络环境下可以实现呢?

两台电脑在同一个局域网内就没问题

广告位

新博娱乐手机