读书人

求高手点拨一个Socket通信的错误的修改

发布时间: 2013-11-19 22:55:29 作者: rapoo

求高手点拨一个Socket通信的异常的修改方案.
高手,您好:
我在写一个Socket通信程序,在我运行了我的代码后,报出了下面的异常报告:
异常报告第一幅图片是:
求高手点拨一个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();
}
}
}


第1幅图和第2幅图中的“MyJButtonLogin类”他的源代码如下:
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();
}
}
});
}
}

弟我经过初步在网络中的学习,了解到:
Socket is closed异常出现的原因是:
一般是由于通信过程中的某一方主动关闭了Socket,而却又想进行数据操作。
的原因导致的..
经过了弟我对于我的程序的“逐类查找”:弟我的程序在这个登录功能上,只有整个C端负责“提供读写两个方法的Lib类中的readsocketUTF()方法具有"close();"语句”,于是,我进行了对应于Socket is closed异常的出现原因进行了代码修改.弟我将这个类型中:
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();
}
}
}


我将上面Lib类型中的“第72”与“第73”行代码的内容,进行了“直接删除”..
再次运行,我得到了下面的结果:
求高手点拨一个Socket通信的错误的修改方案
我的上述“MyJButtonLogin类”是负责我的用户点击“登录按钮”之后所进行的之后一系列代码执行的类型.
她的源代码如下:
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();
}
}
});
}
}


希望得到高手的点拨:
弟我的程序,修改后出现的错误原因,是什么...?
(以上“Lib类”中的代码实施方案,感谢CSDN论坛中的朋友昵称与户名均为“happytaocool”的高手的点拨与指导!!谢谢您!!happytaocool哥!!)
谢谢高手!!
一百分奉上!!
一位日日夜夜向着理想奔跑的筑梦者
2013年11月18日晨 Java调试 Java?Socket Java异常 Java排错 Java网络编程
[解决办法]
这么长,又没有缩进,不知道从哪看起。找了半天没找到main函数。
[解决办法]
synchronized(so3){
str = Lib.readsocketUTF(so3);
}这句有问题
[解决办法]
经过你的修改不是异常没有了吗?
你发送完,在finally加上
ps.close();
os.close();

读书人网 >J2SE开发

热点推荐