日期:2014-05-20  浏览次数:20796 次

求高手点拨一个Socket通信的异常的修改方案.
高手,您好:
    我在写一个Socket通信程序,在我运行了我的代码后,报出了下面的异常报告:
    异常报告第一幅图片是:
    
    异常报告的第二幅图是:
    
    第一幅图和第二幅图中的“Lib类”他的源代码如下:
    
package c_port_packageBJDoctor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public class Lib {
    static InputStream is = null;
    static OutputStream outputstream = null;
    static String MyKey = "CJCO5882";
    static PrintStream ps;
    static BufferedReader br;
    static String buffer0;
    static OutputStream os = null;
    static byte[] readbuf = null;
    static byte[] writebuf = null;
    static BufferedInputStream bis = null;
    static BufferedOutputStream bos = null;
    static int num = 0;
    static String str = "";
 
    static String readsocketUTF(Socket s) {
        String info = "";
        try {
            is = s.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //使用字节来传输
            int r = -1;
            int index = 0;
            byte[] b = new byte[2];
            while((r = is.read()) != -1){
                b[index] = (byte)r;
                if(index==1){
                    //将字节数据转回字符
                    char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
                    index = 0;
                    str += c;
                }else{
                    index++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bResult = Crypttobyte.HloveyRC4(str, MyKey);
        char[] cResult = new char[bResult.length / 2];
        for(int i = 0;i<bResult.length/2;i++){
            cResult[i] =  (char) (((bResult[2*i] & 0xFF) << 8) | (bResult[2*i+1] & 0xFF));
        }
        info = new String(cResult);
        return info;
    }
 
    static void write(Socket s, String str0) {
    System.out.println("接受到一个客户端消息:" + s);
        OutputStream os = null;
        try {
        os = s.getOutputStream();
        PrintStream ps = new PrintStream(os);
        byte[] sendInfo = Crypttobyte.HloveyRC4(str0, MyKey);