Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.MalformedURLException;
- import java.net.ProtocolException;
- import java.net.Proxy;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class FLED extends JFrame implements Runnable{
- /**
- *
- */
- private static final long serialVersionUID = -1318204057094959103L;
- private static ServerSocket sc;
- public static JLabel l1;
- public static int i=0;
- private static String ID="C:\\Android\\DATABASE.bad";
- private static String URLSend="http://www.sitehere.com/adder.php";
- public static ArrayList<Client_To> clients;
- public static void main(String[] args) {
- (new Thread(new FLED())).start();
- }
- public FLED(){
- super("SERVER");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- l1=new JLabel("NOTHING");
- l1.setBounds(0, 0, 200, 30);
- setSize(400, 400);
- setVisible(true);
- add(l1);
- clients=new ArrayList<>();
- try {
- sc=new ServerSocket(5050);
- } catch (IOException e1) {}
- //if(new Connect(URLSend,sc.getInetAddress()).getSuccess())
- // l1.setText("Error of send ip address");
- while(true){
- try {
- clients.add(new Client_To(sc.accept()));
- (new Thread(clients.get(i))).start();
- i++;
- } catch (IOException e) {
- }
- }
- }
- @Override
- public void run() {
- }
- public static int SearchID(String id){
- int l=-1;
- int b=clients.size();
- for(int i=0;i<b;i++){
- if(clients.get(i).getID().equals(id)){
- l=i;
- }
- }
- return l;
- }
- public static Double<String,String> Auth(String log,String pass){
- String database,user,user_i,user_log,user_pass;
- int i_log;
- try {
- database =new String(Files.readAllBytes(Paths.get(ID)));
- } catch (IOException e) {
- return new Double<>("FAIL OF AUTH:FILE NOT FOUND","ERROR");
- }
- try{
- i_log=database.indexOf(log);
- if(i_log!=-1){
- user=database.substring(i_log-1, database.length());
- user_i=user.substring(0,user.indexOf(";"));
- user_log=user_i.substring(user_i.indexOf("(")+1, user_i.indexOf(")"));
- user_pass=user_i.substring(user_i.indexOf("{")+1, user_i.indexOf("}"));
- if((pass.equals(user_pass))&&(log.equals(user_log))){
- String friends=user_i.substring(user_i.indexOf("FRIEND_LIST_ID:")+15, user_i.indexOf("&"));
- user_i.replaceAll("offline", "online");
- return new Double<>(user_i.substring(user_i.indexOf("[")+1, user_i.indexOf("]"))+":"+
- user_i.substring(user_i.indexOf("<")+1,user_i.indexOf(">")),friends);
- }else{
- return new Double<>("","ERROR");
- }
- }else
- return new Double<>("","ERROR");
- }catch(Exception e){
- return new Double<>("","ERROR");
- }
- }
- static Triple<String,Boolean,String> processFriend(String id){
- Scanner scn=null;
- String s=null;
- try {
- scn=new Scanner(new FileInputStream(ID));
- } catch (FileNotFoundException e) {}
- while(scn.hasNext()){
- s=scn.nextLine();
- if(s.indexOf(id)!=-1) break;
- }
- scn.close();
- String name=s.substring(s.indexOf("<")+1, s.indexOf(">"));
- boolean on_off_line=s.substring(s.indexOf("*,")+1, s.indexOf(",*")).equals("online")?true:false;
- return new Triple<>(name,on_off_line,"Friend");
- }
- }
- class Client_To implements Runnable{
- private String mes;
- private StringTokenizer st;
- private ObjectOutputStream out;
- private ObjectInputStream in;
- private Socket inc;
- private String ID="";
- private ArrayList<Triple<String,Boolean,String>> friends;
- private Double<String ,String> dbl;
- public Client_To(Socket s){
- inc=s;
- FLED.l1.setText(inc.getInetAddress()+"");
- }
- public String getID(){
- return ID;
- }
- public void setID(String s){
- ID=s;
- }
- @Override
- public void run() {
- try {
- out=new ObjectOutputStream(inc.getOutputStream());
- in=new ObjectInputStream(inc.getInputStream());
- } catch (IOException e) {
- }
- realise();
- }
- public void realise(){
- int IdForMes;
- String b;
- while(true){
- try{
- mes=(String) in.readObject();
- if(mes.indexOf("AUTH:")!=-1){
- dbl=FLED.Auth(mes.substring(mes.indexOf("<")+1,mes.indexOf(">"))
- ,mes.substring(mes.indexOf("[")+1, mes.indexOf("]")));
- b=dbl.first;
- if(!dbl.second.equals("ERROR")){
- setID(b.substring(0, b.indexOf(":")));
- FLED.l1.setText(FLED.l1.getText()+" || "+getID()+" "+dbl.second);
- out.writeObject("Welcome " +b.substring(b.indexOf(":")+1, b.length()));
- st=new StringTokenizer(dbl.second,",");
- while(st.hasMoreTokens()) {
- String sr;
- FLED.l1.setText(FLED.l1.getText()+" "+(sr=dbl.second));
- friends.add(FLED.processFriend(sr));
- }
- if(!friends.isEmpty()){
- String friend="FRIEND_LIST:";
- for(int i=0;i<friends.size();i++)
- friend+=friends.get(i).t+"&"+friends.get(i).v+"&"+friends.get(i).i+";";
- out.writeObject(friend);
- }
- }else out.writeObject(b);
- FLED.l1.setText(FLED.l1.getText()+b);
- //read inf about user and send to user
- }
- if(mes.indexOf("MES")!=-1){
- IdForMes=FLED.SearchID(mes.substring(mes.indexOf("_")+1,mes.indexOf(":")));
- FLED.l1.setText(IdForMes+"");
- FLED.clients.get(IdForMes==-1?1:IdForMes).out.writeObject(mes.substring(mes.indexOf(":")+1,mes.length()));
- }
- }catch(Exception e){
- for(int i=0;i<FLED.clients.size();i++){
- if(FLED.clients.get(i).ID==ID){
- FLED.clients.remove(i);
- break;
- }
- }
- try {
- inc.close();
- } catch (IOException e1) {/** error of disconnect user*/}
- }
- }
- }
- }
- class Connect{
- private URL uri;
- private HttpURLConnection uri1;
- private PrintWriter pw=null;
- private Scanner scn=null;
- private boolean met=true;
- public Connect(String url,InetAddress in){
- try {
- uri=new URL(url);
- } catch (MalformedURLException e) {
- }
- try {
- uri1=(HttpURLConnection)uri.openConnection(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("",8080)));//proxy here
- } catch (IOException e) {
- //print error
- }
- uri1.setDoInput(true);
- uri1.setDoOutput(true);
- try {
- uri1.setRequestMethod("GET");
- } catch (ProtocolException e1) {}
- try {
- uri1.connect();
- } catch (IOException e) {
- //print error
- }
- try {
- scn=new Scanner(uri1.getInputStream());
- } catch (IOException e) {}
- try {
- pw=new PrintWriter(uri1.getOutputStream());
- } catch (IOException e) {}
- pw.println(URLEncoder.encode("ip="+in.toString()));
- String ans="";
- while(scn.hasNext()) ans+=scn.nextLine();
- if(ans.contains("")) met=false; //check here error
- scn.close();
- pw.close();
- }
- boolean getSuccess(){
- return met;
- }
- }
- class Triple<T,V,I>{
- T t;
- V v;
- I i;
- public Triple(T one,V two,I three){
- t=one;
- v=two;
- i=three;
- }
- }
- class Double<T,I>{
- T first;
- I second;
- public Double(T f,I s){
- first=f;
- second=s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement