我用如下代码:
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String recvMsg = socketReader.readLine();
发送的字符串是:“123456\nabcd”
用这种方法遇到 "\n "符号会认为是结束符,接收终止了,后续的字母“abcd”不会接收到,请问用什么办法能够一次性接收所有的字符?多谢了!
------解决方案--------------------
import java.net.*;
import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.*;
import com.conf.Dbaccess;
import com.conf.*;
import com.file.FileList;
import com.file.ReadXML;
class serverThread extends Thread {
Socket clientRequest;
//用户连接的通信套接字
BufferedReader input; //输入流
PrintWriter output; //输出流
public serverThread(Socket s)
{ //serverThread的构造器
this.clientRequest=s;
int port;
ServerSocket serverSkt;
Socket clientSkt;
BufferedReader fileInfo;
BufferedInputStream fileReader;
BufferedOutputStream fileWriter;
String fileName;
PrintStream printStream;
//接收receiveServer传来的套接字
InputStreamReader reader;
OutputStreamWriter writer;
try {
//接收客户机连接请求
printStream = new PrintStream(clientRequest.getOutputStream());
fileInfo = new BufferedReader(new InputStreamReader(clientRequest.getInputStream()));
fileName = fileInfo.readLine();
System.out.println( "与 " + clientRequest.getInetAddress() + "建立连接 ");
printStream.println( "yes ");
System.out.print( "\nOK! 正在接收文件 " + fileName);
String szfilename= " ";
if(fileName.indexOf( "/ ")> 0){
szfilename= fileName.substring(fileName.lastIndexOf( "/ ")+1,fileName.length());
fileReader = new BufferedInputStream(clientRequest.getInputStream());
fileWriter = new BufferedOutputStream(new FileOutputStream( "d:\\test\\ "+szfilename));
int readin;
while((readin = fileReader.read()) != -1) {
//System.out.print( ". ");
fileWriter.write(readin);
}
fileReader.close();
fileWriter.flush();
clientRequest.close();
System.out.println( "\n文件接收完毕! ");
}
}
catch(IOException e) {
System.out.println(e.toString());
}
}
}
}
------解决方案--------------------
不要用readLine(),直接按字节读socket.inputStream();