我试图制作一个简单的客户端服务器(带有一些gui),服务器发送数据并且客户端接收它(文件的保存也起作用)但是客户端似乎被卡在接收循环中。 我用“.start()”开始了两个线程。 我用粗体标记了问题的位置。
我的一些人知道为什么客户端的程序不会继续... - 祝福ghali
服务器:
public class Server extends Thread implements Runnable{
public File choosenfile;
public int port = 42001;
public boolean running = true;
public String myAdress = "localhost";
public ServerSocket serverSocket;
public BufferedInputStream bis;
public boolean debug = true;
public OutputStream os;
public void run() {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(running){
Socket socket = null;
try {
System.out.println("Listening on port: "+port);
socket = serverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Dies ist die stelle an der man einen Thread aufmachen kann
*/
// 1. File-Hülle anlegen dazu muss man deren größere wissen
if(debug) System.out.println("Server: 1.");
int count;
byte[] buffer = new byte[1024];
try {
os = socket.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(choosenfile));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
while ((count = in.read(buffer)) > 0) {
os.write(buffer, 0, count);
os.flush();
}
} catch (IOException e1) {
e1.printStackTrace();
}
if(debug) System.out.println("Server: finish sending");
try {
os.flush();
if(debug) System.out.println("Server: close");
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
running =false;
}
}
}
客户端:
public class Client extends Thread implements Runnable{
public Gui gui; //graphical surface
public boolean running = true;
public boolean debug =true;
//Networkstuff
Socket socket;
DataInputStream is;
public byte[] returnFile;
public Client(Gui gui){
this.gui = gui;
}
public void run() {
try {
socket = new Socket(InetAddress.getByName(gui.inetAdress),gui.portServer);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(gui.path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int count;
InputStream in = null;
try {
in = socket.getInputStream();
int i =0;
}
catch (IOException e1) {
e1.printStackTrace();
}
try {
while((count=in.read(buffer)) >0){
fos.write(buffer, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**//folloing functions are not executed anymore and i dont know why**
gui.loadPicture();
try {
fos.close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
答案 0 :(得分:1)
这很简单。 read()
阻止
服务器向流发送字节,但从不关闭它,因此从该流读取的客户端无法知道是否会有更多数据进入,因此它会阻塞。
在服务器端关闭流,或设计一个允许客户端知道何时停止读取的协议(例如,发送文件中的字节数,然后发送这些字节)。