A server can use specialized streams to deliver typed data : Server « Network Protocol « Java






A server can use specialized streams to deliver typed data

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();
  }
}

           
         
    
  








Related examples in the same category

1.A generic framework for a flexible, multi-threaded server
2.Server allows connections on socket 6123Server allows connections on socket 6123
3.This server displays messages to a single clientThis server displays messages to a single client
4.The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5.Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
6.A multithreaded serverA multithreaded server
7.Base class to build multithreaded servers easily
8.Manage a pool of threads for clients
9.Client estimates the speed of the network connection to the server
10.This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
11.Quote Server
12.Logging Server based on SocketServer
13.Client and Server Demo
14.Reflector
15.Simple Http Server