Socket Programming in Java:
Steps:
1) Run the Server program.
2) Run the Client program.
3) Type anything on Client to Send. Type type Over to exit.
//Socket for Server
package socket;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(1101); //Server makes a new Socket to communicate with the client and starts Listening.
System.out.println("Server started.");
Socket sk = server.accept(); //accept() method blocks(just sits) until a client connects to the server.
System.out.println("Client connected to Server Socket.");
//Takes input from the client socket
InputStream is = sk.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
//DataInputStream dis = new DataInputStream(is); //This will also work. BufferedInputStream is not required here.
//String line = dis.readLine(); //readLine() displays some junk too
String line = dis.readUTF(); //No junk
System.out.println("Server received: "+line);
//Reads message from client until "Over" is sent.
while (!line.equals("Over")) {
try {
line = dis.readUTF();
System.out.println("Server Received: "+line);
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
//Close connection
sk.close();
dis.close();
}
}
//Socket for Client
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class SocketClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("127.0.0.1", 1101); //If wrong config: java.net.ConnectException: Connection refused
System.out.println("Client got connected to Server.");
String msg = "Hello Socket";
DataOutputStream out = new DataOutputStream(sk.getOutputStream());
out.writeUTF(msg); //Sends out to the socket
out.flush(); //This says, I have no more data.
System.out.println("Client Sent: "+msg);
String line="";
DataInputStream dis = new DataInputStream(System.in);
while (!line.equals("Over")) {
try {
line = dis.readLine();
out.writeUTF(line);
System.out.println("Client Sent: "+line);
}
catch(IOException i) {
i.printStackTrace();
}
}
try {
out.close();
sk.close();
} catch(IOException i) {
System.out.println(i);
i.printStackTrace();
}
}
}
NOTE:
Run Server, Run Client--> Works fine.
Run Server, Run Multiple Clients..... --> type messages in all clients, Server will receive msges from first Connected Client Only.
Server will ignore other clients than the first connected clients. This shows Socket is one-to-one connectivity only.
Wednesday, April 18, 2018
Socket Programming in Java
Thursday, April 5, 2018
BlockingQueue Example Producer Consumer Basic
import java.util.LinkedList;
import java.util.Queue;
public class ProducerConsumer {
static Queue queue = new LinkedList();
static int capacity=3;
public static void main(String[] args) {
final Worker work = new Worker();
Thread t1 = new Thread(new Runnable(){
public void run(){
try {
work.produce();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable(){
public void run(){
try {
work.consume();
} catch (Exception e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
static class Worker {
int number=0;
public void produce() throws Exception {
while(true){
synchronized(this){
while(queue.size()==capacity){
System.out.println("Waiting to get Removed...");
wait();
}
number++;
queue.add(number);
System.out.println("Added: "+number);
notifyAll();
Thread.sleep(500);
}
}
}
public void consume() throws Exception {
while(true){
synchronized(this){
while(queue.isEmpty()){
System.out.println("Waiting to get Added...");
wait();
}
Object t = queue.remove();
System.out.println("Removed: "+t);
notifyAll();
Thread.sleep(500);
}
}
}
}
}
//Output
Added: 1
Added: 2
Added: 3
Removed: 1
Added: 4
Waiting to get Removed...
Removed: 2
Removed: 3
Removed: 4
Waiting to get Added...
Added: 5
Added: 6
Removed: 5
Removed: 6
Waiting to get Added...
Added: 7
Subscribe to:
Posts (Atom)