java Socket问题。贴出源程序。大家帮忙看看。
receiveServer.java
import java.io.*;import java.util.*;import java.net.*; public class receiveServer { final int RECEIVE_PORT=9090; public receiveServer() { ServerSocket rServer=null; Socket request=null; Thread receiveThread=null; try{ rServer=new ServerSocket(RECEIVE_PORT); rServer.setSoTimeout(0); while(true){ System.out.println("等待用户请求."); request=rServer.accept(); System.out.println("接收客户机连接请求."); receiveThread=new serverThread(request); receiveThread.start(); } }catch(IOException e){ e.printStackTrace(); } } public static void main(String args[]){ new receiveServer(); }}import java.net.*;import java.io.*;public class serverThread extends Thread { Socket clientRequest; InputStreamReader reader; BufferedReader input; InputStream inputStream = null; PrintWriter output; OutputStreamWriter writer; OutputStream outputStream; public serverThread(Socket s) { this.clientRequest=s; try{ outputStream = clientRequest.getOutputStream(); inputStream = clientRequest.getInputStream(); reader = new InputStreamReader(inputStream); writer = new OutputStreamWriter(outputStream); input = new BufferedReader(reader); output = new PrintWriter(writer,true); }catch(IOException e){ e.printStackTrace(); } } public void run(){ String command=null; String str=null; boolean done=false; while(!done){ try{ str=input.readLine(); }catch(IOException e){ System.out.println(e.getMessage()); } command=str.trim().toUpperCase(); if(str==null || command.equals("QUIT")) done=true; else if(command.equals("HELP")){ output.println("query"); output.println("quit"); output.println("help"); output.println("sendfile"); } else if(command.startsWith("QUERY")){ output.println("query"); }else if(command.startsWith("SENDFILE")){ output.println("READY\n"); output.flush(); try{ String savePath = "/home/hwcd/002.xls"; DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))); DataInputStream inputS = new DataInputStream(new BufferedInputStream(this.inputStream)); System.out.println("文件的长度为:" + inputS.readLong() + "\n"); System.out.println("开始接收文件!" + "\n"); DataOutputStream outputS = new DataOutputStream(this.outputStream); byte[] buf=new byte[4096]; while(true){ int num=0; num = inputS.read(buf); //客户端会一直停在这个地方。 System.out.println("num="+num+"\n"); if (num == -1) { break; } fileOut.write(buf, 0, num); } System.out.println("接收完成:"+clientRequest.isClosed()+ "\n"); outputS.writeUTF("OK\n"); outputS.flush(); done=true; fileOut.close(); }catch(Exception e){ e.printStackTrace(); done=true; } }else if(!command.startsWith("HELP") && !command.startsWith("SENDFILE") && !command.startsWith("QUIT") && !command.startsWith("QUERY")){ output.println("Command not Found!Please refer to the HELP!"); } } try{ clientRequest.close(); }catch(IOException e){ System.out.println(e.getMessage()); } command=null; }}
import java.io.*;import java.net.*;public class FileClient { public static void main(String[] args) throws Exception { Socket server = new Socket(InetAddress.getLocalHost(), 9090); OutputStream output = server.getOutputStream(); InputStream input = server.getInputStream(); BufferedReader in =new BufferedReader(new InputStreamReader(input)); PrintWriter out =new PrintWriter(output); out.println("sendfile\n"); out.flush(); while(true){ if(in.readLine().equals("READY")) break; } System.out.println("1111\n"); String filePath = "/home/hwcd/001.xls"; File file = new File(filePath); DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); System.out.println("2222\n"); System.out.println("3333"); DataOutputStream ps = new DataOutputStream(output); System.out.println("4444\n"); ps.writeUTF(file.getName()); ps.flush(); ps.writeLong((long) file.length()); ps.flush(); System.out.println("5555\n"); int bufferSize = 4096; byte[] buf = new byte[bufferSize]; while(true){ int read = 0; if (fis != null) { read = fis.read(buf); } if (read == -1) { break; } ps.write(buf, 0, read); } ps.flush(); System.out.println("发送完成:" + server.isClosed()+ "\n"); ps.write("sendfileend\n".getBytes()); ps.flush(); System.out.println("6666\n"); while(true){ if(in.readLine().equals("OK")){ break; } } System.out.println("处理完成\n"); out.close(); ps.close(); }}while(true){ if(in.readLine().equals("OK")){ break; } }
未结的帖子数:2 未结的总分数:70
结贴的百分比:88.24 % 结分的百分比:89.39 %
无满意结贴率:20.00 % 无满意结分率:23.73 %
楼主加油
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=reelcol
[解决办法]
1. 要先判断接收的是不是 -1 ,如果是就break;如果不是才接收。否则num = inputS.read(buf); 会阻塞线程。
2. 我不太明白LZ的意思,但是如果是想在传完文件后再传一些字符串的话,可以在文件传输的最后把字符串拼接上去,只是需要在文件的最后一个字符和字符串的字一个字符之间加上一个用于分割的符号就可以了。server端识别到这个符号就能知道,文件已经传输完毕,后面跟的是字符串
[解决办法]
其实,建议LZ在接收数据之前先判断一下,在socket中一次性可接收的数据量
[解决办法]
你传递文件完毕只好不是还给服务端发送了“sendfileend”
你可以在服务端读取的时候判断内容有这个的就跳出读取的循环。
或者你可以设置一个readtimeout,在超时抛出异常的时候跳出读取的循环。