问题描述
我正在尝试使用todo服务创建UDP应用程序 像这样,但是以下代码显示了待办事项数组列表中的所有消息:客户端发送消息2020年10月14日,提交分配给 WSAPI。服务器响应消息:WSAPI的Submit Assignment;付电 帐单
package udpechoserver;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import udpechoclient.Todo;
public class UDPEchoServer
{
private static final int PORT = 1234;
private static DatagramSocket dgramSocket;
private static DatagramPacket sendPacket,receivePacket;
private static byte[] buffer;
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException,ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readobject();
}
public static void main(String[] args) {
byte[] receiveData = new byte[65536];
byte[] sendData = new byte[65536];
try (DatagramSocket datagramSocket = new DatagramSocket(PORT);) {
while (true) {
try {
System.out.println("server running at port " + PORT + "!");
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
datagramSocket.receive(receivePacket);
int port = receivePacket.getPort();
ArrayList<Todo> listofMessages = (ArrayList)deserialize(receivePacket.getData());
List<Todo> newList = new ArrayList<>(listofMessages);
System.out.println("Received [" + listofMessages.size() + "] messages " );
// print out the text of every message
System.out.println("All messages:");
// List<String> strings = newList.stream()
// .map(o -> Todo.toString(object,null))
// .collect(Collectors.toList()); attempt to filter information based on the dates from the new list creating list of strings and getting date from the newlist created
for(int i =0;i<listofMessages.size();i++){
String date= listofMessages.get(i).getDate();
if(date.equalsIgnoreCase(listofMessages.get(i).getDate())){
System.out.println("Todo"+listofMessages.get(i).getMessage());
}
}
InetAddress address = receivePacket.getAddress();
ArrayList<Todo> array = new ArrayList<Todo>();
sendData = serialize(array);
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,address,port);
datagramSocket.send(sendPacket);
} catch (ClassNotFoundException e) {
e.printstacktrace();
}
}
} catch (IOException e) {
e.printstacktrace();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)