Mega Code Archive

 
Categories / Java / Network Protocol
 

A server can use specialized streams to deliver typed data

import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.ServerSocket; import java.net.Socket; public class DataServer {   public static void main(String args[]) throws Exception {     ServerSocket ssock = new ServerSocket(1234);     while (true) {       System.out.println("Listening");       Socket sock = ssock.accept();       DataOutputStream dstream = new DataOutputStream(sock           .getOutputStream());       dstream.writeFloat(3.14159265f);       dstream.close();       sock.close();     }   } } class DataClient {   public static void main(String[] args) throws Exception {     Socket sock = new Socket(args[0], 1234);     DataInputStream dis = new DataInputStream(sock.getInputStream());     float f = dis.readFloat();     System.out.println("PI=" + f);     dis.close();     sock.close();   } }