關於socket通訊的一個例子!

來源:互聯網
上載者:User

      最近私底下接了個活,忙暈了,都好久沒來這裡了,幾乎要遺忘了!看了http://www.acloudblog.blog.sohu.com/的blog才想起把最近所學到的寫在這裡,一個給自己做個記號,再個供大家參考一下。接的這個活是對ccproxy進行擴充,以方便使用者使用,和給管理員提供一個方便快捷的管理平台!可惜做到今天還是沒辦法往下做了

    因為ccproxy都是用ini檔案來儲存使用者資訊的,這就涉及到遠程通訊的問題了!開始打算用URL來做的,後來發現只能讀遠程,不能寫遠程(查了資料好象有寫方法,但是自己沒有實驗成功,另外如果用url,還必須每個伺服器都要建個網站),所以選擇用socket來做!剛畢業沒多久,學校裡很少接觸過通訊的內容,做起來真是沒有頭緒 

功能點:使用者登入  使用者切換代理  管理員後台管理(包括添加 查詢 刪除 修改)

整體思路是:在每個Proxy 伺服器上開個監聽程式,用來接受使用者的登入資訊,切換代理資訊,接到資訊後就開個線程來進行相應的處理

監聽代碼:

import java.net.*;
import java.io.*;

public class ChatServer extends Thread {
    private Socket fromClient=null;//主機與客戶機的通訊套節字:fromClient
    private static boolean listening=true;
    private ObjectInputStream ois=null;
    private ObjectOutputStream oos=null;

    public ChatServer(Socket client){
        fromClient=client;
    }

    public static void main(String[] args) {
        int port = 10000;  //建立通訊的主機的連接埠號碼
        ServerSocket socServer = null;
        int i=0;
        try{
            socServer = new ServerSocket(port);
            while(listening){
                Socket Client=socServer.accept();
                new ChatServer(Client).start();
            }
        } catch (IOException e) {
            System.err.println("Could not listen on port: "+port);
            System.exit(1);
        }
    }

    public void run(){
        try{
            ois=new ObjectInputStream(fromClient.getInputStream());
            oos=new ObjectOutputStream(fromClient.getOutputStream());
            //對用戶端傳過來的資料進行處理
            String mess=(String)ois.readObject();
            System.out.println("用戶端傳過來的資料是:"+mess);
            String []data=mess.split(",");
            CommonMethod oper=new CommonMethod();
            if(mess!=null&&data[0].equals("add")){   //添加使用者
                oper.IniFile("D://ftp-date//AccInfo.ini");
                UserInfo user=new UserInfo();
                AcceptParPro par=new AcceptParPro();
                user=par.process(data);

                oper.addUserInfo(user);
                oper.save2();
            }
            if(data[0].equals("del")){              //刪除使用者
                oper.IniFile("D://ftp-date//AccInfo.ini");
             oper.delSection(data[1]);
                oper.save2();
            }
            if(data[0].equals("login")){            //驗證登入("login"+","+loginIP+","+username+","+password)
                oper.IniFile("D://ftp-date//AccInfo.ini");
                String loginresult=oper.getUser(data[1],data[2],data[3]);
                oos.writeObject(loginresult);
            }
            ois.close();
            oos.close();
            fromClient.close();//關閉套節字前要關閉所有的流串連
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

主伺服器上的代碼: 

public class DelegateOper {
    String addpath=null;
    String delpath=null;
    UserInfo user=null;

    //登入驗證
    public String login(String loginIP,String username,String password){
      String loginmessage="login"+","+loginIP+","+username+","+password;
      String loginresult=null;
      int port=10000;
      Socket socket=null;
      try{
          socket=new Socket(loginIP,port);
          //將登入資訊發送到伺服器端
          ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
          oos.writeObject(loginmessage);
          //接受伺服器端的驗證結果
          ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
          loginresult=(String)ois.readObject();
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch(ClassNotFoundException e){
          e.printStackTrace();
      }
      return loginresult;
    }

    //通過socket把資料從用戶端寫到服務端(在每個代理上都開一個socket用來監聽資料)
    public void addUser(String serverip,UserInfo user){
      String message="用戶端向服務段寫資料:增加";
      String addmessage="add"+","+user.getSection()+","+user.getUserName()+","+user.getPassword()+","+user.getMACAddress()+","+user.getIPAddressLow()+","+
               user.getIPAddressHigh()+","+user.getServiceMask()+","+user.getMaxConn()+","+user.getBandWidth()+","+
               user.getWebFilter()+","+user.getTimeSchedule()+","+user.getEnableUserPassword()+","+
               user.getEnableIPAddress()+","+user.getEnableMACAddress()+","+user.getEnable()+","+user.getBelongsGroup()+","+
               user.getBelongsGroupName()+","+user.getIsGroup()+","+user.getAutoDisable()+","+user.getDisableDateTime();
      int port=10000;
      Socket socket=null;
      try{
          socket=new Socket(serverip,port);
          ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
          oos.writeObject(addmessage);
          oos.close();
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

  //添加使用者資訊到本機,不用Socket
    public void addUserLocal(UserInfo user){
        try{
            CommonMethod oper=new CommonMethod();
            oper.IniFile("D://ftp-date//CCProxy//AccInfo.ini");
            oper.addUserInfo(user);
            oper.save2();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //增加完資料後再刪除
    public void delUser(String loginIP,UserInfo user){
        String message="用戶端向伺服器寫資料:刪除";
        String delmessage="del"+","+user.getSection();
        int port=10000;
        Socket socket=null;
        try{
            socket=new Socket(loginIP,port);
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(delmessage);
            oos.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //刪除本機上的資料
    public void delUserLocal(UserInfo user){
        try{
            String section=user.getSection();
            CommonMethod oper=new CommonMethod();
            oper.IniFile("D://ftp-date//CCProxy//AccInfo.ini");
            oper.delSection(section);
            oper.save2();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.