伙计们,我是套接字编程的新手。我想将数据从客户端发送到服务器,并将某些文件从服务器发送回客户端,但仅在某些条件下。 因此,我有一个applet,可以从用户和按钮中获取一些输入。用户注册时,数据从客户端(小程序)发送到服务器,并存储在文件中(本地) 到目前为止,我已经能够做到。但是我的问题是我不知道该如何实现: 当用户仅单击getData按钮时,我必须将文件从服务器发送回客户端? 如此丰富的代码
Client.java
if (action.equals("register")) {
String name = name.getText().trim();
String address = addres.getText().trim();
User user = new User(name, address);
toServer.writeObject(user);
}
else if (action.equals("GetData")) {
DataInputStream dis = new
DataInputStream(socket.getInputStream());
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(dis));
String line = null;
while ((line = br.readLine()) != null) {
if (line.equalsIgnoreCase("quit")) {
break;
}
System.out.println("Line entered : " + line);
}
} catch (IOException e) {
System.out.println("Exception while reading input " +e);
} finally {
// close the streams using close method
try {
if (br != null) {
br.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing stream: " + ioe);
}
}
Server.java
while (true) {
Socket socket = serverSocket.accept();
fromClient = new ObjectInputStream(socket.getInputStream());
Object object = fromClient.readObject();
toFile.writeObject(object);
System.out.println("Object created");
}
有人可以帮我吗。 谢谢