TCP/IP SOCKET COMMUNICATION (ECHO COMMAND) - Yimg

gazecummingNetworking and Communications

Oct 26, 2013 (3 years and 5 months ago)

50 views


TCP/IP SOCKET COMMUNICATION (ECHO
COMMAND)


ECHOCLIENT:


import java.io.*;

import java.net.*;

public class echoclient

{

public static void main(String[] args)

{

int port=8080;

try

{

String host=InetAddress.getLocalHost().getHostNa
me();

Socket client=new Socket(host,port);

PrintWriter writer=new PrintWriter(client.getOutputStream());

BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));

BufferedReader in=new BufferedReader(new InputStreamReader(Sy
stem.in));

System.out.println(reader.readLine());

String message;

while(true)

{

System.out.println("enter message to echo or exit:");

message=in.readLine();

if(message==null||message.equals("exit"))

break;

writer.println(message);

writer.flush();

System.ou
t.println("Echo from server:"+reader.readLine());

}

client.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}






ECHOSERVER


import java.net.*;

import java.io.*;

public class echoserver

{

public static void main(String[] args)

{

int port=8080;

try

{

ServerSocket Server=new ServerSocket(port);

while(true)

{

System.out.println("waiting for clients");

Socket client=Server.accept();

System.out.println("get connection from"+client.getInetAddress()+":"+client.getPort());

BufferedReader reader=new Buff
eredReader(new
InputStreamReader(client.getInputStream()));

PrintWriter writer=new PrintWriter(client.getOutputStream());

writer.println("Welcome to server");

writer.flush();

String message=reader.readLine();

while(!(message==null||message.equals("exit")))

{

System.out.println("message received:"+message);

writer.println(message);

writer.flush();

message=reader.readLine();

}

client.close();

}

}

catch(Exception e)

{

System.out.println("Exception"+e);

}

}

}







ECHOCLIENT OUTPUT:









ECHOSERVER OUTPUT:









FILE TRANSFER USING TCP/IP COMMUNICATION


CLIENT:


import java.util.*;

import java.net.*;

import java.io.*;

import java.lang.*;

public class Echoclientz

{


public static void main(String args[])


{


long len;


Socket socket=null;



try



{



S
tring host=InetAddress.getLocalHost().getHostName();



socket=new Socket(host,8085);



System.out.println("Connected with server"+socket.getInetAddress()+ ":"
+socket.getPort());



}



catch(IOException e)



{



System.out.println(e);



System.exit(1);



}




try


{


byte[] buf= new byte[1024];


OutputStream os=socket.getOutputStream();


BufferedOutputStream out=new BufferedOutputStream(os,1024);


System.out.println("Enter the file name(sending):");


DataInputStream get= new DataInputStream(System.in);


St
ring file=get.readLine();


FileInputStream in=new FileInputStream(file);

File fnlen =new File(file);

int i;

long bytecount =0;

len =fnlen.length();

System.out.println(" length of File to be transferred :" +len);


while((len = in.read(buf,0,1))!=
-
1)


{


byt
ecount= bytecount+1;


out.write(buf,0,1);


out.flush();


}


in.close();


out.close();


System.out.println("Bytes sent:"+bytecount);


}

catch(IOException e)

{

System.out.println(e);

}


try


{


socket.close();


}


catch(IOException e)


{


System.out.println(
e);


}

}

}



SERVER:


import java.net.*;

import java.io.*;


public class Echoserverz

{

public static void main(String args[])

{

ServerSocket server_socket;


try


{



server_socket = new ServerSocket(8085);


System.out.println("Server waiting for client on
port"+server_socket.getLocalPort());



while(true)




{



Socket socket =server_socket.accept();



System.out.println("New connection
accepted"+socket.getInetAddress()+":"+socket.getPort());



try



{



byte[] b=new byte[1024];



int len=0;



int bytec
ount=1;



System.out.println("Enter the name of the file to be saved:");



DataInputStream get=new DataInputStream(System.in);



String FileName=get.readLine();



FileOutputStream inFile= new FileOutputStream(FileName);



InputStream is=socket.getInputStre
am();



BufferedInputStream in=new BufferedInputStream(is,1024);




while((len=in.read(b,0,1))!=
-
1)




{








bytecount=bytecount+1;




}



in.close();



inFile.close();



System.out.println("Bytes Written:"+bytecount);



}



catch(IOException e)



{



S
ystem.out.println("Unable to open file"+e);



return;



}





try


{


server_socket.close();


System.out.println("Connection closed by client");


}


catch(IOException e)


{


System.out.println(e);


}


}


}

catch(IOException e)


{


System.out.println(e);


}


}


}







FILECLIENT OUTPUT:












FILESERVER OUTPUT:










FINDING THE IP ADDRESS OF A SYSTEM:


SOURCE CODE:


import java.io.*;

import java.net.*;

import java.lang.*;

import java.util.*;

class address

{

public static void main(String args[])

{

try

{

InetAddress i=InetAddress.getLocalHost();

System.out.println("The IP number is :"+i.getHostAddress());

System.out.println("The system name is :"+i.getHostName());

}

catch(IOException e)

{

System.out.println("ERROR");

}

}

}





















OUTP
UT:























FILE TRANSFER USING UDP:


SOURCE CODE:


SERVER:

import java.io.*;

import java.net.*;

public class FServer

{



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

{



DatagramSocket ds=new DatagramSocket(2000);



String pat
h;



DataInputStream in = new DataInputStream(System.in);



InetAddress client=InetAddress.getLocalHost();



System.out.println("Enter The File Path:");



path=in.readLine();



while(true)




{




byte buffer[]=new byte[32000];




int i=0;




F
ileInputStream fis=new FileInputStream(path);




while(fis.available()!=0)





{





buffer[i]=(byte)fis.read();





i++;




}



DatagramPacket dp=new DatagramPacket(buffer,buffer.length,client,1500);




ds.send(dp);




}



}

}











CLI
ENT:


import java.io.*;

import java.net.*;


public class Fclient{


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


{


DatagramSocket ds= new DatagramSocket(1500);


byte buffer[]=new byte[32000];


String path;


DataInputStream in=new DataInputS
tream(System.in);


System.out.println("Enter The Path To Store:");


path=in.readLine();


while(true)


{


DatagramPacket dp=new DatagramPacket(buffer,buffer.length);


ds.receive(dp);


FileOutputStream fos = new FileOutputStream(path);


fos.write
(dp.getData(),0,dp.getLength());


}


}

}





















OUTPUT:

SERVER:


CLIENT:






CYCLIC REDUNDANCY CHECK


SOURCE CODE:


import java.io.*;

import java.lang.*;

class crc

{


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


{



DataInputStream di=new DataInputStream(System.in);


int input[]=new int[25];


int div[]=new int[10];


int a[]=new int[10];


int b[]=new int[10];


int x[]=new int[10];


int temp[]=new int[25];


int output[]=new int[25];


int i,j,k,m,n,l,flag=0;


Stri
ng str;


System.out.println("Enter the number of bits for input:");


str=di.readLine();


n=Integer.parseInt(str);




System.out.println("Enter the message:");


for(i=0;i<n;i++)


{



str=di.readLine();


input[i]=Integer.parseInt(str);


}


System.ou
t.println("Enter the number of bits for divisor:");


str=di.readLine();


m=Integer.parseInt(str);


System.out.println("Enter the divisor:");


for(i=0;i<m;i++)


{



str=di.readLine();


div[i]=Integer.parseInt(str);


}



for(i=0;i<n;i++)


{


if
(input[i]!=0 && input[i]!=1)


{


System.out.println("Invalid message!!");


break;


}


}

for(i=0;i<n;i++)

temp[i]=input[i];

for(i=0;i<m;i++)


x[i]=div[i];


for(i=n;i<n+(m
-
1);i++)


input[i]=0;


System.out.println("The appended message is: "
);


for(i=0;i<n+(m
-
1);i++)


System.out.print(input[i]);


for(i=0;i<m;i++)


{


a[i]=input[i];


if(div[i]==1)


{


if(a[i]==1)


{


b[i]=0;


}


else


b[i]=1;


}


else


b[i]=a[i];


}


k=m;


while(k<n+(m
-
1))


{


for(i=0;i<4;i++)


{


a[i]=b[i+1];


}


a[m
-
1]=input[k];


if(a[0]==0)


{


for(i=0;i<m;i++)


div[i]=0;


}

else





{


for(i=0;i<m;i++)


div[i]=x[i];

}


for(i=0;i<m;i++)


{


if(div[i]==1)


{


if(a[i]==1)


{


b[i]=0;


}


else


b[i]=1;


}


else


b[i]=a[i];


}


k++;


}


System.out.println("The remainder is:");


for(i=1;i<m;i++)


System.out.print(b[i]);


l=1;


for(i=n;i<n+(m
-
1);i++)


{


temp[i]=b[l];


l++;


}


System.out.pr
intln("The transferred message is:");


for(i=0;i<n+(m
-
1);i++)


System.out.print(temp[i]);


System.out.println("Enter the received message:");


for(i=0;i<n+(m
-
1);i++)


{


str=di.readLine();


output[i]=Integer.parseInt(str);


}


for(i=0;i<m;i
++)


{


a[i]=output[i];


if(div[i]==1)






{


if(a[i]==1)


{


b[i]=0;


}


else


b[i]=1;


}


else


b[i]=a[i];


}


k=m;


while(k<n+(m
-
1))


{


for(i=0;i<4;i++)


{


a[i]=b[i+1];


}


a[m
-
1]=output[k
];


if(a[0]==0)


{


for(i=0;i<m;i++)


div[i]=0;


}

else

{


for(i=0;i<m;i++)


div[i]=x[i];

}


for(i=0;i<m;i++)


{


if(div[i]==1)


{


if(a[i]==1)


{


b[i]=0;


}


else


b[i]=1;


}


else


b[i]=a[i];







}


k++;


}


for(i=1;i<m;i++)


{


if(b[i]==0)


{


flag=1;


}


else


flag=0;


}


if(flag==1)


System.out.println("The message is transferred successfully!!");


else


System.out.println("Errors in message!!");

}

}






























OUTPUT:


TRANSFER WITHOUT ERROR:














TRANSFER WITH ERROR:

















TALK COMMAND WITH CLIENT AND SERVER IN A SINGLE
SYSTEM USING TCP/IP:


SOURCE CODE:

SERVER:


import java.io.*;

import java.net.*;

public class talkserver45

{

publi
c static void main(String[] args)

{

int port=7070;

try

{

ServerSocket server=new ServerSocket(port);

while(true)

{

System.out.println("Waiting for the client");

Socket client=server.accept();

BufferedReader in=new BufferedReader(new InputStreamReader(Syste
m.in));

System.out.println("Got connection from :"
+client.getInetAddress()+":"+client.getPort());

BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));

PrintWriter writer=new PrintWriter(client.getOutputStream());

Buffe
redReader in=new BufferedReader(new InputStreamReader(system.in));

writer.println("Welcome to server ");

writer.flush();

String message;

while(true)

{

System.out.println("Message received from client:"+reader.readLine());

System.out.println("Enter the mess
age to client :");

message=in.readLine();

if(message==null||message.equals("exit"))

break;


writer.println(message);

writer.flush();


}

client.close();

}

}

catch(Exception e)

{

System.out.println(e);

}

}

}


CLIENT:


import java.io.*;

import java.net.*;

pub
lic class talkclient45

{

public static void main(String[] args)

{

int port=8080;

try

{

String host=InetAddress.getLocalHost().getHostName();

Socket client=new Socket(host,port);

PrintWriter writer=new PrintWriter(client.getOutputStream());

BufferedReader r
eader=new BufferedReader(new
InputStreamReader(client.getInputStream()));

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

System.out.println(reader.readLine());

String message;

while(true)

{

System.out.println("enter message to echo

or exit:");

message=in.readLine();

if(message==null||message.equals("exit"))

break;

writer.println(message);

writer.flush();

System.out.println("Echo from server:"+reader.readLine());

}

client.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}




OUTPUT:

SERVER:


CLIENT:







TALK COMMAND WITH CLIENT AND SERVER BETWEEN 2
SYSTEMS USING TCP/IP:


SOURCE CODE:


SERVER:

import java.io.*;

import java.net.*;

public class talkserver

{

public static void main(String[] args)

{

int port=7070;

try

{

Serv
erSocket server=new ServerSocket(port);

while(true)

{

System.out.println("Waiting for the client");

Socket client=server.accept();

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

System.out.println("Got connection from :"
+client.ge
tInetAddress()+":"+client.getPort());

BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));

PrintWriter writer=new PrintWriter(client.getOutputStream());

writer.println("Welcome to server ");

writer.flush();

String messa
ge;

while(true)

{

System.out.println("Message received from client:"+reader.readLine());

System.out.println("Enter the message to client :");

message=in.readLine();

if(message==null||message.equals("exit"))

break;


writer.println(message);

writer.flush();


}

client.close();

}

}

catch(Exception e)

{

System.out.println(e);}}}



CLIENT:

import java.io.*;

import java.net.*;

public class talkclient45

{

public static void main(String[] args)

{

int port=8080;

try

{

String host=InetAddress.getLocalHost().getHostNam
e();

Socket client=new Socket(“172.17.10.53”,port);

PrintWriter writer=new PrintWriter(client.getOutputStream());

BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));

BufferedReader in=new BufferedReader(new InputStream
Reader(System.in));

System.out.println(reader.readLine());

String message;

while(true)

{

System.out.println("enter message to echo or exit:");

message=in.readLine();

if(message==null||message.equals("exit"))

break;

writer.println(message);

writer.flush();

System.out.println("Echo from server:"+reader.readLine());

}

client.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}




OUTPUT:

CLIENT:


OUTPUT :

SERVER:




BROADCASTING


SOURCE CODE

SERVER

import java.net.*;

import java.io.*;

public clas
s Broadsend

{


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


{



DatagramSocket socket=new DatagramSocket();



DataInputStream in=new DataInputStream(System.in);



String msg="";



String client="";



byte[] buf=new byte[1025];



while(t
rue)



{




System.out.println("SERVER...");




System.out.println("ENTER MESSAGE TO BE SENT");




msg=in.readLine();




buf=msg.getBytes();



for(int i=0;i<=10;i++)





{





try





{





client="Cl
ient"+i;





DatagramPacket packet=new
DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),5000);





socket.send(packet);


System.out.println("Message Sent Successfully to "+client);





}





catch(Exception e)





{





Sy
stem.out.println("Unable to connet at "+client+e);





}




if(msg.equalsIgnoreCase("Bye"))





{





System.out.println("Server Quits");





return;





}




}




}


}


}








CLIENT


import java.net.*;

import java.io.*;

import java.lang
.*;

public class Broadreceive

{


public static void receive()throws Exception


{



DatagramSocket socket=new DatagramSocket(5000);



while(true)



{




try





{





System.out.println("CLIENT...");




byte[] buf=new byte[1025];




DataInputStr
eam in=new DataInputStream(System.in);




String msg=null;




DatagramPacket packet =new DatagramPacket(buf,buf.length);




socket.receive(packet);




msg=new String(packet.getData(),0,packet.getLength());




InetAddress add=packet.getAddress();




System.
out.println("From:"+add+"Message from server:"+msg);





if(msg.equalsIgnoreCase("bye"))



{





System.out.println("Quits");





return;





}




}



catch(Exception e){}



}


}


public static void main(String args[])th
rows Exception



{





receive();



}

}









INPUT AND OUTPUT

CLIENT




SERVER










SHORTEST PATH ALGORITHM

SOURCE CODE:


import java.io.*;


public class shortestpath

{


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


{



int[][] a
=new int[10][10];



int[][] cost=new int[10][10];



int[] dist=new int[40];



int i,j,k,n;




DataInputStream cin=new DataInputStream(System.in);






System.out.println("Enter the no.of routers:");



n=Integer.parseInt(cin.readLine());




System.out.print
ln("Enter the distance b/w routers:");




for(i=1;i<=n;i++)



{




for(j=1;j<=n;j++)




{




if(i!=j)




{



System.out.print("Enter the distance b/w"+i+" "+j+" router ");




k=Integer.parseInt(cin.readLine());




cost[i][j]=k;




if(cost[i][j]!=0)





a[i
][j]=cost[i][j];




else





a[i][j]=32666;




}




else





a[i][j]=cost[i][j]=0;




}






}





for(k=1;k<=n;k++)



for(i=1;i<=n;i++)




for(j=1;j<=n;j++)





if(a[i][j]<(a[i][k]+a[k][j]))






a[i][j]=a[i][j];





else






a[i][j]=a[i][k]+a[k][j];








System.out.println();



for(i=1;i<=n;i++)



{





for(j=1;j<=n;j++)





System.out.print(a[i][j]+" ");




System.out.println();



}



}

}




































INPUT AND OUTPUT:





















DISTANCE VECTOR ROUTING:

SOURCE CODE:


import java.io.*;

class routerdv

{

public static void main(string args[]) throws ioexception

{

int mat[][]=new int[20][20];

int adj_dest[]=new int[20];

int final_dest[]=new int[20];

int n,adjcount;

int adj_name[]=new int[10];

int final_name[]=new int[20];

int source;

int i,j;

DataInputStream dis = new DataInputStream(System.in);

System.out.println("Routing Algorith
-

Distance Vector Method");

System.out.println();

System.out.print("No.Of Nodes in Subnet ? :");

n=Integer.parseInt(dis.readLine());

System.out
.print("Enter The Source Node Number : ");

source=Integer.parseInt(dis.readLine());

System.out.println("Enter the Number Of Nodes Adjacent to the source node
"+source);

adjcount=Integer.parseInt(dis.readLine());

System.out.println("Adjacent Node Details.."
);

for(i=1;i<=adjcount;i++)

{


System.out.print("Enter the node "+i+" : ");


adj_name[i]=Integer.parseInt(dis.readLine());

}

for(i=1;i<=adjcount;i++)

{


System.out.print(adj_name[i]);

}

System.out.println();

System.out.println();

for(i=1;i<=n;i++)

{


Syst
em.out.print(i);


for(j=1;j<=adjcount;j++)

mat[i][j]=Integer.parseInt(dis.readLine());

}

System.out.println("The Existing Routing Table Is");

for(i=1;i<=n;i++)

{


System.out.print(i);


for(j=1;j<=adjcount;j++)



System.out.print("
\
t"+mat[i][j]);


Syst
em.out.println();

}

for(i=1;i<=adjcount;i++)

{


System.out.print("Enter the estimated Cost of " +source+ " from "+adj_name[i] +" :
");


adj_dest[i]=Integer.parseInt(dis.readLine());

}

for(i=1;i<=n;i++)

{


final_dest[i]=32767;


if(source==i)


{


final_
dest[i]=0;


final_name[i]=source;


continue;



}


for(j=1;j<=adjcount;j++)


{


if((mat[i][j]+adj_dest[j])<final_dest[i])


{



final_dest[i]=mat[i][j]+adj_dest[j];



final_name[i]=adj_name[j];


}


}

}

for(i=1;i<=n;i++)

{


if(source==i)


{


final_dest[
i]=0;


final_name[i]=source;


break;


}

}

System.out.println("New Routing table for "+source);

System.out.println("
\
tDestination
\
tDelay
\
tLine");

for(i=1;i<=n;i++)

{

System.out.println("
\
t"+i+"
\
t"+final_dest[i]+"
\
t"+final_name[i]);

}}}


SAMPLE INPUT AND OU
TPUT: