View RSS Feed

Java Socket

Client Sockets

Rate this Entry
by , 04-25-2012 at 10:18 PM (460 Views)
Client sockets are handled by a class which is in java.net package. This line would be included generally;
import java.net.Socket;

So that package methodís short names could be used. Number of constructors could be used for creation of new [client] socket. This depends upon the specific circumstances. Simplest one is:
Java Code:
 Socket(String host, int port)
that connects to machine hostís port. For example,
Java Code:
Socket s = new Socket("this.doesnt.exist.com", 1024);
To read or write to/from socket, Standard Java I/O methods are used. This code given below is used to open the socket and also to attach the input or output streams with it:
Java Code:
import java.net.*;
import java.io.*;

Socket s = new Socket("this.doesnt.exist.com", 1024);
BufferedReader in = new BufferedReader(
               new InputStreamReader(s.getInputStream()));
PrintStream out =
            new PrintStream(s.getOutputStream());

Submit "Client Sockets" to Facebook Submit "Client Sockets" to Digg Submit "Client Sockets" to del.icio.us Submit "Client Sockets" to StumbleUpon Submit "Client Sockets" to Google

Categories
Tutorial

Comments