问题描述
||
大家好,我设计了一个服务器客户端,该客户端通过套接字传输数据。一切正常,当我在计算机上运行它时,它可以100%地运行。当我在另一台计算机上运行服务器而我的客户端上运行时,我无法获取数据。当我在我的机器上运行服务器并且在他们的计算机上运行客户端时,我无法放置数据,但是我可以得到。我不知道发生了什么事,也许您可以阐明一些想法。
还有更多代码可以使此工作正常进行,但我省略了该代码以减少复杂性。如果您对此有所了解,请告诉我为什么它可以在我的系统上运行而不在服务器上运行?有人知道如何调试吗?我的意思是这是在服务器上运行的,因为我不能在服务器上调试(我的系统上一切正常吗?),我该如何调试服务器?
服务器:
if (get.equals(\"get\")) {
try {
Copy copy = new Copy(socket,dir);//maybe dir is not needed
String name = input.substring(4);
File checkFile = new File(dir.getCurrentPath(),name);
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
if (checkFile.isFile() && checkFile.exists()) {
outToClient.writeBytes(\"continue\" + \"\\n\");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
boolean cont = false;
String x;
while (!cont) {
if ((x = inFromServer.readLine()).equals(\"continue\")) {
cont = true;
}
}
copy.copyFile(name);
output = \"File copied to client successfully\" + \"\\n\";
} else {
outToClient.writeBytes(\"File failed to be copied to client\" + \"\\n\");
output = \"\";
}
} catch (Exception e) {
output = \"Failed to Copy File to client\" + \"\\n\";
}
} else if (get.equals(\"put\")) {
//so the client sends: the put request
//then sends the length
try {
DataInputStream inFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
outToClient.writeBytes(\"continue\" + \"\\n\");
long lengthLong = (inFromClient.readLong());
int length = (int) lengthLong;
byte[] recieveFile = new byte[length];//FIX THE LENGTH
// InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(\"Copy \" + input.substring(4));
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead;
int current = 0;
bytesRead = inFromClient.read(recieveFile,recieveFile.length);
current = bytesRead;
do {
bytesRead = inFromClient.read(recieveFile,current,(recieveFile.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > 0); // FIX THE LENGTH
bos.write(recieveFile,current);
bos.flush();
bos.close();
output = \"File copied to Server successfully\" + \" \\n\";
复制类:
File checkFile = new File(dir.getCurrentPath(),file);
if (checkFile.isFile() && checkFile.exists()) {
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
// byte[] receivedData = new byte[8192];
File inputFile = new File(dir.getCurrentPath(),file);
byte[] receivedData = new byte[(int) inputFile.length()];
long length = inputFile.length();
outToClient.writeLong(length);
//maybe wait here for get request?
DataInputStream dis = new DataInputStream(new FileInputStream(getCopyPath(file)));
dis.read(receivedData,receivedData.length);
OutputStream os = socket.getOutputStream();
outToClient.write(receivedData,receivedData.length);//outputStreasm replaced by Datatoutputstream
outToClient.flush();
客户端类:
else if (sentence.length() > 3 && sentence.substring(0,3).equals(\"get\")) {
outToServer.writeBytes(sentence + \"\\n\");
String response = inFromServer.readLine();
if (response.equals(\"File failed to be copied to client\")) {
System.out.println(response);
} else {
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
outToClient.writeBytes(\"continue\" + \"\\n\");
long lengthLong = (inFromClient.readLong());
int length = (int) lengthLong;
byte[] recieveFile = new byte[length];
FileOutputStream fos = new FileOutputStream(\"Copy \" + sentence.substring(4));
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead;
int current = 0;
bytesRead = inFromClient.read(recieveFile,recieveFile.length);
current = bytesRead;
do {
bytesRead = inFromClient.read(recieveFile,(recieveFile.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > 0);
bos.write(recieveFile,current);
bos.flush();
bos.close();
}
} else if (sentence.length() > 3 && sentence.substring(0,3).equals(\"put\")) {
File checkFile = new File(dir.getCurrentPath(),sentence.substring(4));
if (checkFile.isFile() && checkFile.exists()) {
try {
outToServer.writeBytes(sentence + \"\\n\");
boolean cont = false;
String x;
while (!cont) {
if ((x = inFromServer.readLine()).equals(\"continue\")) {
cont = true;
}
}
String name = sentence.substring(4);
copy.copyFile(name);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)