⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientpart.java

📁 学生管理系统
💻 JAVA
字号:
/**
 *@ClientPart.java
 *@author weiWang
 *@date 2006-3-27
 */
 package com.herb.teacherclient; 
 //客户端
 import java.net.*;
 import java.io.*;
 import java.util.*;
 
 public class ClientPart implements Runnable{
    private static final int size=8;
    Socket socket;
    InetAddress address;
    static BufferedReader in;
    static PrintWriter out;
    String[] infor=new String[size];
    private static String content;
    private static String CODE;
    static int row;         //修改的行数
    static String markset;  //修改的分数
    static String who,msg;  //聊天的对象和内容
    String ip=null;
    int port=8000;

    public ClientPart(String CODE){
        this.CODE=CODE; 
    }

   
    public ClientPart(String CODE,String msg,String who){
        this.CODE=CODE;
        this.msg=msg;
        this.who=who;
    }         
    
    public ClientPart(String CODE,String m,int r,String da){
        this.CODE=CODE;          //修改分数的代号
        content=m;             //修改的学生所在系
        row=r;
        markset=da;        
    }
 
    public ClientPart(String CODE,String content){
        this.CODE=CODE;
        this.content=content; 
    }
 
    public void run(){
      setIpAndPort();
      try{
         runClient();
      }catch(Exception e){
         System.out.println(e.getMessage());
      }
    }
     
   /**
    *客户端程序运行,与服务器进行连接
    */
   public void runClient()throws Exception{
    try{
      try{ 
        address=InetAddress.getByName(ip);
        socket=new Socket(address,port);
        in=new BufferedReader(
           new InputStreamReader(
             socket.getInputStream()));
        out=new PrintWriter(
           new BufferedWriter(
             new OutputStreamWriter(
               socket.getOutputStream())),true);
        out.println(CODE);               //传送代号建立连接,这里代号为CODE1(001)
        System.out.println("ClentPart code=="+CODE);
        getMsg(in.readLine());           //接收信息
     
      }catch(IOException ie){
         System.out.println("连接服务器出错"+ie.getMessage());
         return;
      }
     }finally{ 
       if(!(socket.isClosed()))
         System.out.println("client close..."); 
          socket.close();
     }
   }
   
   //获得ip,port
   public void setIpAndPort(){
       Vector v=new Vector();
       v=IpsetEvent.getInfor();
       ip=v.elementAt(0).toString();
       port=Integer.parseInt(v.elementAt(1).toString());
   }

   public static void getMsg(String code){  
     String s;
     Vector majorVector=new Vector(); 
     Object[][] data;
     Vector dataVector=new Vector();   //用来暂存学生的数据
    try{  
      if(code.equals("001")){          //服务器返回的确认信息      
         while(!(((s=in.readLine()).equals("over"))))
            majorVector.add(s);
         TeacherMainFrame.updateList(majorVector);
      }
      if(code.equals("002")){
         out.println(content);      //确定服务器所要传送的那个系的学生信息
         out.flush();
         while(!(((s=in.readLine()).equals("over"))))
            dataVector.add(s);
         data=new Object[(dataVector.size())/size][size];
         int z=0;
         for(int i=0;i<data.length;i++)
            for(int j=0;j<size;j++)
                data[i][j]=dataVector.elementAt(z++);
         TeacherMainFrame.updateTable(data);
      }                 
      if(code.equals("003")){
         out.println(content);
         out.flush();     
         out.println(row);
         out.flush();
         out.println(markset);
         out.flush(); 
      } 
      if(code.equals("004")){   
         out.println(msg);
         out.flush();
         out.println(who);
         out.flush();
      }
      if(code.equals("005")){ 
         String msg;
         Vector v=new Vector();
         while(!((msg=in.readLine()).equals("over")))
            v.add(msg);
         ChatFrame.showChatMsg(v);
      }   
      if(code.equals("006")){ 
         out.println(content);
         out.flush();
         Vector v=new Vector();
         while(!((s=in.readLine()).equals("over"))){
            ChatFrame.updateList(s);
            v.add(s);
         }
         System.out.println("接受到的客人昵称:"+v.toString());   
       }
      if(code.equals("007")){
         Vector v=new Vector();
          while(!((s=in.readLine()).equals("over")))
             v.add(s);
          ChatLogFrame.initBox(v);
        //System.out.println("已经接受到了系名:"+v.toString());
      }   
    }catch(IOException e){ 
        System.out.println(e.getMessage());
     }   
   }               
 }                    






 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -