【问题标题】:Server-Client in UDPUDP 中的服务器-客户端
【发布时间】:2016-11-18 12:30:21
【问题描述】:

我的数据报简单客户端服务器程序出错。客户端从服务器获取信息,服务器也从客户端获取信息。

这是我的服务器:

package udp;

import java.net.*;

public class DatagramServer implements Runnable {

DatagramPacket pack;
DatagramSocket sock;

DatagramServer() {
    new Thread(this).start();
}

public void run() {
    try {
        while (true) {
            recieve();
            send();
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

public void send() throws Exception {
    byte data[] = "This is a \n datagram packet".getBytes();
    InetAddress add = InetAddress.getByName("localhost");
    pack = new DatagramPacket(data, data.length, add, 8000);
    sock = new DatagramSocket();
    sock.send(pack);
    sock.close();
}

public void recieve() throws Exception {

    byte[] buffer = new byte[65536];
    DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
    sock = new DatagramSocket(8000);
    sock.receive(incoming);
    byte[] data = incoming.getData();
    String s = new String(data, 0, incoming.getLength());
}

public static void main(String[] args) {
    new DatagramServer();
}
}

这是我的客户:

package udp;

import java.net.*;

public class DatagramClient {

DatagramPacket pack;
DatagramSocket sock;

DatagramClient() {
    Thread t1 = new Thread() {
        public void run() {
            while (true) {
                try {
                    receive();
                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    };
    t1.start();
    Thread t2 = new Thread() {
        public void run() {
            while (true) {
                try {
                    send();
                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    };
    t2.start();
}

public void receive() throws Exception {
    byte data[] = new byte[1000];
    pack = new DatagramPacket(data, data.length);
    sock = new DatagramSocket(8000);
    sock.receive(pack);
    System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
    System.out.println("Port::" + pack.getPort());
    sock.close();
}

public void send() throws Exception {

    String s = "KOLI SDF DFEF XFFFSS";
    byte[] b = new byte[1000];
    b = s.getBytes();
    DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
    sock= new DatagramSocket(8000);
    sock.send(dp);
}

public static void main(String[] args) {
    new DatagramClient();
}
}

我收到如下错误:

java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bindBUILD STOPPED  (total time: 4 seconds)

我该怎么办?

【问题讨论】:

    标签: java networking network-programming udp client-server


    【解决方案1】:

    您的客户端和服务器存在一些问题,例如在不同线程中打开相同的端口 8000,因此我已在以下代码中修复了这些问题。

    客户

    package udp;
    
    import java.net.*;
    
    public class DatagramClient {
    
        DatagramPacket pack;
        DatagramSocket sock;
    
        DatagramClient() {
            Thread t1 = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            receive();
                            Thread.sleep(5000);
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                    }
                }
            };
            t1.start();
        }
    
        public void receive() throws Exception {
            byte data[] = new byte[1000];
            pack = new DatagramPacket(data, data.length);
            sock = new DatagramSocket(8000);
            sock.receive(pack);
            System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
            System.out.println("Port::" + pack.getPort());
            sock.close();
        }
    
        public void send() throws Exception {
    
            String s = "KOLI SDF DFEF XFFFSS";
            byte[] b = new byte[1000];
            b = s.getBytes();
            DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
            sock= new DatagramSocket(8000);
            sock.send(dp);
        }
    
        public static void main(String[] args) {
            new DatagramClient();
        }
    }
    

    服务器

    package udp;
    
    import java.net.*;
    
    public class DatagramServer implements Runnable {
    
        DatagramPacket pack;
        DatagramSocket sock;
    
        DatagramServer() {
            new Thread(this).start();
        }
    
        public void run() {
            try {
                while (true) {
                    //recieve();
                    send();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    
        public void send() throws Exception {
            byte data[] = "This is a \n datagram packet".getBytes();
            InetAddress add = InetAddress.getByName("localhost");
            pack = new DatagramPacket(data, data.length, add, 8000);
            sock = new DatagramSocket();
            sock.send(pack);
            sock.close();
        }
    
        public void recieve() throws Exception {
    
            byte[] buffer = new byte[65536];
            DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
            sock = new DatagramSocket(8000);
            sock.receive(incoming);
            byte[] data = incoming.getData();
            String s = new String(data, 0, incoming.getLength());
        }
    
        public static void main(String[] args) {
            new DatagramServer();
        }
    }
    

    【讨论】:

    • 您能在回答中举个例子吗?我在这里是全新的,我应该在哪里做出改变?服务器还是客户端?
    • 但是服务器没有收到任何东西
    • 检查它从服务器接收到的客户端控制台。服务器仅根据给定的代码生成 udp 数据包。
    • 是的,客户端收到,但服务器没有……我也需要那个
    • 你能告诉我你的解决方案吗?提前致谢
    【解决方案2】:

    问题是您的服务器代码每次调用receive() 时都试图绑定到8000 端口。并且在 while(true) 循环中重复调用接收。所以它会一次又一次地尝试绑定到同一个端口。

    要修复它,请删除 UDP 套接字实例化,即

    sock = new DatagramSocket(8000);
    

    从接收方法并将其放在您的 while 循环开始之前。

    【讨论】:

    • 我的服务器如何接收和发送数据给客户端?
    • 不确定你在问什么,你的服务器代码已经做到了。
    • 在你的服务器发送方法中删除这个 - sock = new DatagramSocket();。您的服务器应该使用与它收到的相同的套接字来响应。在客户端,您的套接字应该连接服务器的地址和端口,发送数据并等待它收到来自服务器的响应。
    猜你喜欢
    • 2020-06-22
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多