繁体   English   中英

客户端 服务器 udp 插座

[英]client server udp socket

嗨,我有一个 udp 客户端服务器代码不起作用我问一个一般性问题“Shane 是个好孩子吗”这两个代码都没有出现错误,但是当我运行代码时它输出 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.长度,IP 地址,9876); 而不是让客户端问候服务器。 流程应该是服务器初始化并等待客户端--客户端问候服务器--服务器提出问题--客户端响应问题--服务器记录是否投票并显示天气或人是否喜欢=。 任何关于如何完善代码的建议都将受到欢迎 听到的是服务器代码

import java.net.*;

public class VotingServer {
//private static final int yes = 0;
private static int yes2;

public static void main(String[] args, int getrep) throws Exception{
    // part 1: initialization
    DatagramSocket serverSocket = new DatagramSocket(9876);
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];
    InetAddress[] IPAddressList = new InetAddress[5];
    int[] portList = new int[5];

    // part 2: receive the greeting from clients
    for (int i=0; i<1; i++) {
        DatagramPacket receivePacket = 
            new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String greeting = new String(receivePacket.getData());
        System.out.println("From Client: " + greeting);

        IPAddressList[i] = receivePacket.getAddress();
        portList[i] = receivePacket.getPort();
    } // for (i)

    // part 3: broadcast the votiong question to all clients
    String question = "is shane a good kid 1 for yes 0 no?\n";
    for (int i=0; i<5; i++) { 
        sendData = question.getBytes();
        DatagramPacket sendPacket = 
            new DatagramPacket(sendData, sendData.length);
        serverSocket.send(sendPacket);

    // part 5: receive the age of client (B)
        DatagramPacket receivePacket = 
            new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String ageStr = new String(receivePacket.getData());
        yes2 = Integer.parseInt(ageStr);

        IPAddressList[i] = receivePacket.getAddress();
        portList[i] = receivePacket.getPort();

    // part 6: compute the price (C)
    double count= 0; 
    double no = 0;

    if (yes2 >= 1 ) count = 1;
    else 
        if (yes2 <= 0 ) no = 1;

    // part 7: send the price to client
    String rep = null;
    String countStr = ""+count+"\n";
    String noStr = ""+no+"\n";
    if (no < count) rep = "Is a good kid";
    else 
        if (no > count) rep = "is a bad kid";
    System.out.println(" "+getrep);
    sendData = countStr.getBytes();
    sendData = noStr.getBytes();
    sendData = rep.getBytes();
    DatagramPacket sendPacket1 = 
        new DatagramPacket(sendData, sendData.length);
    serverSocket.send(sendPacket1);

} // main()

}} // UDPServer

这里是客户端代码导入 java.io。 ; 导入java.net。 ;

public class ClientVoting {

    public static void main(String[] args) throws Exception {
        // part 1: initialization
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];



        String sentence = inFromUser.readLine();
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = 
            new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket);


        // part 2: receive the question from server
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String question = new String(receivePacket.getData());
        System.out.println("From Server:" + question);

        String yes2 = inFromUser.readLine();
        sendData = yes2.getBytes();
        DatagramPacket sendPacket1 = 
            new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket1);


        // part 4: get the price from server
        receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String rep = new String(receivePacket.getData());
        System.out.println("the answer is " + rep);

        // part 4: close the socket
        clientSocket.close();

    } // main()

    } // class UDPClient

谢谢 SPF

我得到一个 NullPointException 在服务器端运行您的代码......代码本身存在一些问题。 第一个是您尝试保持客户端连接实例的数组的索引。 在这一点上,你只有一个......

for (int i=0; i<1; i++) {
    DatagramPacket receivePacket = 
        new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    String greeting = new String(receivePacket.getData());
    System.out.println("From Client: " + greeting);

    IPAddressList[i] = receivePacket.getAddress();
    portList[i] = receivePacket.getPort();
} // for (i)

但是,此时,当您尝试对其进行 5 次迭代时,您的代码很容易出现 NullPointException ......

String question = "is shane a good kid 1 for yes 0 no?\n";
for (int i=0; i<5; i++) { 
    sendData = question.getBytes();
    DatagramPacket sendPacket = 
        new DatagramPacket(sendData, sendData.length);
    serverSocket.send(sendPacket);   <<<<---- NPE prone code line... 

这是运行代码的结果...

From Client: hello
Exception in thread "main" java.lang.NullPointerException: null buffer || null address
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:629)
    at com.vasoftware.sf.common.VotingServer.main(VotingServer.java:38)

