source file name: SumClient.java 

import java.net.*;  

import java.io.*;  

class SumClient

{  

public static void main(String args[])throws Exception{  

Socket s=new Socket(InetAddress.getLocalHost(),1234);  

DataInputStream din=new DataInputStream(s.getInputStream());  

DataOutputStream dout=new DataOutputStream(s.getOutputStream());  

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  

    System.out.println("enter any number");

String str="",str2="";  


str=br.readLine();  

dout.writeUTF(str);  

dout.flush();  


str2=din.readUTF();  

System.out.println(str2);  

  

dout.close();  

s.close();  

}

}  


source file name:  SumServer.java


import java.net.*;  

import java.io.*;  

class SumServer

{  

public static void main(String args[])throws Exception

{  

ServerSocket ss=new ServerSocket(1234);  

Socket s=ss.accept();  

DataInputStream din=new DataInputStream(s.getInputStream());  

DataOutputStream dout=new DataOutputStream(s.getOutputStream());  

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  

  System.out.println("server here..");

String str="",str2="";  


str=din.readUTF();  

 

int n=Integer.parseInt(str);

System.out.println("given no is: "+n);

int sum=0;

int d;

while(n>0)

{

d=n%10;

sum=sum+d;

n=n/10;

}

System.out.println("sum of digits: "+sum);

str2="sum of digits: "+sum;

dout.writeUTF(str2);  

dout.flush();  

  

din.close();  

s.close();  

ss.close();  

}

}  


output:

D:\JavaPrograms\Networking>javac SumClient.java

D:\JavaPrograms\Networking>javac SumServer.java

client side view 

D:\JavaPrograms\Networking>java SumClient

enter any number

12315

sum of digits: 12

server side view 
D:\JavaPrograms\Networking>java SumServer
server here..
given no is: 12315
sum of digits: 12