Sunday, February 9, 2014

Use sockets in java

<<Previous   Next>>
Java - use sockets in java - Feb 28, 2010 at 16:16 PM by Vidya Sagar

Explain how to create and use sockets in java.

Sockets are created for client and server. The ServerSocket class is used for creating sockets for server. The Socket class is used for creating sockets for client side.
After creating the sockets, these sockets are utilized for both writing and writing data onto the streams. For writing data onto the output stream using client socket, the method getOutputStream() is sent as parameter to the designated output stream object, such as DataOutputStream. For reading data from the input stream using client socket, the method getInputStream() is sent as parameter to the designated inpout stream object, such as DataInputStream.
For placing data on the server socket, the method accept() is used. The result of accept() is assigned to the client socket.
Java - use sockets in java - Jan 15, 2009 at 8:10 am by Amit Satpute

Explain how to create and use sockets in java. 

Client Side:
      //creating client socket
      socket=new Socket("localhost",4000); //InetAddress.getByName ("127.0.0.1")
      //cretaing output stream for writting a data
      write=new DataOutputStream(socket.getOutputStream());
               //reading from a console
      read=new DataInputStream(System.in);
      String data=null;
            while(true)
            {
                       data=read.readLine();
                       write.writeUTF(data);
             }
Server :
//creating severSocket object.
           server=new ServerSocket(4000, 2);
           System.out.println("Socket open");
            //accept() method listens for a connection to be made to this socket and accepts it.
           client=server.accept();
           System.out.println("Client connected");
                    //receive inputs from client
           receive=new DataInputStream(client.getInputStream());
           String data=null;
            while(true)
            {
               data=receive.readUTF();
              System.out.println(data);
            }


<<Previous   Next>>

3 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

jordan pas cher, chanel handbags, nike outlet, michael kors pas cher, kate spade outlet, replica watches, longchamp pas cher, nike free, jordan shoes, christian louboutin shoes, nike free run, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, polo ralph lauren, ugg boots, christian louboutin uk, air max, louis vuitton outlet, ugg boots, tiffany jewelry, polo ralph lauren outlet online, burberry pas cher, prada outlet, nike air max, gucci handbags, sac longchamp pas cher, ray ban sunglasses, louboutin pas cher, louis vuitton, uggs on sale, tiffany and co, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, longchamp outlet, louis vuitton, longchamp outlet, replica watches, nike roshe, polo outlet, oakley sunglasses, cheap oakley sunglasses, oakley sunglasses wholesale, christian louboutin, christian louboutin outlet, tory burch outlet

oakleyses said...

lululemon canada, nike air max, burberry outlet, oakley pas cher, burberry handbags, coach outlet store online, kate spade, michael kors outlet, michael kors, nike air force, true religion jeans, true religion outlet, michael kors, polo lacoste, nike tn, new balance, abercrombie and fitch uk, michael kors outlet, uggs outlet, michael kors outlet, ralph lauren uk, michael kors outlet online, replica handbags, coach outlet, true religion outlet, coach purses, nike free uk, sac vanessa bruno, mulberry uk, michael kors outlet online, michael kors outlet online, michael kors outlet online, north face, uggs outlet, converse pas cher, hogan outlet, nike air max uk, hollister pas cher, sac hermes, nike roshe run uk, hollister uk, nike air max uk, true religion outlet, timberland pas cher, vans pas cher, ray ban pas cher, guess pas cher, ray ban uk