高手,您好:
    我在用Java做一个im系统,我的C端的登陆面板的代码为:
    package c_port_packageBJTeacher;import java.awt.Rectangle;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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;
public class Login extends JFrame{ /**
 * 
 */
static String S_PORT_SERVER = "127.0.0.1";
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
    static JTextField jtf0 = new JTextField();
static JTextField jtf1 = new JTextField();
JLabel jl0 = new JLabel();
JLabel jl1 = new JLabel();
MyJButton jb2 = null;
MyJButtonPayForSysProduct jb3 = null;
static JPasswordField jpwf = new JPasswordField();       
JPanel jp1 = new JPanel();
public static String UID = null;
static String PWD = null;
Socket so2 = null;
Socket so3 = null;
DataOutputStream dops2 = null;
DataOutputStream dops3 = null;
DataInputStream dips2 = null;
DataInputStream dips3 = null;

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

public Login(){
this.setTitle("赤兔马网络学习平台,让您采用电子商务的价格,享受到当面手把手授课的学习效果");
try {
so3 = new Socket(S_PORT_SERVER,8888);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jb2 = new MyJButton("注册","127.0.0.1",so3);
try {
dops3 = new DataOutputStream(so3.getOutputStream());
dips3 = new DataInputStream(so3.getInputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyJButtonLogin jb1 = new MyJButtonLogin("登陆","褚彦丽","cjcjcjcjcocococo!!",this,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));
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);
this.add(jp1);
this.setBounds(500,218,370,330);
this.setContentPane(jp1);
}
}
    其中的第71行的MyJButtonLogin类的源代码如下:
    package c_port_packageBJTeacher;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JButton;public class MyJButtonLogin extends JButton {
String S_PORT_SERVER = "127.0.0.1";
static NoteUserLoginFrame cjco;
boolean isMyUser = false;
DataOutputStream Localdops3 = null;
DataOutputStream Localdops2 = null;
DataInputStream Localdips3 = null;
DataInputStream Localdips2 = 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;
String[] buffer =null;
/**
 * 
 */
private static final long serialVersionUID = 1L; /**
 * @param args
 */ public MyJButtonLogin(String button, String UID, String pwd, Login login,Socket s3) {
super(button);
so3 = s3;
MyUID = UID;
MyPWD = pwd;
try {
Localdops3 = new DataOutputStream(so3.getOutputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
str3 = addr.getHostAddress().toString();// 获得本机IP
log = login;
System.out.println("" + log.toString());
this.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent et) {
log.setVisible(false);
MyUID = Login.jtf0.getText();
MyPWD = new String(Login.jpwf.getPassword());
try {
String aa = "0#" + MyUID + "%" + MyPWD + "%"
+ "Teacher" + "%" + str3 + "#" + "C" + "#"
+ "Teacher" + "#" + "0";
System.out.println(aa);
Localdops3.writeUTF("0#" + MyUID + "%" + MyPWD + "%"
+ "Teacher" + "%" + str3 + "#" + "C" + "#"
+ "Teacher" + "#" + "0");
Localdops3.flush();
String str = Lib.readsocketUTF(so3);
Localdops3.close();
so3.close(); 
System.out.println("888888888888888888888888888888888888888888888888");
System.out.println(str);
buffer = str.split("[#]");
isMyUser = Boolean.parseBoolean(buffer[0]);
MySysNum = buffer[1];
try {
if(isMyUser){
System.out.println(str);

System.out.println("MyJButtonLogin_start");
cjco = new NoteUserLoginFrame(MyUID,MyPWD,so2,so3);
}
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
    由于篇幅的原因,我将我的S端的源代码写到了下一个帖子中,希望高手能够过目..
Java 调试Java 异常Java SocketJava 流Java im

解决方案 »

  1.   

    我的S端的源代码如下:
        package S_port_packageBJTeacher;import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Arrays;
    import java.util.concurrent.ArrayBlockingQueue;import javax.swing.JFrame;public class ChatServer extends JFrame {
    /**
     * ChatServer.java 2013-5-9
     * 
     * @author cjco
     */
    // 全局定义区域:定义各种成员变量;
    int SysPreTeacherNum = 2000;
    int SysPreCustomerNum = 2000;
    static Integer MyMEMNum = 202;
    static Integer MyCUSNum = 202;
    static String S_PORT_SERVER = "127.0.0.1";
    static String C_PORT_CLIENT = "127.0.0.1";
    static String[] bufMemUID = new String[MyMEMNum];
    static String[] bufMemPWD = new String[MyMEMNum];
    static String[] bufCusUID = new String[MyCUSNum];
    static String[] bufCusPWD = new String[MyCUSNum];
    static MyArrayList MyBJMemUserAuthorizeTime = new MyArrayList();
    static MyArrayList MyBJCusUserAuthorizeTime = new MyArrayList();
    static MyArrayList MyYCMemUserAuthorizeTime = new MyArrayList();
    static MyArrayList MyYCCusUserAuthorizeTime = new MyArrayList();
    String MyMemIDPath = "D:" + File.separator + "MyDoc" + File.separator
    + "Sport" + File.separator + "registeredMemUID.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 so2;
    File f = null;
    static int PM_linknumlog = 0;
    DataOutputStream dops2 = null;
    DataOutputStream dips2 = null;
    public static int[] LocalTeacheronline = new int[MyMEMNum];
    public int[] LocalTeacheronlineCondition = new int[MyMEMNum];
    public int[] Teachercomplicatingnum = new int[MyMEMNum];
    ArrayBlockingQueue<String> sportmessagelist = new ArrayBlockingQueue<String>(
    100000, true);
    public static MyMemHashMap mapTeacheronline = new MyMemHashMap();
    public static MyCusHashMap mapCustomeronline = new MyCusHashMap();
    public static ArrayBlockingQueue<String> GoalTeacherchatwordlist = null;
    public static ArrayBlockingQueue<String> GoalCustomerchatwordlist = null;
    public static ArrayBlockingQueue<DataOutputStream> GoalTeacherSendInfo = null;
    public static ArrayBlockingQueue<DataOutputStream> GoalCustomerSendInfo = null; public static void main(String[] args) {
    new ChatServer().start();
    } // “C端用户的其他Socket请求的"交给线程处理"动作。
    public void start() {
    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 MemnumPWD = ReadTxt.logData.size();
    for (int i = 0; i < MemnumPWD; 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);
    int CusnumPWD = ReadTxt.logData.size();
    for (int i = 0; i < CusnumPWD; i++) {
    bufCusPWD[i] = ReadTxt.logData.get(i);
    }
    GoalTeacherchatwordlist = new ArrayBlockingQueue<String>(100000, true);
    GoalCustomerchatwordlist = new ArrayBlockingQueue<String>(100000, true);
    GoalTeacherSendInfo = new ArrayBlockingQueue<DataOutputStream>(
    SysPreTeacherNum, true);
    GoalCustomerSendInfo = new ArrayBlockingQueue<DataOutputStream>(
    SysPreCustomerNum, true); 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端口进行监听");
    // 服务器循环接受客户端的请求,为不同的客户端提供服务
    System.out.println("进入try");
    System.out.println("进入try1");
    System.out.println("进入try2");
      buffertemp = Lib.readsocketUTF(so1);
    System.out.print(buffertemp);
    System.out.println("进入try3");
    System.out.println("C_port_connect" + "I'm_Teacher_From_BeiJing");
    System.out.println(buffertemp);
    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);
    System.out.println("MyInfo:" + MyInfo);
    MySysRole = buffer[2];
    MyRole = buffer[3];
    MySysNum = buffer[4];
    if (MySysRole.equals("D")) {
    ManageTheChannel3RecvUserAccountLoopSendClientNum mb = new ManageTheChannel3RecvUserAccountLoopSendClientNum(
    "Thread_3", MyInfo, this, so1);
    mb.start();
    } else {
    if (Integer.parseInt(MySysNum) == 200) {
    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;
    }
    }
    } }
    }
    }    上述第135行的Lib.readsocketUTF(so1);的代码,是我为了避免Socket编程中的“EOFException”异常而进行的“readUTF();方法”的重写..
        他的源代码如下:
        package S_port_packageBJTeacher;import java.io.DataInputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.Socket;public class Lib {
    static String readsocketUTF(Socket s){
            DataInputStream read = null;
            try {
                read = new DataInputStream(s.getInputStream());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String msg = "";
            String info = "";
            msg  = info;   
            while(true){ 
            try {
                info  = read.readUTF();
            }catch(EOFException e){
            break; 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            } 
            msg =  msg + info; 
            try {
    read.close();
            s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

            return msg;
        }
    }    上述S端的“用户登陆账户”的备份中,有一个“UID:褚彦丽”,“PWD:cjcjcjcjcocococo!!”的账户。
        按照逻辑来说,在进行了S端的运行后,C端连接到S端后,应该会有S端第135行输出:
        ChatServer类的第135行的代码的“C端MyJButtonLogin类中第74行语句进行的”:
        Localdops3.writeUTF("0#" + MyUID + "%" + MyPWD + "%"
                                + "Teacher" + "%" + str3 + "#" + "C" + "#"
                                + "Teacher" + "#" + "0");
        中的数据:
        “0#褚彦丽%cjcjcjcjcocococo!!%Teacher%192.168.1.3#C#Teacher#0”
        的打印输出的内容的.
        但是,经过运行测试后,出现的结果为:
        
        请问高手:
        这是怎么回事...?
        一百分奉上!!
                                                                   一位求知者
                                                                   2013年11月3日早晨6点半
      

  2.   

    我这里还是看到的readUTF()while(true){ 
            try {
                info  = read.readUTF();
            }catch(EOFException e){
            break; 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            } 就不能改成2进制嘛?
      

  3.   

    withiter版主,谢谢您再次的点拨!!
        如果改成二进制,是可以的..
        但是弟我很希望能够知道:
        弟我的上面的代码,错在什么地方..?
        版主,这是弟我的“测试类”中的实验代码:
        (S端主类..)
        package s;import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;public class S_port {
    String str = "";
    ServerSocket s =null;
    Socket so = null;
    S_port(){
    try {
    s = new ServerSocket(60000);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    while(true){
    try {
    so = s.accept();
    break;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    SystemThread m1 = new SystemThread(so);
    m1.start();
    try {
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void main(String[] args){
    new S_port();
    }
    }    (S端的子线程类..)
        package s;import java.io.DataInputStream;
    import java.net.Socket;public class SystemThread extends Thread{
    Socket so = null;
    DataInputStream dips = null;

    public SystemThread(Socket s){
    so = s;
    }

    public void run(){
    String str = Lib.readsocketUTF(so);
    System.out.println(str);
    }
    }    (S端的Lib类..)
        package S_port_packageBJTeacher;import java.io.DataInputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.Socket;public class Lib {
    static String readsocketUTF(Socket s){
            DataInputStream read = null;
            try {
             Object ob = s.getInputStream();
                read = new DataInputStream(s.getInputStream());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String msg = "";
            String info = ""; 
            msg  = info;   
            while(true){ 
            try {
                info  = read.readUTF();
            }catch(EOFException e){
            break; 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            } 
            msg =  msg + info; 
            try {
    read.close();
            s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

            return msg;
        }
    }    我的C端的测试类的代码如下:
        package s;import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;public class C_port {
    Socket s = null;
    DataOutputStream dops = null;
    public C_port(){
    try {
    s = new Socket("127.0.0.1",60000);
    dops = new DataOutputStream(s.getOutputStream());
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    dops.writeUTF("CJ必胜!!CJCO!!");
    dops.close();
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void main(String[] args){
    new C_port();
    }

    }    最后打印出的结果为:
        (S端console窗口的数据为:)
        
        这个结果说明,我的这个Lib类的readsocketUTF()方法,是能够起到“代替readUTF()方法”的功能的..
        希望得到withiter版主和CSDN论坛中的高手的点拨:
        我的这个项目的登陆模块,他的出错原因在什么地方..
        谢谢CSDN!!我的大学!!