Saturday 20 April 2013

client-server socket.




AIM:
                  Program for client-server socket.

THEORY:
            A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
            On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.
A client's connection request
            If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
The connection is made
            On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.The client and server can now communicate by writing to or reading from their sockets.Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.
.
Running the Programs
            You must start the server program first. To do this, run the server program using the Java interpreter, just as you would any other Java application. Remember to run the server on the machine that the client program specifies when it creates the socket.
            Next, run the client program. Note that you can run the client on any machine on your network; it does not have to run on the same machine as the server.
            If you are too quick, you might start the client before the server has a chance to initialize itself and begin listening on the port. If this happens, you will see a stack trace from the client. If this happens, just restart the client.

CONCLUSION :
            Sockets are the means by which computers on a network communicate











































PROGRAM:

Client Server Socket

//SERVER:
import java.io.*;
import java.net.*;
public class Server
{
            public Server()
            {}
            public static void main(String args[])
            {
                        try
                        {
                                    String num1 = null;
                                    ServerSocket server = new ServerSocket(8080);
                                    Socket s= server.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                                    PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                                    num1 = in.readLine();
                                    System.out.println("The requested integer number is: "+ num1);
                                    int i=Integer.parseInt(num1);
                                    out.println(Integer.toHexString(0x10000|i).substring(1).toUpperCase());
                                    int binry[]= new int[8];
                                    int octal[] = new int[4];
                                    binaryconversion(binry,i);
                                    octalconv(octal,i);
                                    System.out.println();
                                    num1=Integer.toString(binry[0]);
                                    for(int p =1; p<8;p++)
                                    {
                                                num1=num1 + Integer.toString(binry[p]);
                                    }
                                    //binary number
                                    out.println(num1);
           
                                    num1=Integer.toString(octal[0]);
                                    for(int p =1; p<4;p++)
                                    {
                                                num1=num1+Integer.toString(octal[p]);
                                    }
                                    //octal number
                                    out.println(num1);
                        }
                        catch(IOException a)
                        {
                                    System.out.println("Error in input");
                        }
            }

            public static void binaryconversion(int binry[], int i)
            {
                        int k;
                        for(k=0;k<8;k++)
                                    binry[k]=0;
                        k=7;

                        for(int a=i;a>0;)
                        {
                                    binry[k]=a%2;
                                    a=a/2;
                                    k--;
                        }
            }
            public static void octalconv(int octal[], int i)
            {
                        int k;
                        for(k=0;k<4;k++)
                                    octal[k]=0;
                        k=3;

                        for(int a=i;a>0;)
                        {
                                    octal[k]=a%8;
                                    a=a/8;
                                    k--;
                        }
            }
}

OUTPUT
The requested integer number is: 42
                                            


























                                                                Client Server Socket

//CLIENT:
import java.io.*;
import java.net.*;
public class client
{
public client()
{}
public static void main(String args[])throws IOException
{
String num1 = null;
Socket s = new Socket("localhost",8080);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number for conversion : ");
num1 = br.readLine();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println(num1);
num1 = in.readLine();
System.out.println("The HEX number is:" + num1);

num1 = in.readLine();
System.out.println("The binary number is:" + num1);

num1 = in.readLine();
System.out.println("The Octal number is:" + num1);
}
}

OUTPUT:
Enter the number for conversion :
42
The HEX number is:002A
The binary number is:00101010
The Octal number is:0052


Objective Questions:
1.Java uses ______ class representing a server and ______ class representing the client that uses TCP protocol.
2. ______  is used to wait for accepting client connection requests.
3. In order to create a client socket connecting to a particular server, the IP address must be given to the client socket, otherwise, it cannot connect to the server: True or False.
4. Sockets provide an interface for programming networks at the transport layer: True or False.
5. Java TCP Socket uses the InputStream/OutputStream to read/write data to the network channel:
True or False.
6. Call Socket.close() method will close the TCP server that socket connects to: True or False.
7. The socket instance does not need to be explicitly closed to release all the resources it occupies, as the Java Garbage Collection mechanism will do it automatically: True or False
8. The following line of code
  Socket socket = new Socket(“localhost,” 1254);
  will create a TCP server at localhost port 1254: True or False.

No comments:

Post a Comment