求高手点拨一个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);
System.out.println(sendInfo);
ps.write(sendInfo, 0, sendInfo.length);
ps.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package c_port_packageBJDoctor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
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";
String D_PORT_SERVER = "127.0.0.1";
static NoteUserLoginFrame cjco;
InputStream inputstream = null;
boolean isMyUser = false;
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;
String str = "";
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public MyJButtonLogin(String button, String UID, String pwd, Login login,Socket s2,Socket s3) {
super(button);
so2 = s2;
so3 = s3;
MyUID = UID;
MyPWD = pwd;
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 + "%"
+ "Doctor" + "%" + str3 + "#" + "C" + "#"
+ "Doctor" + "#" + "0";
System.out.println(aa);
synchronized(so3){
Lib.write(so3,aa);
}
System.out.println("MyJButtonLogin类启动");
synchronized(so3){
str = Lib.readsocketUTF(so3);
}
System.out.println("888888");
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();
}
}
});
}
}
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);
System.out.println(sendInfo);
ps.write(sendInfo, 0, sendInfo.length);
ps.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package c_port_packageBJDoctor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
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";
String D_PORT_SERVER = "127.0.0.1";
static NoteUserLoginFrame cjco;
InputStream inputstream = null;
boolean isMyUser = false;
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;
String str = "";
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public MyJButtonLogin(String button, String UID, String pwd, Login login,Socket s2,Socket s3) {
super(button);
so2 = s2;
so3 = s3;
MyUID = UID;
MyPWD = pwd;
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 + "%"
+ "Doctor" + "%" + str3 + "#" + "C" + "#"
+ "Doctor" + "#" + "0";
System.out.println(aa);
synchronized(so3){
Lib.write(so3,aa);
}
System.out.println("MyJButtonLogin类启动");
synchronized(so3){
str = Lib.readsocketUTF(so3);
}
System.out.println("888888");
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();
}
}
});
}
}