Beruflich Dokumente
Kultur Dokumente
Examples
Discussion in 'Java' started by techgeek.in, Feb 24, 2010.
1.
techgeek.inNew Member
JOINED:
MESSAGES:
572
LIKES RECEIVED:
19
TROPHY POINTS:
OCCUPATION:
LOCATION:
HOME PAGE:
http://www.techgeek.in
A socket forms the interface between the protocol and client for communication. A
Java socket forms the base for data transmission between two computers using
TCP/IP. The socket specifies the site address and the connection port. When
packets reach a client or server, they are routed to the destination port specified in
packet header.
The java.net package contains the classes and interfaces required for networking.
Some important classes are MulticastSocket, ContentHandler, URLServerSocket,
Socket, InetAddress, URLConnection, DatagramSocket, and DatagramPacket.
Some important interfaces in the java.net package are ContentHandlerFactory,
SocketImplFactory, FileNameMap, URLStreamHandlerFactory, and SocketOptions.
InetAddress
This class encapsulates the numerical IP address and the domain name for the
address. Factory methods of a class allow you to call the method without referencing
the object. The factory methods of this class are:
getLocalHost() method: Returns the name of the local
computer
getByName() method: Returns the address by the Domain name
getAllByName() method: Returns all the addresses by their
domain name
The instance methods of a class are methods that can be called
from an object only. The instance methods for the class are:
getAddress() method: Returns a four-element byte array that
represents the objects IP address in network byte order
getHostAddress() method: Returns the host address
getHostName() method: Returns the hostname that is
associated with the host address.
Where, hostname1 is a string type variable that refers to the destination address and
port1 refers to the port number of the destination address. This method can throw
the exceptions, UnknownHostException or IOException, in case of errors.
By specifying an object of InetAddress and the port number as
follows:
Where, ipaddr1 is object of the InetAddress class and port1 refers to the port number
of the destination. This method can throw the exception IOException, in case of
errors.
Methods of the Socket Class
int getPort() -- Returns the port number on which the socket is connected
int getLocalPort() -- Returns the local port number on which the socket is created
//filename ExSocket.java
import java.io.*;
import java.net.*;
public class ExSocket {
public static void main(String args[]) throws UnknownHostException
{
try
{
Socket mySocket = new Socket("www.my-site.com",80);
System.out.println("Connection to: " +
mySocket.getInetAddress());
System.out.println("Port Number: " + mySocket.getPort());
System.out.println("Local Address: " +
mySocket.getLocalAddress());
System.out.println("Local Port: " +
mySocket.getLocalPort());
}
catch (UnknownHostException e)
{
System.out.println("Site not found!");
}
catch (SocketException e)
{
System.out.println("Socket error");
}
catch ( IOException e)
{
System.out.println("An I/O Exception Occurred!");
}
}
}
EXPLANATION:-
The TCP/IP server socket creates a socket that listens for incoming connections.
The server socket is implemented by creating an instance of the ServerSocket class.
The server socket creates a server on the system to detect client connections.
An instance of the ServerSocket class can create a server that accepts incoming
requests. An object of the ServerSocket class can be created using any one of these
methods:
Specify the port number in the int type. An example of this
method is:
ServerSocket(int port1)
Where, port1 is an integer, which can have any value between 0 and 65,536.
Specify the port number and maxQueue. maxQueue refers to
the number of connections the socket can leave pending before
refusing more connections.
An example of this method is:
Where, maxQ refers to an int value that specifies the number of connections it can
leave pending before refusing any more connections.
Specify the port number, maxQueue, and the local address. An
example of this method is:
int getLocalPort() -- Returns the port number on which the server socket is
listening.
void close() -- Closes the server socket.
Code:
EXPLANATION:-
TWO EXAMPLES
1. A PROGRAM ON ECHO SERVER
import java.net.*;
import java.io.*;
class clientsock
{
public static void main(String args[]) throws IOException
{
System.out.println("sending request to server....");
Socket client=new Socket("127.0.0.1",1235);
System.out.println("successfully conneted");
BufferedReader br1=new BufferedReader(new
InputStreamReader(System.in));
PrintStream ps=new PrintStream(client.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(client.getInputStream()));
while(true)
{
System.out.println("input the data u want to send to echo
server: ");
String s=br1.readLine();
ps.println(s);
if(s.equals("exit"))
{
System.exit(1);
}
String st=br.readLine();
System.out.println("data returned by the server: ");
System.out.println(st);
}
}
}
import java.net.*;
import java.io.*;
class serversock
{
public static void main(String args[]) throws IOException
{
ServerSocket server=new ServerSocket(1235);
System.out.println("waiting for request from client");
Socket client=server.accept();
System.out.println("request accepted");
2. A CHAT PROGRAM
CLIENT SIDE CODE
Code:
import java.net.*;
import java.io.*;
}
public void run()
{
try{
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class clientsock
{
public static void main(String args[]) throws IOException
{
try{
System.out.println("sending request to peer....");
Socket client=new Socket("127.0.0.1",1300);
System.out.println("successfully conneted");
clie c=new clie(client);
import java.net.*;
import java.io.*;
}
}
}
class serverchat
{
public static void main(String args[]) throws IOException
{
(NOTE:- If anyone facing any problem regarding the above concept and codes then
feel free to ask me questions. Give positive feedback if you really liked the article.
Give negative feedback as well to make the above concept well explained further.
No concept should be left unexplained. Thank you