gpt4 book ai didi

java - 如何在java数据报包中发送字符串数组?

转载 作者:行者123 更新时间:2023-12-01 12:22:28 24 4
gpt4 key购买 nike

如何在 java udp 数据包中发送字符串数组?我想发送这个数组String[] name = new String[10];

 try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:udp");
Statement st=con.createStatement();
String q="select emailfrom from emails where emailto='"+username+"'";
ResultSet rs=st.executeQuery(q);
String user;
int i=0;
String[] name=new String[10];
while(rs.next()){
user=rs.getString("emailftom");
name[i]=user;
byte[] box=name.getBytes();
DatagramPacket p=new DatagramPacket(box,name.length(),request.getAddress(),request.getPort());
aSocket.send(p);
}
}catch(ClassNotFoundException | SQLException ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}

最佳答案

您可以将其序列化,然后发送生成的字节数组。

    ByteArrayOutputStream contentStream = new ByteArrayOutputStream() 

ObjectOutputStream out = new ObjectOutputStream(contentStream);
out.writeObject(name);

out.flush();
out.close();

byte[] contents = contentStream.toByteArray();
DatagramPacket p=new DatagramPacket(contents,contents.length(),request.getAddress(),request.getPort());
aSocket.send(p);

关于java - 如何在java数据报包中发送字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com