In Java socket programming(Network programming) we basically study about how to implement programs that communicate over a network. When we use java to implement those kind of applications
java.net package will be used because it contains collection of classes and interfaces that provide low level communication details. It provides support for
TCP and
UDP which are two major network protocols.
Process between client and a server
- First client program creates a socket and try to connect to a socket in a server. That server socket should be already created and ready for client requests.
- After a connection is made server and client communicate by writing and reading from the socket.
What happens when establishing a TCP connection in java
- Server instantiate a ServerSocket using a needed port number.
- Then Wait for client requests by invoking accept() method.
- Client instantiate a Socket object by giving server name and port number of the server.
- Socket constructor of the client attempt to connect to server and if it is succeeded then there is a Socket object that can communicate over a network.
- On the server side accept() method returns a reference to a new socket on the server that is connected to a client socket
- Finally both client and server write and read from the their socket objects.
Java Implementation for a TCP server
import java.net.*;
import java.io.*;
public class Server1 extends Thread
{
private ServerSocket serverSocket;
public Server1(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(100000);
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "+ server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = 1234;
try
{
Thread t = new Server1(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Java Implementation for a TCP Client
import java.io.*;
import java.net.*;
public class Client1 {
public static void main(String [] args)
{
String serverName = "localhost";
int port = 1234;
try
{
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Connected to " + client.getRemoteSocketAddress());
OutputStream toServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(toServer);
out.writeUTF("Hello I'm " + client.getLocalSocketAddress());
InputStream fromServer = client.getInputStream();
DataInputStream in = new DataInputStream(fromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment