我做这样一个应用:
client和server连接,client发一个xml给server,server处理数据后再回传一个xml给client,但是在我回传的时候,系统提示socket closed!为什么呢,搞了很久,找不到原因,请那位兄弟赐教!!!
server:package com.socketserver;import java.net.*;
import java.io.*;/**
*
*/
public class SendFileSocket extends Thread { public static void main(String[] args) {
server();// 启动服务器程序 } private static final int PORT = 6000;
private Socket s; public SendFileSocket(Socket s) {
this.s = s;
} public void run() {
OutputStream os =null;
InputStream is = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
os = s.getOutputStream();
is = s.getInputStream();
os.write("Hello,welcome you!".getBytes());
byte[] buf = new byte[100];
while (true) { int len = is.read(buf);
String revStr = new String(buf, 0, len);
System.out.println("client send message code is " + revStr); switch (Integer.parseInt(revStr)) {
case 5001:
System.out.println("5001");
boolean authenend = false;
String[] authen = new String[2]; try {
//本地保存路径,文件名会自动从服务器端继承而来。
String savePath = "D:\\AuthenUser.xml";
int bufferSize = 8192;
byte[] readbuf = new byte[bufferSize];
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
while (true) {
int read = 0;
if (is != null) {
read = is.read(readbuf);
} if (read == -1) {
break;
} fileOut.write(readbuf, 0, read);
}
System.out.println("接收完成,文件存为" + "\n"); fileOut.close();
} catch (Exception e) {
System.out.println("接收消息错误" + "\n");
e.printStackTrace();
}
AuthenUserXML authenxml = new AuthenUserXML();
authen = authenxml.ReadReceiveXMLFile("D:\\AuthenUser.xml"); for (int i = 0; i < authen.length; i++) {
System.out.println(authen[i]);
} // GetUmInfo um = new GetUmInfo();
// authenend = um.userAuth(authen[1], authen[2]);
authenend = true;
AuthenUserXML authenxml2 = new AuthenUserXML();
File returnfile = new File("D:\\AuthenUserReturn.xml");
if (authenend) {
if (returnfile.exists()) {
authenxml2.ModiXMLFile("D:\\AuthenUserReturn.xml", 3, "aa");
} else {
authenxml2.createReturnXMLFile("D:\\AuthenUserReturn.xml", 3, "aa");
}
} else {
if (returnfile.exists()) {
authenxml2.ModiXMLFile("D:\\AuthenUserReturn.xml", 4, "aa");
} else {
authenxml2.createReturnXMLFile("D:\\AuthenUserReturn.xml", 4, "aa");
}
}
os.write("5002".getBytes());
try {
// 选择进行传输的文件 System.out.println("Server start sent file to client."); dis = new DataInputStream(new BufferedInputStream(new FileInputStream("D:\\AuthenUserReturn.xml")));
dos = new DataOutputStream(s.getOutputStream()); int bufferSize = 8129;
byte[] writebuf = new byte[bufferSize]; while (true) {
int read = 0;
if (dis != null) {
read = dis.read(writebuf);
} if (read == -1) {
break;
}
dos.write(writebuf, 0, read);
}
dos.flush();
System.out.println("Server send file complete!");
} catch (IOException e) {
System.out.println("Server send file error!");
e.printStackTrace();
}
break;
case 5003: break;
default:
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
os.close();
is.close();
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void server() {
System.out.println("This is server");
try {
ServerSocket ss = new ServerSocket(PORT);
int count = 0;
while (true) {
// 创建一个Socket等待客户端连接 Socket s = ss.accept();
count++;
System.out.println("This is the " + count + "'st client connetion!");
new SendFileSocket(s).start();// 启动一个线程为这个客户端服务 }
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
client和server连接,client发一个xml给server,server处理数据后再回传一个xml给client,但是在我回传的时候,系统提示socket closed!为什么呢,搞了很久,找不到原因,请那位兄弟赐教!!!
server:package com.socketserver;import java.net.*;
import java.io.*;/**
*
*/
public class SendFileSocket extends Thread { public static void main(String[] args) {
server();// 启动服务器程序 } private static final int PORT = 6000;
private Socket s; public SendFileSocket(Socket s) {
this.s = s;
} public void run() {
OutputStream os =null;
InputStream is = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
os = s.getOutputStream();
is = s.getInputStream();
os.write("Hello,welcome you!".getBytes());
byte[] buf = new byte[100];
while (true) { int len = is.read(buf);
String revStr = new String(buf, 0, len);
System.out.println("client send message code is " + revStr); switch (Integer.parseInt(revStr)) {
case 5001:
System.out.println("5001");
boolean authenend = false;
String[] authen = new String[2]; try {
//本地保存路径,文件名会自动从服务器端继承而来。
String savePath = "D:\\AuthenUser.xml";
int bufferSize = 8192;
byte[] readbuf = new byte[bufferSize];
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
while (true) {
int read = 0;
if (is != null) {
read = is.read(readbuf);
} if (read == -1) {
break;
} fileOut.write(readbuf, 0, read);
}
System.out.println("接收完成,文件存为" + "\n"); fileOut.close();
} catch (Exception e) {
System.out.println("接收消息错误" + "\n");
e.printStackTrace();
}
AuthenUserXML authenxml = new AuthenUserXML();
authen = authenxml.ReadReceiveXMLFile("D:\\AuthenUser.xml"); for (int i = 0; i < authen.length; i++) {
System.out.println(authen[i]);
} // GetUmInfo um = new GetUmInfo();
// authenend = um.userAuth(authen[1], authen[2]);
authenend = true;
AuthenUserXML authenxml2 = new AuthenUserXML();
File returnfile = new File("D:\\AuthenUserReturn.xml");
if (authenend) {
if (returnfile.exists()) {
authenxml2.ModiXMLFile("D:\\AuthenUserReturn.xml", 3, "aa");
} else {
authenxml2.createReturnXMLFile("D:\\AuthenUserReturn.xml", 3, "aa");
}
} else {
if (returnfile.exists()) {
authenxml2.ModiXMLFile("D:\\AuthenUserReturn.xml", 4, "aa");
} else {
authenxml2.createReturnXMLFile("D:\\AuthenUserReturn.xml", 4, "aa");
}
}
os.write("5002".getBytes());
try {
// 选择进行传输的文件 System.out.println("Server start sent file to client."); dis = new DataInputStream(new BufferedInputStream(new FileInputStream("D:\\AuthenUserReturn.xml")));
dos = new DataOutputStream(s.getOutputStream()); int bufferSize = 8129;
byte[] writebuf = new byte[bufferSize]; while (true) {
int read = 0;
if (dis != null) {
read = dis.read(writebuf);
} if (read == -1) {
break;
}
dos.write(writebuf, 0, read);
}
dos.flush();
System.out.println("Server send file complete!");
} catch (IOException e) {
System.out.println("Server send file error!");
e.printStackTrace();
}
break;
case 5003: break;
default:
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
os.close();
is.close();
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void server() {
System.out.println("This is server");
try {
ServerSocket ss = new ServerSocket(PORT);
int count = 0;
while (true) {
// 创建一个Socket等待客户端连接 Socket s = ss.accept();
count++;
System.out.println("This is the " + count + "'st client connetion!");
new SendFileSocket(s).start();// 启动一个线程为这个客户端服务 }
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;public class SendFileClient
{ private static final int Server_PORT = 6000;
private static final int Client_TIMEOUT = 6001;
public SendFileClient()
{ System.out.println( "This is client" );
byte[] buf = new byte[100];
String ipStr = "localhost";
Socket s = null;
OutputStream os =null;
InputStream is = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try
{
// 创建一个Socket
s = new Socket();
s.connect ( new InetSocketAddress (ipStr , Server_PORT ), Client_TIMEOUT );
os = s.getOutputStream( );// 输出流
is = s.getInputStream( );// 输入流
int len = is.read( buf );// 从输入流中读取数据到buf
System.out.println( new String( buf, 0, len ) );
os.write( "5001".getBytes( ) ); try {
// 选择进行传输的文件
System.out.println("Client starting send file!"); dis = new DataInputStream(new BufferedInputStream(new FileInputStream("c:\\AuthenUser.xml")));
dos = new DataOutputStream(s.getOutputStream()); int bufferSize = 8129;
byte[] buffile = new byte[bufferSize]; while (true) {
int read = 0;
if (dis != null) {
read = dis.read(buffile);
} if (read == -1) {
break;
}
dos.write(buffile, 0, read);
}
dos.flush();
System.out.println("Client send file complete!");
} catch (IOException e) {
System.out.println("Client send file error!");
e.printStackTrace();
} finally {
try {
dis.close();
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
len = is.read( buf );// 从输入流中读取数据到buf//这句时系统提示socket closed!
String tempStr = new String(buf,0,len);
if ( tempStr.trim()=="5002" )
{
System.out.println("client:5002");
try {
//本地保存路径,文件名会自动从服务器端继承而来。
String savePath = "E:\\AuthenUserReturn.xml";
int bufferSize = 8192;
byte[] readbuf = new byte[bufferSize]; DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(savePath)))); while (true) {
int read = 0;
if (is != null) {
read = is.read(readbuf);
} if (read == -1) {
break;
} fileOut.write(readbuf, 0, read);
}
fileOut.close();
} catch (Exception e) {
System.out.println("receive message error!");
e.printStackTrace();
}
System.out.println("File has been recerved successfully.");
} } catch ( Exception ex )
{
ex.printStackTrace( );
}finally{
try {
os.close();
is.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main( String[] args ) throws IOException
{
new SendFileClient();
}
}
Server start sent file to client.
Server send file error!
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.DataOutputStream.write(DataOutputStream.java:85)
at com.ctil.socketserver.SendFileSocket.run(SendFileSocket.java:122)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.checkBounds(String.java:283)
at java.lang.String.<init>(String.java:370)
at com.ctil.socketserver.SendFileSocket.run(SendFileSocket.java:39)
Client端报错:
This is client
Hello,welcome you!
Client starting send file!
Client send file complete!
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.net.SocketInputStream.read(SocketInputStream.java:90)
at com.ctil.socketserver.SendFileClient.<init>(SendFileClient.java:91)
at com.ctil.socketserver.SendFileClient.main(SendFileClient.java:142)
但是我能否在一个socket连接中实现双向传递文件呢?怎么实现?
xml比较大,你可以随便换个文件就可以!文本都行。