这可能是新手问题,但我刚开始使用插座进行冒险。
所以我有Server和Client类,客户端可以向服务器添加服务并从服务器获取服务。我实现了添加功能,它工作正常,但我有问题将客户端serviceList发送给他。当我运行程序并使用getAllServices方法没有任何反应时,程序会永远等待某些东西,没有错误 - 没有错误。
这是我的代码:
//serverSide code
input=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
output= new PrintWriter(clientSocket.getOutputStream());
//same input and output declaration on client side
private void sendClientList() throws IOException{
List <ServerHandler> clientList=server.getClients();
int size=clientList.size();
output.println(size);
output.flush();
for(ServerHandler client: clientList){
output.println(client.getLogin());
output.flush();
}
}
private void getAllServices()throws IOException{
List <ServerHandler> clientList=server.getClients();
sendClientList();
int serviceListSize=0;
for(ServerHandler client: clientList){
List<Service> serviceList=getServiceList();
serviceListSize=serviceList.size();
output.println(serviceListSize);
output.flush();
for(Service service: serviceList){
output.println(service.getName());
output.flush();
output.println(service.getOwner());
output.flush();
output.println(service.getAvailable());
output.flush();
output.println(service.getData());
output.flush();
}
}
}
//client side code
private List <String> getClientsFromServer() throws IOException{
int getSize=0; //clientList size
getSize=Integer.parseInt(input.readLine());
for(int i=0;i<getSize;i++){
String name=input.readLine();
clientList.add(name);
}
return clientList;
}
private void getAllServices()throws IOException{
List<String> clientList=getClientsFromServer();
int serviceListSize=0;
for(String client: clientList){
serviceListSize=Integer.parseInt(input.readLine());
for(int i=0;i<serviceListSize;i++)
{
String name=input.readLine();
String owner=input.readLine();
String available=input.readLine();
String data=input.readLine();
serviceList.add(new Service(name,owner,available,data));
}
}
for(String client: clientList){
for(Service service: serviceList){
System.out.println(service.getService());
}
}
}
我知道有类似ObjectInputStream和ObjectOutputStream的东西,但我想尝试按字段发送服务对象字段,我想知道这里的错误是什么。感谢您提供任何帮助或建议。