我对套接字编程,android studio和Java总体来说还是一个新手,所以请原谅我的代码经验不足和混乱。这也是我第一次在这里提问。我最初也使用DataOutputStream来发送图像,但是我还是使用BufferedWriter来让服务器发送回复,因为readUTF函数似乎不起作用,即使我看到了答案,“ resultfromServer”变量仍然是“”关于我已经在使用的建议AsyncTask。使用它的行被注释。 客户端应该将图像发送到服务器和PC上的服务器,服务器应该将文件的内容作为字符串发送回去。最初,服务器应该等待几秒钟,而客户端也要等待,以便模拟处理,但是我出于测试目的将该部分注释掉了。我的Java IDE是Notepad ++。这段代码是许多在线教程的结果,我浪费了数周的时间对其进行调试。 为了进行测试,我将客户端代码的简化形式转移到Java,并在PC上同时运行了客户端和服务器。无法处理图像发送。这是代码。
import java.util.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.net.*;
import javax.imageio.ImageIO;
import java.lang.*;
class myServerTry
{
public static void main(String[] args) throws InterruptedException{
ServerSocket serverSocket;
Socket socket;
DataInputStream dis;
int len;
String somestring="";
try {
serverSocket = new ServerSocket(9998);
System.out.println("waiting\n");
while( (true)){
socket = serverSocket.accept();
InputStream is=socket.getInputStream();
dis=new DataInputStream(is);
len= dis.readInt(); //originally the length of the data input stream.
System.out.println("Received integer: " +len);
//while(somestring==""){
//somestring= dis.readUTF();
//}
System.out.println(somestring);
//Thread.sleep(5000); //Read file
String filepath= "C:\\Users\\mahno\\Desktop\\BlankFile.txt";
FileInputStream fr=new FileInputStream(filepath); /* include your file name which you want to move*/
byte b[]=new byte[2002];
fr.read(b, 0, b.length);
//to show on console
String result ="";
for(int i=0; i<2002 && b[i]!=0; i++){
result +=(char) b[i];
}
System.out.println(result);
OutputStream os=socket.getOutputStream();
//DataOutputStream dos= new DataOutputStream(os);
//dos.writeUTF(result);
BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(os));
writer.write(result);
os.close();
writer.close();
//close the socket
dis.readUTF();
fr.close();
is.close();
dis.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
客户端。
import java.util.*;
import java.net.*;
import java.io.*;
public class SendImageClient {
public static void main(String args[]) throws Exception {
Socket socket=null;
String resultfromServer="";
OutputStream outputStream;
InputStream inputstream;
DataOutputStream dos;
DataInputStream dis;
try {
socket = new Socket("192.168.10.33", 9998);
outputStream = socket.getOutputStream();
dos = new DataOutputStream(outputStream);
inputstream = socket.getInputStream();
//dis = new DataInputStream(inputstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
try {
dos.writeInt(9);
dos.writeUTF("Some data is being sent\n");
outputStream.flush();
dos.flush();
//Thread.sleep(10000);
while((resultfromServer = reader.readLine())=null){
} //read and put it in a string
dos.writeUTF("\nOK");
System.out.println(resultfromServer);
//System.out.println(resultfromServer);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputstream.close();
//dis.close();
reader.close();
outputStream.close();
dos.close();
}
} catch(Exception e){
}
socket.close();
}
}
双方都抛出异常。客户端例外:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at SendImageClient.main(SendImageClient.java:36)
服务器端异常:
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implClose(Unknown Source)
at sun.nio.cs.StreamEncoder.close(Unknown Source)
at java.io.OutputStreamWriter.close(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at myServerTry.main(myServerTry.java:58)
服务器端将一直工作,直到必须从客户端读取某些内容为止。