Java中Socket传输文件中Read阻塞的问题!!!
i8dea 2007-07-03 04:46:17 现在需要Client和Server通过Socket相互传文件,流程是这样的:
connect
Client------------->Server;
sendfile
Client------------->Server;
server接收成功后:
sendfile
Server------------->Client;
所以Client和Server通过同一个通道互相传输文件,目前我的代码中出现了,Client发送文件给Server过程中,Server全部接收下来了,按理说再调用Read函数,应该返回一个“-1”的,可现在却吊死在那了,不再往下运行,不知道为什么,寻求高手帮助!!!
Server端代码:
import java.net.*;
import java.io.*;
import java.text.*;
public class Server extends ServerSocket {
public static final int SERVER_PORT = 11111;
public Server() throws IOException{
super(SERVER_PORT);
try{
while(true){
Socket s = accept();
System.out.println("create thread!");
new CreateServerThread(s);
}
}
catch(IOException e){
e.printStackTrace();
}
finally{
close();
}
}
class CreateServerThread extends Thread
{
private Socket client;
private InputStream in;
private PrintWriter out;
byte[] buffer = new byte[1000];
int length = 0;
public CreateServerThread(Socket s) throws IOException
{
client = s;
client.setSoTimeout(1000);
in = new DataInputStream(client.getInputStream());
System.out.println("--- Welcome ---");
start();
}
public void run()
{
try
{
FileOutputStream output = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String filename = formatter.format(new java.util.Date());
File file = new File("D:\\"+ filename +".xml");
output = new FileOutputStream(file);
length = in.read(buffer);
while ((length) != -1)
{
output.write(buffer, 0, length);
length = in.read(buffer);
}
output.close();
System.out.println("out while");
file = new File("D:\\NewFile2.xml");
DataInputStream input = new DataInputStream(new FileInputStream(file));
DataOutputStream output1 = new DataOutputStream(client.getOutputStream());
while ((length = input.read(buffer)) != -1) {
output1.write(buffer, 0, length);
}
System.out.println("--- See you, bye! ---");
client.close();
}
catch (IOException e)
{}
}
private String createMessage(String line)
{
return new java.util.Date().toString();
}
}
}
Client代码:
import java.io.*;
import java.net.*;
public class Client {
public Client() {
}
public static void main(String[] args) {
int port = 11111;
Socket s = null;
try {
s = new Socket(InetAddress.getLocalHost(), port);
if (s.isConnected())
System.out.println("connected");
byte[] buffer = new byte[1000];
int length = 0;
File file = new File("D:\\1.txt");
DataInputStream input = new DataInputStream(new FileInputStream(file));
DataOutputStream output = new DataOutputStream(s.getOutputStream());
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
output.flush();
}
output.flush();
System.out.println("客户端文件发送成功");
DataInputStream in = new DataInputStream(s.getInputStream());
File newFile = new File("D:\\NewFilesss.xml");
if (!newFile.exists())
newFile.createNewFile();
FileOutputStream fileOut = new FileOutputStream(newFile);
while ((length = in.read(buffer)) != -1) {
fileOut.write(buffer, 0, length);
}
System.out.println("从服务器端接收文件成功");
fileOut.close();
} catch (IOException e) {
System.out.println(e);
} finally {// Always be sure to close the socket
try {
if (s != null)
s.close();
}
catch (IOException e1) {
}
}
}
}