高手,您好:
    我在写一个“CS结构的聊天工具”,我设计成了:
    CSD的结构:
    C端:
    用户端.
    S端:
    运营商端.
    D端:
    开发者端.
    我的C端的登陆,经过了:
    1.登录窗口类“Login”.连接S端进行“查库中有无本账户”.
    2.进入“NoteUserLoginJFrame类”,“提示用户等待”,“接收S端的返回值”.
    3.“NoteUserLoginJFrame类”根据接收到的S端的返回值,进行:是否进入RelayNo1类或者提示用户检查密码账户是否输入准确.
    4.进入RelayNo1类之后,向D端发出连接数据,D端进行“授权期限是否到达”的检查,和是否用户在“授权缓冲器内”的判断,如果在,让用户端进行“相应的提示”.并且C端连接D端的服务器中的程序.
    如果不在,在用户端也进行相应的提示.
    我的“1.”步骤中的“Login类”的源代码如下:
    package c_port_packageTeacher;import java.awt.Rectangle;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;import c_port_packageTeacher.Login;
import c_port_packageTeacher.MyJButton;public class Login extends JFrame{ /**
 * 
 */
    public static String D_PORT_SERVER = "127.0.0.1";
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
JTextField jtf0 = new JTextField();
JTextField jtf1 = new JTextField();
JLabel jl0 = new JLabel();
JLabel jl1 = new JLabel();
MyJButton jb2 = new MyJButton("注册","127.0.0.1");
MyJButtonPayForSysProduct jb3 = new MyJButtonPayForSysProduct("买授权");
JPasswordField jpwf = new JPasswordField();       
JPanel jp1 = new JPanel();
public static String UID = null;
static String PWD = null;
static String D_port_Server = "127.0.0.1";
static String S_port_Server = "127.0.0.1";
Socket so2 = null;
Socket so3 = null;

public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}

public Login(){
this.setTitle("赤兔马网络答疑学校,让您的孩子,从杜月笙成为段永平");
try {
so2 = new Socket(D_port_Server,9999);
so3 = new Socket(S_port_Server,8888);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyJButtonLogin jb1 = new MyJButtonLogin("登陆",jtf0.getText(),jtf1.getText(),this,so2,so3);
this.setSize(500,600);
this.setVisible(true);
this.setDefaultCloseOperation(Login.EXIT_ON_CLOSE);
jb1.setBounds(new Rectangle(30,220,80,30));
jb2.setBounds(new Rectangle(236,220,80,30));
jb3.setBounds(new Rectangle(128,220,90,30));
jp1.setLayout(null);
jp1.add(jb1);
jp1.add(jb2);
jtf0.setBounds(new Rectangle(73,115,220,25));
jp1.add(jtf0);
jpwf.setBounds(new Rectangle(73,150,220,25));
jp1.add(jpwf) ;
jl0 = new JLabel("用户名");
jl1 = new JLabel("密码");
jl0.setBounds(new Rectangle(30,115,40,25));
jl1.setBounds(new Rectangle(30,150,40,25));
jp1.add(jl0);
jp1.add(jl1);
jp1.add(jb3);
this.add(jp1);
this.setBounds(283,115,370,330);
this.setContentPane(jp1);
UID = jtf0.getText();
PWD = jtf1.getText();
}
}
    其中的按钮“jb1登录按钮”,他的源代码如下:
    package c_port_packageDoctor;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JButton;import c_port_packageDoctor.Login;public class MyJButtonLogin extends JButton {
static NoteUserLoginFrame cjco;
boolean isMyUser = false;
DataOutputStream dops3 = null;
DataOutputStream dops2 = null;
DataInputStream dips3 = null;
DataInputStream dips2 = null;
String MySysNum = null;
public static String MyUID = null;
public static String MyPWD = null;
String str2 = null;
public static String str3 = null;
String str4 = null;
Login log = null;
Socket so2 = null;
Socket so3 = null;
/**
 * 
 */
private static final long serialVersionUID = 1L; /**
 * @param args
 */
public MyJButtonLogin(String button, String UID, String pwd, Login log_in,Socket s2,Socket s3) {
super(button);
MyUID = UID;
MyPWD = pwd;
InetAddress addr = null;
so2 = s2;
so3 = s3;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
str3=addr.getHostAddress().toString();//获得本机IP
log = log_in;
System.out.println("" + log.toString());
this.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent et) { try {
dops3 = new DataOutputStream(so3.getOutputStream());
dips3 = new DataInputStream(so3.getInputStream());
dops2 = new DataOutputStream(so2.getOutputStream());
dips2 = new DataInputStream(so2.getInputStream());
System.out.println("so3++++++++"+so3);
dops3.writeUTF("0#"+MyUID + "%" + MyPWD+"%"+"Doctor"+"%"+str3+"#"+"C"+"#"+"Doctor"+"#"+"1800000000");
dops3.flush();
dops3.close(); 
cjco = new NoteUserLoginFrame(MyUID,MyPWD,so2,so3);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
    在这里,用户输入了用户名和密码后,点击“上文的登陆按钮”之后,所要连接的S端的Socket请求接收类文件的部分源代码如下:
    (高手,由于篇幅的原因,我将我的接下来的“ChatServer类”,写到了下面的1#楼的留言位置处了.
    希望得到高手的指导...)
    (【本提问尚未结束,希望高手能够观看下面将要继续交代的问题场景...)im聊天聊天工具源代码Java调试错误Java通信im

解决方案 »

  1.   

    我的S端的ChatServer类的代码如下:
        package s_port_package_BeiJing;import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Arrays;import javax.swing.JFrame;public class ChatServer extends JFrame {
    /**
     * ChatServer.java
     * 2013-5-9
     * @author cjco
     */
        //全局定义区域:定义各种成员变量;
    static String D_PORT_SERVER = "127.0.0.1";
    static String S_PORT_SERVER = "127.0.0.1";
    static String C_PORT_CLIENT = "127.0.0.1";
    static String[] bufMemUID = new String[200];
    static String[] bufMemPWD = new String[200];
    static String[] bufCusUID = new String[200];
    static String[] bufCusPWD = new String[200];
    String MyMemIDPath = "D:"+File.separator+"MyDoc"+File.separator+"Sport"+File.separator+"registeredMemID.txt";
    String MyMemPWDPath = "D:"+File.separator+"MyDoc"+File.separator+"Sport"+File.separator+"registeredMemPWD.txt";
    String MyCustomerIDPath = "D:"+File.separator+"MyDoc"+File.separator+"Sport"+File.separator+"CustomerID.txt";
    String MyCustomerPWDPath = "D:"+File.separator+"MyDoc"+File.separator+"Sport"+File.separator+"CustomerPWD.txt";
    String MyRole = null;
    String MySysNum = null;
    ServerSocket ss1 = null;
    String buffertemp = null;
    String MyInfo = null;
    String[] buffer = null;
    String MySysRole = null; 
    public static int Index = 0;
    static int num = 0;
    int[] a = null;
    int para = -1;
    int condition = -1;
    private static final long serialVersionUID = 1L;
    boolean started = false;
    static Socket so1;
    static Socket so4;
     File f = null;
    static int PM_linknumlog = 0;
    static DataOutputStream dops = null;
    public static int[] LocalTeacheronline = new int[200];
    public int[] LocalTeacheronlineCondition = new int[200];
    public int[] Teachercomplicatingnum = new int[200];

    public static void main(String[] args) {
    try {
    so4 = new Socket(D_PORT_SERVER,9999);
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    new ChatServer().start();
    }            //“C端用户的其他Socket请求的"交给线程处理"动作。
         public void start(){
         try {
    dops = new DataOutputStream(so4.getOutputStream());
         dops.writeUTF("0#0#YinChuanXinErZhong#0#Start#S#BeiJing");
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
         ReadTxt.read(MyMemIDPath);
         int numID = ReadTxt.logData.size();
     
         for(int i = 0;i<numID;i++){
         bufMemUID[i]=ReadTxt.logData.get(i);
         }
         ReadTxt.logData.clear();
         ReadTxt.read(MyMemPWDPath);
         int numPWD = ReadTxt.logData.size();
         for(int i = 0;i<numPWD;i++){
         bufMemPWD[i]=ReadTxt.logData.get(i);
         }
         ReadTxt.logData.clear();
         ReadTxt.read(MyCustomerIDPath);
         numID = ReadTxt.logData.size();
         System.out.println(numID);
         for(int i=0;i<numID;i++){
         bufCusUID[i] = ReadTxt.logData.get(i);
         }
         ReadTxt.logData.clear();
         ReadTxt.read(MyCustomerPWDPath);
             numPWD = ReadTxt.logData.size();
             for(int i =0;i<numPWD;i++){
              bufCusPWD[i]=ReadTxt.logData.get(i);
             }
             ReadTxt.logData.clear();
         try {
         Arrays.fill(LocalTeacheronline, 0);
         // TODO Auto-generated method stub 
         ss1 = new ServerSocket(8888,50); 
             } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
         while(true){
                  //创建绑定到端口8888的ServerSocket对象
    try {
    so1 = ss1.accept();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(so1+"|||---");
                  System.out.println("服务器正在对8888端口进行监听");
                     //服务器循环接受客户端的请求,为不同的客户端提供服务
             InputStream in; 
    try {
    in = so1.getInputStream();
             BufferedReader buffReder = new BufferedReader(new InputStreamReader(in));
             buffertemp = buffReder.readLine();
             buffer = buffertemp.split("[#]");
             System.out.println("S:"+buffer);
             condition = Integer.parseInt(buffer[0]);
             System.out.println("S:"+condition);
             MyInfo = buffer[1];
             System.out.println(MyInfo);
             MySysRole = buffer[2]; 
             MyRole = buffer[3];
             MySysNum = buffer[4];
             buffReder.close();
             in.close();
             if(MySysRole.equals("D")){
                  ManageTheChannel3RecvUserAccountLoopSendClientNum mb = new ManageTheChannel3RecvUserAccountLoopSendClientNum("Thread_3",MyInfo,this);
                  mb.start(); 
             }else{
             if(Integer.parseInt(MySysNum)==1800000000){
             if(MyRole.equals("Teacher")){
             LocalTeacheronline[Integer.parseInt(MySysNum)]=1;
             LocalTeacheronlineCondition[Integer.parseInt(MySysNum)]=1;
             }
             }
             switch(condition){
                  case 0:
                  {//已对照完毕.
                  ManageTheChannel0ConfirmandSaveDS obj0 = new ManageTheChannel0ConfirmandSaveDS(so1 ,this,MyInfo);
                  Thread m0 = new Thread(obj0,"MyThread1");
                  m0.start();
                  break;
                  }
                  case 1:
                  {
                  ManageTheChannel1SendInfo m1 = new ManageTheChannel1SendInfo(so1,"Thread2",this,MyInfo);
                  m1.start();
                  break;
                  }
                  case 2:
                  {//已对照完毕.
                  ManageTheChannel2ExitSysGivOrderNum m2 = new ManageTheChannel2ExitSysGivOrderNum(so1,"Thread3",MyInfo,this);
                  m2.start();
                  break;                                                                                                           
                  }
                  case 3:
                  {
                  }
                  case 4:
                  { 
                  ManageTheServiceValueForEnterprise m4 = new ManageTheServiceValueForEnterprise(MyInfo,so1);
                  m4.start();
                  }
                  case 6:
                  {
                  GetOurServiceWorkValue m6 = new GetOurServiceWorkValue(MyInfo,so1);
                  m6.start();
                  }
                  }
             }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
                 
         }
         }
    }我将本类文件中的第153行的
      

  2.   

    ManageTheChannel0ConfirmandSaveDS类的源代码写到了下面:
        我的在“S端的第153行”的ManageTheChannel0ConfirmandSaveDS类文件的源代码如下:
        package s_port_package_BeiJing;import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.Socket; public class ManageTheChannel0ConfirmandSaveDS implements Runnable{
    ChatServer Localdemo = null;
    int condition = -1;
    Integer MyIndex = 0;
    Integer MyIndexCustomer = 0;
    static String str1,str2,str3;
    int Myonline = 1;
    static Socket so1 = null;
    int j = 0;
    boolean ismember = false;
    boolean iscustomer = false;
    byte[] b1 = new byte[1024];
    byte[] b2 = new byte[1024];
    int lenone = 0;
    int lentwo=0;
    FileInputStream fis0 = null;
    FileInputStream fis1 = null;
    BufferedReader dips0 = null;
    DataOutputStream dops0 = null;
    String LocalBufffer,MyRole,UserIP,MyUID,MyPWD = null;
    String[] bufMemUID = null;
    String[] bufMemPWD = null;
    String[] bufCusUID = null;
    String[] bufCusPWD = null;
    String[] buffers2 = null;
    String MySocketData = null;

    public  ManageTheChannel0ConfirmandSaveDS(Socket s1,ChatServer demo,String SocketData) {
    System.out.println("S_port_0_thread");
    Localdemo = demo;
    so1 = s1;
    MySocketData = SocketData;
    } public void run(){
    try {
    buffers2 = MySocketData.split("[%]");
    MyUID = buffers2[0];
    MyPWD = buffers2[1];
    MyRole = buffers2[2];
    UserIP = buffers2[3];
    if(MyRole.equals("doctor")){
    ismember = confirmDoctor();
    if(ismember){
    System.out.println("S_port_0_thread_53");
    int DoctorIndex = GetUserIndex.returnDoctorIndex(MyUID);
    MyIndex = new Integer(DoctorIndex);
    dops0.writeUTF("true"+"#"+String.valueOf(MyIndex));
    dops0.flush();
    ChatServer.LocalDoctoronline[MyIndex]=1; 
    S_port_ChatClientDoctor_DS D1 = new S_port_ChatClientDoctor_DS(MyIndex,buffers2[0],"doctor",buffers2[2],so1,1);
    ManageTheChannel1SendInfo.mapDoctoronline.put(GetUserIndex.returnDoctorIndex(MyUID),D1);
    closeResource();
    }else{
    dops0.writeUTF("false");
    closeResource();
    }
    }else{
    iscustomer = confirmCustomer();
    if(iscustomer){
    System.out.println("S_port_0_thread_69");
    int CustomerIndex = GetUserIndex.returnCustomerIndex(MyUID);
    MyIndex = new Integer(CustomerIndex);
    dops0.writeUTF("true"+String.valueOf(MyIndex));
    dops0.flush();
    closeResource();
    S_port_ChatClientCustomer_DS D1 = new S_port_ChatClientCustomer_DS(MyIndex,buffers2[0],"Customer",buffers2[2],so1,1);
    ManageTheChannel1SendInfo.mapCustomeronline.put(GetUserIndex.returnCustomerIndex(MyUID),D1);
    }else{
    System.out.println("S_port_0_thread_78");
    dops0.writeUTF("false");
    closeResource();
    }
    }} catch (IOException e) {

    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } public boolean confirmDoctor(){
    int i = 0;
    while (i < bufMemUID.length) {
    if ((MyUID == ChatServer.bufMemUID[i]) && (MyPWD == ChatServer.bufMemPWD[i])) {
    return true;
    }
     i++;
    }
    return false;
    } public void closeResource(){ try {
    if(fis0!=null){
    fis0.close();
    }
    if(fis1!=null){
    fis1.close();
    }
    if(dips0!=null){
    dips0.close();
    }
    if(dops0!=null){
    dops0.close();
    }
    if(so1!=null){
    so1.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }

    public boolean confirmCustomer(){
    int i = 0;
    while (i < bufCusUID.length) {
    if ((MyUID == ChatServer.bufCusUID[i]) && (MyPWD == ChatServer.bufCusPWD[i])) {
    return true;
    }
    i++;
    }
    return false;
    }}
        在我的C端的“登陆窗口类按钮MyJButtonLogin类”之中的jb1登录按钮的构造方法之内的“NoteUserLoginJFrame类的源代码”,我写到了下面:
        package c_port_packageTeacher;import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.Socket;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class NoteUserLoginFrame extends JFrame implements WindowListener{
    /**
     * 
     */

    private static final long serialVersionUID = 1L;
    JPanel jP1 = new JPanel();
    static String MyworkNum = null;
    Font fnt = new Font("宋体",0,20);
    static JLabel jL0 = new JLabel();
    JLabel jL1 = new JLabel();
    DataInputStream dips = null;
    String SocketData = null;
    String[] buffer = null;
    String isOurUser = null;
    boolean isUser = false;

    public NoteUserLoginFrame(String UID,String PWD,Socket s2,Socket s3){
    this.setTitle("系统信息提示:");
    jP1.setLayout(new BorderLayout());
    jL0.setText("客户端正在登陆,请尊敬的用户稍等十几秒钟,我们正在与我们的服务器进行数据通信!!请您耐心等待!!");
    jL0.setFont(fnt);
    jP1.add(jL0,BorderLayout.CENTER);
    this.add(jP1);
    setBounds(200,300,1000,100);
    setVisible(true);
    Socket so2 = s2;
    Socket so3 = s3;
    try {
    dips = new DataInputStream(so3.getInputStream());
    SocketData = dips.readUTF();
    buffer = SocketData.split("[#]");
    isOurUser = buffer[0];
    System.out.println(isOurUser);
    MyworkNum = buffer[1];
    System.out.println(MyworkNum);
    isUser = Boolean.parseBoolean(isOurUser);
    System.out.println(isUser);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if(isUser){
    new RelayNo1(UID,PWD,isUser,so2,so3,MyworkNum);
    }
    }
    public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub
    System.exit(0);
    }
    public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    System.exit(0);
    }
    public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

    }

    }
        这里从逻辑上来说,我检查了好几遍,如果用户输入的账户为合法的志华应该是能够“在登入了S端的服务器经查确有此账户之后,应该能够得到登入D端的开发效果的吧...?
        为什么在我运行了D端的程序,S端的程序和C端的程序之后,并且,我采用了备份在本地的文件中的标准账户进行了用户名,密码的书写,得到了下面的“点击登录按钮”这个动作发生后的运行错误提示为下图中的场景:
        
        请问高手,这里我的错误,是什么原因?
        应该怎样修改...?
        谢谢高手的点拨!!
        一百分奉上!!
                                                             一位日日夜夜向着理想奔跑的筑梦者
                                                            2013年10月8日星期二早晨10点56分   
        
      

  3.   

    错误已经给出了,so3 已经关闭了,请检查下,构造函数传入的s3
      

  4.   

    学会如何debug之后可轻松的解决大部分问题