(Java)Client – Server _ Concurrenct Server

What is a concurrent server?

A server that can handle multiple client requests at the same time, makes multiple copies of itself.

Server


import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import project.Client.View_Client1;
import project.Model.Model_server;
import project.Server.View_Server;

/**
 *
 * @author root
 */
public class ControlServer {
   View_Server server;
   Model_server model;
   
   public void setModelserver(Model_server model){
       this.model = model;
   }
   
    public void startServer(View_Server Server) {
        
     int port = Integer.parseInt(Server.getPort().getText());
     try{
           ServerSocket ss = new ServerSocket(port);
            while (true) {
              try {
		Socket socket = ss.accept();
                System.out.println("Server connection");
                ServerChild child = new ServerChild(socket);
                child.start();
              } catch (Exception ex) {
               	   System.out.println("client disconnected");
                }
            }
         } catch (Exception ex){
            ex.printStackTrace();
           }
     }
 class ServerChild extends Thread{
        Socket soc;
        ServerChild (Socket p_soc){
            soc = p_soc;
        }
        public void run(){
            try{
               
                DataInputStream dis = new DataInputStream(soc.getInputStream());
                while (true) {
                    String text = dis.readUTF();
                    System.out.println("Pesan = "+ text);
                }
             } catch (Exception ex){
                 System.out.println("Connection closed");
               }
        }
    }
    
}
<pre> 

Client

import java.io.DataOutputStream;
import java.net.Socket;
import project.Client.VIew_Client2;
import project.Model.Model_user2;

/**
 *
 * @author root
 */
public class ControlerUser2 {
    private Model_user2 model;
    private VIew_Client2 view;
    
    public void setModeluser1(Model_user2 model){
        this.model = model;
    }
     public void startClient(VIew_Client2 view) {
        String host = view.getHost().getText();
        int port =Integer.parseInt(view.getPosrt().getText());
        try {
            Socket socket = new Socket(host,port); 
                
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
       
                dos.writeUTF("User 1 = "+view.getPesan().getText());
                Thread.sleep(1000);
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

<pre> 

OUTPUR

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s