使用UDP

时间:2016-05-08 18:00:44

标签: java udp

我试图使用UDP创建一个简单的测试程序。 使用键,矩形(称为Knight)向右移动。当两个客户端打开时,我希望发送方客户端将矩形数据发送到"主机"客户端,以便矩形同时移动。我继续收到绑定异常,我不认为,数据会不断被发送以在两个客户端上更新它。

主要课程:

package me.enchanter.main;

import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;

import me.enchanter.connection.Host;
import me.enchanter.connection.Join;
import me.enchanter.heroes.Knight;

public class Main extends JFrame{

    /**
     * Rewrite client and server in to UDP but keep object output stream code
     * 
     * ObjectOutputStream oos = new ObjectOutputStream(skt.getOutputStream());
     * oos.writeObject(Main.knight);
     * 
     * Object object  = (Knight) ois.readObject();
     * Knight tmp = (Knight) object;
     * tmp.print();
     * Main.knight = (Knight) object;
     * 
     */

    //testing stuff clean up later
    public static Knight knight1 = new Knight(1, "11");
    public static Knight knight2 = new Knight(2, "22");

    private static final long serialVersionUID = 1L;

    Host host = new Host();
    Join join = new Join();

    public static int port = 4000; //default port is 4000
    public static String ip = "localhost"; //default ip is localhost

    //JFrame vars
    JButton joinb;
    JButton hostb;

    public Main(){
        setTitle("Game");
        setSize(1000, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        setVisible(true);

        TextField iip = new TextField("IP");
        iip.setSize(100, 50);
        add(iip);

        TextField iport = new TextField("Port");
        iport.setLocation(0, 100);
        iport.setSize(100, 50);
        add(iport);

        hostb = new JButton("Host");
        hostb.setSize(100, 100);
        hostb.setLocation(0, 200);
        hostb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                port = Integer.parseInt(iport.getText());
                ip = iip.getText();
                host.start();
                remove(joinb);
                remove(hostb);
                remove(iip);
                remove(iport);
            }   
        });
        add(hostb);

        joinb = new JButton("Join");
        joinb.setSize(100, 100);
        joinb.setLocation(100, 200);
        joinb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                port = Integer.parseInt(iport.getText());
                ip = iip.getText();
                join.start();
                remove(joinb);
                remove(hostb);
                remove(iip);
                remove(iport);
            }   
        });
        add(joinb);

        KeyListener kl = new KeyListener(){

            public void keyPressed(KeyEvent e) {
                knight1.x++;
            }

            public void keyReleased(KeyEvent e) {

            }

            public void keyTyped(KeyEvent e) {

            }

        };
        addKeyListener(kl);
        run();
    }

    public void update(){

    }

    public void paint(Graphics g){
        super.paint(g);
        g.fillRect(knight1.x, 10, 100, 100);
        g.dispose();
    }

    public void run(){
        while(true){
            update();
            repaint();
        }
    }

    public static void main(String[] args){
        Main m = new Main();
    }
}

收到课程:

package me.enchanter.connection;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import me.enchanter.heroes.Knight;
import me.enchanter.main.Main;

public class Host extends Thread{

    private Thread t;
    private String threadName = "Host";

    DatagramSocket serverSocket;
    byte[] receiveData;
    byte[] sendData;

    public Host(){
        try{
            serverSocket = new DatagramSocket(4000);
            receiveData = new byte[1024];
            sendData = new byte[1024];
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void run(){
        while(true){
            try{
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);

                byte[] data = receivePacket.getData();
                ByteArrayInputStream in = new ByteArrayInputStream(data);
                ObjectInputStream is = new ObjectInputStream(in);
                Main.knight1 = (Knight) is.readObject();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public void start(){
        if(t == null){
            t = new Thread(this, threadName);
            t.start();
        }
    }
}

发送课程:

package me.enchanter.connection;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import me.enchanter.main.Main;

public class Join extends Thread{

    private Thread t;
    private String threadName = "join";

    byte[] sendData;
    ByteArrayOutputStream outputStream;
    ObjectOutputStream os;
    InetAddress IPAddress;

    DatagramSocket Socket;

    public Join(){
        sendData = new byte[1024];
        try {
            Socket = new DatagramSocket();
            IPAddress = InetAddress.getByName(Main.ip);
        } catch (UnknownHostException | SocketException e) {
            e.printStackTrace();
        }
    }

    public void run(){
        while(true){
            try{
                outputStream = new ByteArrayOutputStream();
                os = new ObjectOutputStream(outputStream);
                os.writeObject(Main.knight1);
                byte[] data = outputStream.toByteArray();
                DatagramPacket sendPacket = 
                    new DatagramPacket(data, data.length, IPAddress, Main.port);
                Socket.send(sendPacket);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public void start(){
        if(t == null){
            t = new Thread(this, threadName);
            t.start();
        }
    }
}

*注意,我没有包含的骑士类只有一个变量x;

由于

0 个答案:

没有答案