【发布时间】:2020-07-24 22:26:17
【问题描述】:
根据你们的所有建议编辑我的代码,并将代码压缩到我可以查明导致问题的代码行的位置。 服务器代码:`
公共类 Server2 {
public static void main(String args[]) throws Exception {
ServerSocket servsock = null;
Socket sock = null;
int SOCKET_PORT = 12362;
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_SEND = "SFileToBeSent.txt";
String FILE_TO_RECEIVED = "CFileReceived.txt";
int FILE_SIZE = 6022386;
try {
System.out.println("Server created.");
servsock = new ServerSocket(SOCKET_PORT); // creates servsock with socket port
while (true) {
System.out.println("Waiting for connection...");
try {
sock = servsock.accept(); // accepts a socket connection
System.out.println("Accepted connection : " + sock);
System.out.println();
os = sock.getOutputStream(); // sets Output Stream using the sockets output stream
is = sock.getInputStream(); // get input stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
// if any streams or sockets are not null, the close them
if (os != null)
os.close();
if (is != null)
is.close();
if (sock != null)
sock.close();
} // end of finally
} // end of while
} // end of try
finally {
if (servsock != null)
servsock.close();
} // end of finally
}
}
公共类 Client2 {
public static void main(String args[]) throws Exception {
Socket sock = null; // used in main
String SERVER = "localhost"; // local host
int SOCKET_PORT = 12362; // you may change this
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_RECEIVED = "SFileReceived.txt";
String FILE_TO_SEND = "CFileToBeSent.txt";
int FILE_SIZE = 6022386;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");
System.out.println();
// get input and output from socket
is = sock.getInputStream(); // get input stream from socket
os = sock.getOutputStream(); // get output stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
if (sock != null)
sock.close();
if (os != null)
os.close();
if (is != null)
is.close();
} // end of finally
}
}
服务器输出:--------
服务器已创建。
等待连接...
接受的连接:Socket[addr=/127.0.0.1,port=51565,localport=12362]
发送 SFileToBeSent.txt(32 字节)
要发送的文件中的字节数组:[B@4e25154f
发送文件完成
接收到的文件可用字节数:0
要接收的文件中的字节数组:[B@70dea4e
客户端输出--------
正在连接...
收到的文件可用字节数:32
要接收的文件中的字节数组:[B@4e25154f
while 读取的字节数:32
bos.write
当前 += bytesRead
我仍然有 InputStream 的问题,我通过调试发现服务器和客户端都卡在接收部分中的 while() 语句上。对于服务器来说,它会立即停止,而客户端会经过一次 while 循环,然后在遇到 while 语句时停止。
如果有人有任何建议或解决方案,将不胜感激!
【问题讨论】:
-
TCP 套接字是全双工的,Java
Sockets和您从中获得的流也是如此。没有干扰。您的许多变量在它们不应该是静态的时候是静态的。 -
@user207421 感谢您的帮助,但我不完全理解您所说的某些变量不应该是静态的意思。这是否意味着如果仅在单个方法中使用变量,我应该将变量移动到使用它们的方法中?
-
为了隔离问题,请添加打印语句。或者您可以使用调试器来跟踪程序停止的语句。
标签: java sockets client inputstream outputstream