dcsimg
Java Networking UDP Streams
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Angel_Tan
Posted On:   Sunday, March 30, 2003 11:53 PM

how do i write to a file i a udp stream? //file named 0003388f.txt File f= new File ("LogFile0003388f.txt"); // create Fileoutputstream FileOutputStream fos= new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fos); // printwriter PrintWriter pw = new PrintWriter(bos); try { while(true) { // READ INTO THE DATAGRAM FROM THE SOCKET HERE s.receive(p); // finally extract the data received from the datagram and print it // method .getData() returns a byte array buf = p.getData(); len = p.getLength(); str = new String(buf,0,len); System.out.println("LogReceiver: Got &l   More>>

how do i write to a file i a udp stream?

//file named 0003388f.txt
File f= new File ("LogFile0003388f.txt");
// create Fileoutputstream
FileOutputStream fos= new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// printwriter
PrintWriter pw = new PrintWriter(bos);

try
{
while(true)
{
// READ INTO THE DATAGRAM FROM THE SOCKET HERE
s.receive(p);
// finally extract the data received from the datagram and print it
// method .getData() returns a byte array
buf = p.getData();
len = p.getLength();
str = new String(buf,0,len);
System.out.println("LogReceiver: Got < "+str+" >");
pw.println(str);

if(str.equals("END"))
{
System.out.println("LogReceiver: Finished.");
System.exit(1);
}

}
}

catch (Exception e)
{
System.out.println("LogReceiver: Could not read from socket");
System.exit(1);
}

   <<Less

Re: Java Networking UDP Streams

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, March 31, 2003 07:04 AM

UDP does not have "streams", it only has packets. Can you fit the entire file in a single packet? Otherwise you're going to have to design a protocol, to handle a file that spans multiple packets.

About | Sitemap | Contact