查看此异常,我注意到由于您的缓冲区不是 null,因此您的地址是问题所在,因为您创建了一个新的 DatagramPacket 没有 IP 和客户端连接的端口号...您必须将它们传递给DatagramPacket 实例,以便服务器知道谁是试图通信的客户端......您尝试实现的一个非常简单/基本的示例位于http://systembash.com/content/a-simple-java-udp-服务器和 udp 客户端/ 以下是我对代码的初步修复...您的答案仍需要在缓冲区上进行一些工作,我将其留作练习...

这是仅接受 1 个客户端的服务器的固定代码...我将把多线程的东西 + 数据处理程序留给您作为练习...

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

public class VotingServer {

  //private static final int yes = 0;
    private static int yes2;

    public static void main(String[] args) throws Exception {
        // part 1: initialization
        DatagramSocket serverSocket = new DatagramSocket(9876);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        InetAddress IPAddressList;
        int portList = -1;

        // part 2: receive the greeting from clients
        System.out.println("Ready to receive connections at port " + serverSocket.getLocalPort());
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String greeting = new String(receivePacket.getData());
        System.out.println("From Client: " + greeting);

        IPAddressList = receivePacket.getAddress();
        portList= receivePacket.getPort();

        // part 3: broadcast the votiong question to all clients
        String question = "is shane a good kid 1 for yes 0 no?\n";
        sendData = question.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddressList, portList);
        serverSocket.send(sendPacket);

    // part 5: receive the age of client (B)
        receiveData = new byte[1024];
        receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String ageStr = new String(receivePacket.getData());

        try {
            yes2 = Integer.parseInt(ageStr);   //<<<----- WILL NEVER GET THE VALUE... LEAVING IT AS AN EXERCISE....

        } catch (NumberFormatException nfe) {
            yes2 = 0;
        }

        receivePacket.getAddress();
        receivePacket.getPort();

        // part 6: compute the price (C)
        double count= 0; 
        double no = 0;

        if (yes2 >= 1 ) count = 1;
        else 
            if (yes2 <= 0 ) no = 1;

        // part 7: send the price to client
        // ALSO FIXING SOME CODE HERE AS WELL....
        String rep = null;
        rep = no < count ? "Is a good kid" : "is a bad kid";
        rep += " Server Count: " + count;

        sendData = rep.getBytes();
        DatagramPacket sendPacket1 = new DatagramPacket(sendData, sendData.length, IPAddressList, portList);
        serverSocket.send(sendPacket1);
    }
}

这是客户端:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ClientVoting {

    public static void main(String[] args) throws Exception {
        // part 1: initialization
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];

        System.out.print("What's the question? ");
        String sentence = inFromUser.readLine();
        sendData = sentence.getBytes();
        System.out.println("Attempting to connect the server at port " + 9876);
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket);

        System.out.println("Initial greeting sent... Waiting for response...");

        // part 2: receive the question from server
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String question = new String(receivePacket.getData());
        System.out.println("From Server:" + question);

        String yes2 = inFromUser.readLine();
        sendData = yes2.getBytes();
        DatagramPacket sendPacket1 = 
            new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket1);


        // part 4: get the price from server
        receiveData = new byte[1024];
        receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String rep = new String(receivePacket.getData());
        System.out.println("the answer is " + rep);

        // part 4: close the socket
        clientSocket.close();

    } // main()
}

您必须先执行服务器,因为它会侦听端口 9876 上打开的套接字。然后,您可以使用客户端连接到服务器。

###### Here's the output in the server-side... Just added a few details of what's going on...
Ready to receive connections at port 9876
From Client: Marcello

####### Here's the output of the client:
What's the question? Marcello
Attempting to connect the server at port 9876
Initial greeting sent... Waiting for response...
From Server:is shane a good kid 1 for yes 0 no?
the answer is is a bad kid Server Count: 0.0

由于您的要求似乎是设计一个可以处理多个客户端并计算票数的服务器,我还建议您使用服务器的多线程版本,通过使用不同的线程来处理其中的每个客户端自己的线程并更新 static 计数器的值(示例是 while(true) 循环在此处使用 Executor 执行新的 Runnable http://java-x.blogspot.com/2006/11/java-5-executors-线程池.html )。 考虑按照描述创建一个 Runnable 实例,并将服务器的代码放在 public void run() {} 方法实现中......我也将把这个作为练习留给你......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM