Simple Chat Application :
Client Side:
import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=d.readLine();
ps.println("Client : "+str);
str=dis.readLine();
System.out.println(str);
}
}
}
Server side:
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
str=d.readLine();
ps.println("Server : "+str);
}
}
}
..................................................................................................................................................................
file transfer using TCP/IP
In this you are gona send a file named velan.txt in server to vadi.txt in client side . You shd priorly hav a file velan.txt
Client side:
import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
byte[] str=new byte[1024];
Socket s=new Socket("localhost",7);
DataInputStream dis=new DataInputStream(s.getInputStream());
FileOutputStream fos=new FileOutputStream("vadi.txt");
while(true)
{
if(dis.read(str)!=-1)
fos.write(str);
else
break;
}
}
}
Server side:
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
byte[] str=new byte[1024];
ServerSocket ss=new ServerSocket(7);
Socket s=ss.accept();
DataOutputStream ps=new DataOutputStream(s.getOutputStream());
FileInputStream fis=new FileInputStream("velan.txt");
while(true)
{
fis.read(str);
ps.write(str);
if(fis.read(str)==-1)
break;
}
}
}
..................................................................................................................................................................
Remote Command Execution:
Sender side:
import java.io.*;
import java.lang.*;
import java.net.*;
class sender
{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream ss=new DataInputStream(System.in);
while(true)
{
System.out.println("Enter the Command to send:");
String str=ss.readLine();
ps.println(str);
str=dis.readLine();
System.out.println(str);
}
}
}
Receiver side:
import java.io.*;
import java.net.*;
import java.lang.*;
class receiver
{
public static void main(String args[])throws Exception
{
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
while(true)
{
Runtime r=Runtime.getRuntime();
String str=dis.readLine();
r.exec(str);
ps.println("Command Executed Successfully");
}
}
}
..................................................................................................................................................................
Sliding Window :
Server side :
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
int n;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
System.out.println("Enter the window size:");
n=Integer.parseInt(d.readLine());
ps.println(String.valueOf(n));
while(n>0)
{
str=d.readLine();
ps.println(str);
n--;
}
str=dis.readLine();
System.out.println("Acknowledgement Received For "+str+" Packets");
}
}
}
Client side:
import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
int n=Integer.parseInt(dis.readLine());
int i=0;
while(n>0)
{
i++;
str=dis.readLine();
System.out.println("Received : "+str);
n--;
}
ps.println(String.valueOf(i));
}
}
}
................................................................................................................................................................
Stop and Wait protocol:
Server side:
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
int k=1;
while(true)
{
if(k==0)
k=1;
else
k=0;
System.out.println("Enter the Data To send:");
str=d.readLine();
str=str+String.valueOf(k);
ps.println("Server : "+str);
int i=Integer.parseInt(dis.readLine());
if(i==1)
System.out.println("Acknowledgement Received");
else
System.out.println("Acknowledgement not Received Send the Packet again");
}
}
}
Client side:
import java.net.*;
import java.io.*;
class clent
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
ps.println(String.valueOf(1));
}
}
}
................................................................................................................................................................
Unix Socket programming:
Server Side:
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
int port=1600,ch=0;
System.out.println("port address is 1600(default) enter 1 to change it else enter 0:");
DataInputStream d=new DataInputStream(System.in);
ch=Integer.parseInt(d.readLine());
if(ch==1)
port=Integer.parseInt(d.readLine());
ServerSocket ss=new ServerSocket(port);
Socket s=ss.accept();
System.out.println("Server Reading");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
while(true)
{
if((str=dis.readLine())!=null)
System.out.println(str);
else
{System.out.println("Connection Terminated");break;}
str=d.readLine();
ps.println("Server : "+str);
}
}
}
Client Side:
import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
int port=1600,ch=0;
System.out.println("port address is 1600(default) enter 1 to change it else enter 0:");
DataInputStream d=new DataInputStream(System.in);
ch=Integer.parseInt(d.readLine());
if(ch==1)
port=Integer.parseInt(d.readLine());
Socket s=new Socket("localhost",port);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
while(true)
{
if((str=d.readLine())!=null)
ps.println("Client : "+str);
else
{
System.out.println("Connection Terminated");
break;
}
str=dis.readLine();
System.out.println(str);
}
}
}
.................................................................................................................................................................
Windows Socket Programming:
Server Side:
import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(231);
Socket s=ss.accept();
System.out.println("Server Reading");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
str=d.readLine();
ps.println("Server : "+str);
}
}
}
Client Side:
import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",231);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=d.readLine();
ps.println("Client : "+str);
str=dis.readLine();
System.out.println(str);
}
}
}
...................................................................................................................................................................
No comments:
Post a Comment