steps for writting server program
STEPS
1)Create the object of serversocket class
ServerSocket server = new ServerSocket(2345);
this statement creates socket for the server and the argument represents the port number to which the server listens.
2)Accept the connection from the clint
socket clint=Server.accept();
after creating the socket it enters into the infinite loop and waits for the clint connection by using accept method.
3) Get input and output stream of the socket
Scanner ins=new Scanner(server.getInputStream());
PrintWriter outs =new PrintWriter(server.getOutputStream(),true);
the first statement gets inputstream of server's socket and opens scanner on it,similerly,the second statement gets output stream of server's socket and opens Printwriters on it.
4) Read/write data to the socket
rs=ins.nextLine();
outs.println(ss);
the first statement reads a line from server's socket and puts it in variable 'rs' and the second statement writes the value of string variable 'ss' into server's socket.
5) Close stream
ins.close();
outs.close();
once i/o is complited,above statements are used to close inpt stream and output stream of clint's socket.
6)close socket
server.close();
clint.close();
we use statements to close the connection between clint and server computers.
1)Create the object of serversocket class
ServerSocket server = new ServerSocket(2345);
this statement creates socket for the server and the argument represents the port number to which the server listens.
2)Accept the connection from the clint
socket clint=Server.accept();
after creating the socket it enters into the infinite loop and waits for the clint connection by using accept method.
3) Get input and output stream of the socket
Scanner ins=new Scanner(server.getInputStream());
PrintWriter outs =new PrintWriter(server.getOutputStream(),true);
the first statement gets inputstream of server's socket and opens scanner on it,similerly,the second statement gets output stream of server's socket and opens Printwriters on it.
4) Read/write data to the socket
rs=ins.nextLine();
outs.println(ss);
the first statement reads a line from server's socket and puts it in variable 'rs' and the second statement writes the value of string variable 'ss' into server's socket.
5) Close stream
ins.close();
outs.close();
once i/o is complited,above statements are used to close inpt stream and output stream of clint's socket.
6)close socket
server.close();
clint.close();
we use statements to close the connection between clint and server computers.
Comments
Post a Comment