DarkWebCode is a place where you can find a lot of exciting and inspiring stuff about Web Development, Blogging, SEO and many more

Breaking

Applications usingTCP Sockets

Applications usingTCP Sockets

A. Echo client and Echo server

Aim

To write a java program for  application using TCP Sockets Links


Algorithm

1.Start the program.

2.Get the frame size from the user

3.To create the framebased on the user request.
 4.To send frames to server from the client side.

5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to client.

6.Stop the program

EchoServer.java
import java.net.*;
import java.io.*;
public class EServer
{
            public static void main(String args[])
            {
                        ServerSocket s=null;
                        String line;
                        DataInputStream is;
                        PrintStream ps;
                        Socket c=null;
                        try
                        {
                                    s=new ServerSocket(9000);
                        }
                        catch(IOException e)
                        {
                                    System.out.println(e);
                        }
                        try
                        {
                                    c=s.accept();
                                    is=new DataInputStream(c.getInputStream());
                                    ps=new PrintStream(c.getOutputStream());
                                    while(true)
                                    {
                                                line=is.readLine();
                                                ps.println(line);
                                    }
                        }
                        catch(IOException e)
                        {
                                    System.out.println(e);
                        }
            }
}


EClient.java

import java.net.*;
import java.io.*;
public class EClient
{
            public static void main(String arg[])
            {
                        Socket c=null;
                        String line;
                        DataInputStream is,is1;
                        PrintStream os;
                        try
                        {         
                                    InetAddress ia = InetAddress.getLocalHost();
                                    c=new Socket(ia,9000);
                        }
                        catch(IOException e) 
                        {
                                    System.out.println(e);
                        }
                        try
                        {
                                    os=new PrintStream(c.getOutputStream());
                                    is=new DataInputStream(System.in);
                                    is1=new DataInputStream(c.getInputStream());
                                    while(true)
                                    {
                                    System.out.println("Client:");
                                                line=is.readLine();
                                                os.println(line);
                                                System.out.println("Server:" + is1.readLine());
                                    }}
                                   
                        catch(IOException e)
                        {
                                    System.out.println("Socket Closed!");
                        }
            }
}

Output
Server
C:\Program Files\Java\jdk1.5.0\bin>javac EServer.java
Note: EServer.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EServer
C:\Program Files\Java\jdk1.5.0\bin>
Client
C:\Program Files\Java\jdk1.5.0\bin>javac EClient.java
Note: EClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EClient
Client:
Hai Server
Server:Hai Server
Client:
Hello
Server:Hello
Client:
end
Server:end
Client:
ds
Socket Closed!























B.Chat


Aim
Write a Program client –server application for chat using UDP Sockets
Program
UDPserver.java
import java.io.*;
import java.net.*;
class UDPserver
{
            public static DatagramSocket ds;
            public static byte buffer[]=new byte[1024];
            public static int clientport=789,serverport=790;
            public static void main(String args[])throws Exception
            {
                        ds=new DatagramSocket(clientport);
                        System.out.println("press ctrl+c to quit the program");
                        BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
                        InetAddress ia=InetAddress.geyLocalHost();
                        while(true)
                        {
                                    DatagramPacket p=new DatagramPacket(buffer,buffer.length);
                                    ds.receive(p);
                                    String psx=new String(p.getData(),0,p.getLength());
                                    System.out.println("Client:" + psx);
                                    System.out.println("Server:");
                                    String str=dis.readLine();
                                    if(str.equals("end"))
                                                break;
                                    buffer=str.getBytes();
                                    ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
                        }
            }
}





UDPclient.java
import java .io.*;
import java.net.*;

class UDPclient
{
            public static DatagramSocket ds;
            public static int clientport=789,serverport=790;
            public static void main(String args[])throws Exception
            {
                        byte buffer[]=new byte[1024];
                        ds=new DatagramSocket(serverport);
                        BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("server waiting");
                        InetAddress ia=InetAddress.getLocalHost();
                        while(true)
                        {
                                    System.out.println("Client:");
                                    String str=dis.readLine();
                                    if(str.equals("end"))
                                                break;
                                    buffer=str.getBytes();
                                    ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
                                    DatagramPacket p=new DatagramPacket(buffer,buffer.length);
                                    ds.receive(p);
                                    String psx=new String(p.getData(),0,p.getLength());
                                    System.out.println("Server:" + psx);
                        }
            }
}

















OUTPUT:

Server

C:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.java

C:\Program Files\Java\jdk1.5.0\bin>java UDPserver
press ctrl+c to quit the program
Client:Hai Server
Server:
Hello Client
Client:How are You
Server:
I am Fine


Client

C:\Program Files\Java\jdk1.5.0\bin>javac UDPclient.java

C:\Program Files\Java\jdk1.5.0\bin>java UDPclient
server waiting
Client:
Hai Server
Server:Hello Clie
Client:
How are You
Server:I am Fine
Client:
end


















C. File Transfer
Program

File Client


import java.io.*; import java.net.*; import java.util.*; class Clientfile

{              public static void main(String args[])

{

Try

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); Socket clsct=new Socket("127.0.0.1",139);

DataInputStream din=new DataInputStream(clsct.getInputStream()); DataOutputStream dout=new DataOutputStream(clsct.getOutputStream()); System.out.println("Enter the file name:");
String str=in.readLine(); dout.writeBytes(str+'\n'); System.out.println("Enter the new file name:"); String str2=in.readLine();

String str1,ss;

FileWriter f=new FileWriter(str2); char buffer[];

while(true)

{ str1=din.readLine(); if(str1.equals("-1")) break; System.out.println(str1); buffer=new char[str1.length()];

str1.getChars(0,str1.length(),buffer,0);

f.write(buffer);

}

f.close();

clsct.close();

}

catch (Exception e)

{

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



Server

import java.io.*; import java.net.*; import java.util.*; class Serverfile

{ public static void main(String args[])

{

Try

{

ServerSocket obj=new ServerSocket(139);
while(true)

{

Socket obj1=obj.accept();

DataInputStream din=new DataInputStream(obj1.getInputStream()); DataOutputStream dout=new DataOutputStream(obj1.getOutputStream()); String str=din.readLine();

FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;

while((s=b.readLine())!=null) { System.out.println(s);

dout.writeBytes(s+'\n');

}

f.close(); dout.writeBytes("-1\n");

}              }

catch(Exception e)

{              System.out.println(e);}

}

}


Output

File content Computer networks jhfcgsauf

jbsdava jbvuesagv client

Enter the file name: sample.txt

server

Computer networks jhfcgsauf

jbsdava jbvuesagv client

Enter the new file name: net.txt

Computer networks jhfcgsauf

jbsdava jbvuesagv Destination file

Computer networks jhfcgsauf

jbsdava jbvuesagv