求教一个Socket通信的异常修改.
我在写一个Socket通信的程序,今天碰到了一个错误:点击图中的最后一个连接,得到下面的代码现场:
点击图1中的倒数第二个链接,得到下面的第60行代码现场:
package s_port_package_BeiJing;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient {
public Socket getS() {
return s;
}
public void setS(Socket s) {
this.s = s;
}
private Socket s;
private InputStream in;
private OutputStream out;
private BufferedInputStream inByte;
private OutputStream outByte;
private BufferedReader inStr;
private PrintWriter outStr;
private long size = 0;
public SocketClient(String ip, int port) {
try {
s = new Socket(ip, port);
in = s.getInputStream();
out = s.getOutputStream();
inByte = new BufferedInputStream(in);
outByte = out;
inStr = new BufferedReader(new InputStreamReader(in));
outStr = new PrintWriter(new OutputStreamWriter(out));
} catch (Exception e) {
e.printStackTrace();
}
}
public String readStr() throws IOException {
synchronized (this.in) {
return this.inStr.readLine();
}
}
public void writeStr(String content,String MyThreadname) {
synchronized (this.out) {
outStr.println(content+"#"+MyThreadname);
outStr.flush();
}
}
public File readToFile(File file) throws IOException {
synchronized (this.in) {
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = this.inByte.read(temp))) {
fos.write(temp, 0, count);
fos.flush();
}
fos.close();
return file;
}
}
public void writeFile(File file) {
synchronized (this.out) {
size = file.length();
this.noticeFileSize(size);
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = fis.read(temp))) {
this.outByte.write(temp, 0, count);
this.outByte.flush();
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
// long progress = 0;
// while (size != (progress = getServerReceiveSize())) {
// System.out.println("progress : " + (progress / (size / 100)) + "%");
// }
}
}
private void noticeFileSize(long l) {
String str = l + "";
int j = 19 - str.length();
for (int i = 0; i < j; i++) {
str = "0" + str;
}
this.writeByByte(str);
}
protected void writeByByte(String content) {
synchronized (this.out) {
try {
this.out.write(content.getBytes());
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private long getServerReceiveSize() {
synchronized (in) {
byte[] b = new byte[19];
try {
this.in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
return Long.parseLong(new String(b));
}
}
public String getProgress() {
long l = this.getServerReceiveSize() / (size / 100);
if(100 == l) {
return null;
}
return l + " %";
}
public void getMyResourceBack(){
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(inStr!=null){
try {
inStr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(outStr!=null){
outStr.close();
}
}
}
求高手点拨:
我的错误如何修改?
谢谢高手!! --------------------编程问答-------------------- 应该是SocketClient类的构造函数中创建Socket时候的异常,可以catch中输出特定的字符来定位一下,缩小范围再考虑原因 --------------------编程问答-------------------- 可能是this.out这个对象为null,也就是在SocketClient的构造函数中out = s.getOutputStream();这句out得到的值为null。 --------------------编程问答-------------------- socket拒绝连接,你拿到的输出流当然是空了.. --------------------编程问答--------------------
求独行码夫的指导:
我已经仔细检查了对方通信节点的ServerSocket端口,没有错误.
造成“Socket拒绝连接”的现场的原因,还有哪些可能导致这个场景..?
--------------------编程问答-------------------- 有可能是防火墙或者网络问题,
你在本机用127.0.0.1 自己测试一下就知道程序是不是正常了.
补充:Java , Java SE