当前位置:编程学习 > JAVA >>

求高手点拨一个“Socket通信”中的问题.

高手,您好:
    我在写一个Socket通信程序,现在我碰到了一个MyEclipse报出的错误:
  
    由上面的图片中报出的错误,弟我判断出:
    下面的S端的“读取数据”的方法,或者C端的“写出数据”的方法,出现了问题.
    (S端的接收数据的方法源码)
    
package s;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
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) throws UnsupportedEncodingException{
        String info = "";
         try {
is = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
try {
str = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info = Systemcrypt.HloveyRC4(str,MyKey);
        return ChangeCharset.toISO_8859_1(info);
    }
}

    (C端的写出数据的方法)
    

    import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
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 void write(Socket s,String str0) throws UnsupportedEncodingException{

System.out.println("接受到一个客户端消息:" + s);
OutputStream os = null;
try {
os = s.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
String sendInfo = Systemcrypt.HloveyRC4(ChangeCharset.toISO_8859_1(str0), MyKey);
ps.println(sendInfo);
}
}

    希望高手能够帮助我看一下:
    弟我的代码,错误在什么地方?
    如何修改...?
    谢谢高手的点拨!!
    一百分奉上!!
                                                           一位日日夜夜向着理想奔跑的筑梦者
                                                         2013年11月19日早晨1点正 Java调试 Java异常 Java Socket Java编程 Java流 --------------------编程问答-------------------- 这也太抽象了。。。
错误信息没看明白。。
这些给出的代码看起来没什么问题,但是这些代码中的流都没有看到flush和close --------------------编程问答-------------------- ChangeCharset是你自己写的个东西吗? --------------------编程问答-------------------- GeekDonie哥,弟我的代码中PrintStream流,他是具有“自动调用flush()方法”在每一个“写出语句”之后的..
PrintStream类的JDK的相关解释如下:
    
    他是具有“写入数组后自动调用flush()方法”的功能的..
    并且,GeekDonie哥,弟我的C端的报错代码的现场为:
    
			@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);
synchronized(so3){
Lib.write(so3,aa);
}
System.out.println("MyJButtonLogin类启动");

synchronized(so3){
        str = Lib.readsocketUTF(so3);
}
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,MySysNum);
}else{
new NoteClientUIDNotExist(so2);
}
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}

    GeekDonie哥,请问:
    弟我的上层调用这两个方法的C端的代码,(弟我是将着两个方法都写在了一个类型中,并且让包含这两个方法的类型,都在两个系统端点中同时出现的),他的调用逻辑和场景,还有什么问题吗...?
    谢谢GeekDonie哥!! --------------------编程问答-------------------- 也希望CSDN论坛中的高手,withiter版主,能够为弟我的代码,作指导. --------------------编程问答-------------------- ChangeCharset这个类型,不是我自己写的.
    是我自己借鉴的网络中的一个类型,进行的“我的工程的拿来主义”.
    这不重要,关键是将项目做成,借助街上随地可以开上的坦克,去抗日,去杀鬼子,保家卫国!!
    纠结这个问题有什么意义..? --------------------编程问答-------------------- 把异常信息贴出来吧,这样有针对性的纠错效率上高得多。
不然直接这样找,很浪费大家时间。 --------------------编程问答-------------------- 出什么问题了? --------------------编程问答-------------------- 由上面的图片中报出的错误,弟我判断出:

那张图片报啥错了?没看到有报错丫。 --------------------编程问答-------------------- StoneImprint哥,谢谢您的点拨!!
    弟我现在初步将错误定位在了:“S端在接收C端的写入数据类型,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 = "";
        System.out.println("try1");
        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){
             System.out.println("try2");
                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();
        }
        info = Systemcrypt.HloveyRC4(str, MyKey);
        return info;
    }
}

    他的“读取数据的readsocketUTF()方法”,未能够在整个项目的调用这个类型的“上层类”中,进行自己的“返回值”的传递,为了能够在这个端点中进行错误定位,弟我在上文的代码中进行了第41行代码的“输出语句”的书写.(下文中用到了这个输出进行判断),调用它的上层类的现场为:
    
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");
buffertemp = Lib.readsocketUTF(so1);
System.out.println(buffertemp);
buffer = buffertemp.split("[#]");
condition = Integer.parseInt(buffer[0]);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
MyInfo = buffer[1];
System.out.println("MyInfo:" + MyInfo);

    并且,我在上面的Lib类中的第55行代码的“Systemcrypt类型”,它的作用是:“按照一定的密钥加密或者解密传入的字符串参数”。
    这里的“Systemcrypt类”的源代码如下:
    
package c_port_packageBJDoctor;
public class Systemcrypt{

public static String HloveyRC4(String aInput,String aKey)   
    {   
        int[] iS = new int[256];   
        byte[] iK = new byte[256];   
          
        for (int i=0;i<256;i++)   
            iS[i]=i;   
              
        int j = 1;   
          
        for (short i= 0;i<256;i++)   
        {   
            iK[i]=(byte)aKey.charAt((i % aKey.length()));   
        }   
          
        j=0;   
          
        for (int i=0;i<255;i++)   
        {   
            j=(j+iS[i]+iK[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
        }   
      
      
        int i=0;   
        j=0;   
        char[] iInputChar = aInput.toCharArray();   
        char[] iOutputChar = new char[iInputChar.length];   
        for(short x = 0;x<iInputChar.length;x++)   
        {   
            i = (i+1) % 256;   
            j = (j+iS[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
            int t = (iS[i]+(iS[j] % 256)) % 256;   
            int iY = iS[t];   
            char iCY = (char)iY;   
            iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;      
        }   
          
        return new String(iOutputChar);   
                  
    } 
}

    上面的这个系统端点的最后console窗口的输出为:
       
    上面的这个输出窗口中的内容显示:“我的程序是在上文中的Lib类里,进行了"面向C端传输的字符串数据”进行了“对应于C端节点传输过来的字符串所对应的字节数”所进行的“循环次数的处理”,进行循环的使命:“将字节数组转化为字符串,交给字符串变量str”,从而让整个程序,在之后的代码里,进行“将这里的字符串,经过解密,进行最后得到的网络Socket字符串成果的返回”.
    希望得到高手的点拨:
    为什么我的代码,在S端调用Lib类的readsocketUTF()方法的上层类中,最后在上层类型中,未能打印出自己所接收到的C端节点传送过来的“字符串数据”...?
    谢谢高手!!
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,