/**
- 尤其是局域网,用该协议,但是它的缺点是不安全,数据可能会有丢失
*/
import server.Student;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpObjectClient {
public static void main (String[] args) throws Exception {
DatagramSocket poster = null;
poster = new DatagramSocket ( );
int a = 220;
double b = 2234.2;
Student s = new Student ( 12,"jshd" );
//把上面的数据转换成字节数组
//第二个低级流,即将数据写到字节数组里,或者将字节数组里的字节读出来
ByteArrayOutputStream bos = new ByteArrayOutputStream ( );//它就往缓存或者内存里面写
ObjectOutputStream object = new ObjectOutputStream ( bos );//往进写
object.writeInt ( a );
object.writeDouble ( b );
object.writeObject ( s );
object.flush ();
byte[] t = bos.toByteArray ();//转换字节数组,得到数据
object.close ();
//网络传输
DatagramPacket packet = new DatagramPacket ( t,t.length,
InetAddress.getByName ( "127.0.0.1" ),
9999);
poster.send ( packet );
}
}