- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我有一个 udp 客户端服务器代码无法正常工作,我问一个一般性问题“Shane 是个好 child 吗”这两个代码都没有出现错误,但是当我运行它输出的代码时 数据报发送数据包 = 新的 DatagramPacket(sendData, sendData.length, IPAddress, 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)
查看此异常,我注意到由于您的缓冲区不会为空,因此您的地址就是问题,因为您创建了一个新的 DatagramPacket,而没有客户端连接的 IP 和端口号...您必须通过它们到 DatagramPacket 实例,以便服务器知道谁是尝试通信的客户端...您想要实现的一个非常简单/基本的示例位于 http://systembash.com/content/a-simple-java-udp-server-and-udp-client/ 。下面是我对代码的初步修复...您的答案仍然需要在缓冲区上进行一些工作,我将把它作为练习...
这是服务器的固定代码,仅接受 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
由于您的要求似乎是设计一个可以处理多个客户端并计算投票数的服务器,因此我还建议您使用多线程版本的服务器,通过使用不同的线程来处理每个客户端客户端在自己的线程中并更新静态计数器的值(一个示例是 while(true) 循环使用 Executor 执行新的 Runnable http://java-x.blogspot.com/2006/11/java-5-executors-threadpool.html )。考虑按照描述创建一个 Runnable 实例,并将服务器的代码放置在 public void run() {} 方法实现中...我也将把它作为练习留给您...
关于java - 客户端服务器udp套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836795/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!