Monday, September 11, 2017

Socket Programming in Java

Source Code:

client Side:



package learning;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;



public class DailyAdviceClient {
 
 
 private JTextArea incoming;
 private JTextField outgoing;
 private Socket sock;
 private BufferedReader reader;
 private PrintWriter writer;
 public void go() {
  
  JFrame frame =new JFrame("Client");
  JPanel mainPanel=new JPanel();
  incoming =new JTextArea(15,30);
     incoming.setLineWrap(true);
     incoming.setWrapStyleWord(true);
        incoming.setEditable(false);
        JScrollPane jScrollPane=new JScrollPane(incoming);
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outgoing=new JTextField(20);
        JButton sendButton =new JButton("Send");
        sendButton.addActionListener(new sendButtonListener());
        mainPanel.add(jScrollPane);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);
        
        
        setUpNetworking();
    
        //IncomingReader is Runnable which override run method
        Thread readerThread =new Thread(new IncomingReader());
        readerThread.start();
  
        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400, 500);
        frame.setVisible(true);
 }
 private void setUpNetworking() {
  try {
   sock=new Socket("127.0.0.1",6000);
   InputStreamReader streamReader=new InputStreamReader(sock.getInputStream());
   reader=new BufferedReader(streamReader);
   writer =new PrintWriter(sock.getOutputStream());
   System.out.println("Networking established");
   
   
  }catch(IOException e) {
   e.printStackTrace();
  }
  
 }
 
 public class sendButtonListener implements ActionListener{

  @Override
  public void actionPerformed(ActionEvent arg0) {
   try {
    writer.println(outgoing.getText());
    writer.flush();
    
   }catch(Exception e) {
    e.printStackTrace();
   }
   outgoing.setText("");
   outgoing.requestFocus();
   
  }
  
 }
 public class IncomingReader implements Runnable{

  @Override
  public void run() {
   String message;
   try {
    while((message=reader.readLine())!=null) {
   
  
     incoming.append(message+"\n");
    }
   }catch(Exception e) {
    e.printStackTrace();
   }
   
  }
  
 }
 
 
 
 public static void main(String[] args) {
  DailyAdviceClient adviceServer=new DailyAdviceClient();
  adviceServer.go();
 }
}




Server Side:



 
package learning;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

import multithreading.TheThread;

public class DailyAdviceServer {
 
ArrayList clientOutputStream;
private PrintWriter writer;
public class ClientHandler implements Runnable{

 BufferedReader reader;
 Socket sock;
 /*
  * constructor of clienthandere class
  */
 public ClientHandler( Socket clientSocket) {

  try {
   sock=clientSocket;
   InputStreamReader isReader =new InputStreamReader(sock.getInputStream());
   reader =new BufferedReader(isReader);
   
   
  }catch(Exception e) {
   e.printStackTrace();
  }
  
 }
 
 
 
 @Override
 public void run() {

  String message;
  try {
   while((message=reader.readLine())!=null) {
    System.out.println("read"+message);
    tellEverone(message);
   }
  }catch(Exception e) {
   e.printStackTrace();
  }
  

 }
 
}

public static void main(String[] args) {
 new DailyAdviceServer().go();
 System.out.println(new DailyAdviceServer().clientOutputStream);
}
 

public void go() {
 clientOutputStream=new ArrayList<>();
 try {
  ServerSocket serverSocket=new ServerSocket(6000);
  while(true) {
   Socket clientSocket=serverSocket.accept();
    writer =new PrintWriter(clientSocket.getOutputStream());
   clientOutputStream.add(writer);
   
   Thread t=new Thread(new ClientHandler(clientSocket));
   t.start();
   System.out.println("got a connection"+clientOutputStream.toString());
  }
 }catch (Exception e) {
  e.printStackTrace();
 }
 
}


public void tellEverone(String message) {
 Iterator it=clientOutputStream.iterator();
 while(it.hasNext()) {
  try {
    writer=(PrintWriter)it.next();
   System.out.println(message);
   writer.println(message);
  
   writer.flush();
  }catch(Exception e) {
   e.printStackTrace();
  }
 }
 
}



}


No comments:

Post a Comment

Socket Programming in Java

Source Code: client Side: package learning; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event....