A simple socket program using Java
The socket connection has two major parts, the code that executes at client-side and the code that executes at server-side. So using the functionality of sockets can be partitioned into two major steps:
- The server or the server-side code
- The client or the client-side code
Server:
/*Server side program*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer
{
public static void main(String[] args) throws Exception
{
ServerSocket server=new ServerSocket(8);
while(true)
{
System.out.println("Waiting for client Request.....");
Socket socket=server.accept();
System.out.println("Client : "+new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("hi welcome");
}
}
}
public static void main(String[] args) throws Exception
{
ServerSocket server=new ServerSocket(8);
while(true)
{
System.out.println("Waiting for client Request.....");
Socket socket=server.accept();
System.out.println("Client : "+new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("hi welcome");
}
}
}
In the above program ServerSocket server=new ServerSocket(8),
server is a server side socket.
The server listening the client request through port number 8.
One point to keep in mind is that the above mentioned constructors return TCP sockets and not UDP sockets.
Socket socket; is used communicate with client.server.accept() method is return the socket, which is connected to server.
ObjectInputStream() is used to get the message from client through connected Socket.
ObjectOutputStream() is used to send the message to client through connected Socket.
Client:
/* Client program*/
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleClient
import java.net.InetAddress;
import java.net.Socket;
public class SimpleClient
{
public static void main(String[] args) throws Exception
{
Socket socket=new Socket(InetAddress.getLocalHost(),8);
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hi how r u");
System.out.println("From Server : "+new ObjectInputStream(socket.getInputStream()).readObject());
}
}
public static void main(String[] args) throws Exception
{
Socket socket=new Socket(InetAddress.getLocalHost(),8);
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hi how r u");
System.out.println("From Server : "+new ObjectInputStream(socket.getInputStream()).readObject());
}
}
Here socket is an object used to connect with Server. The constructor socket(InetAddress.getLocalHost(),8) has tow arguments ,
- InetAddress.getLocalHost() is the address of server,here the server and client program are running on a single host, so the method getLocalHost() is called.
- 8 is the port number of server,which is listening.
ObjectInputStream() is used to get the message from server through connected Socket.
Copy and paste the above two programs and save as SimpleServer.java and SimpleClient.java respectively.
Compile it, and run the two programs in different command prompt window.
It is very simpl program.
ReplyDeleteThank you,