Java programming part 49 Java  Sockets 2 : DataInputStream DataOutputStream
Channel: Redemptie
Date: 10/05/2012

Java Programming tutorial, Java Sockets and ServerSockets network, Using DataInputStream and DataOutputStream to send and receive data types over the network.
//server and socket code below
import java.net.*;
import java.io.*;


public class ServerClass {
 public static void main(String[] args){
  try {
   ServerSocket serv = new ServerSocket(7777);
   String message = " ";  
    
    Socket ss = serv.accept();
    DataInputStream input = new DataInputStream(ss.getInputStream());
    
    message = input.readUTF();
    System.out.println(" From SOCKET : "+message);
    
    DataOutputStream out = new DataOutputStream(ss.getOutputStream());
    out.writeUTF("From SERVER with LOVE");
        
    System.out.println("Socket connected to server OK!!!!");
    ss.close();
   
  } catch (IOException e) {
   System.out.println("Server error!!");
  }
 }
 
}

/// client socket code
import java.net.*;
import java.io.*;

public class SocketClass {

 public static void main(String[] args){
  try{
   Socket sock = new Socket("localhost",7777);
      
   DataOutputStream out = new DataOutputStream(sock.getOutputStream());
   out.writeUTF("From SOCKET with LOVE"); 
   DataInputStream input = new DataInputStream(sock.getInputStream());
   String strings = input.readUTF();
   System.out.println("From SERVER :  "+strings);
   
  }catch(IOException e)
  {
   System.out.println("Socket Error!!!!");
  }
  
 
 }
}