【新人求助】 java socket 通信,客户端无法读取服务器端写回的消息,求解
这是客户端的代码:
这是服务器端的:
求解.
完整代码:(其他先可以不看,现在是book()这一块,我的其他两个,问题也是一样,都是服务器端能收到指令,但写回客户端的时候客户端读不了)
package com.BrokerServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.Constants.HotelConstants;
import com.DAO.*;
public class BrokerServerHOPP {
protected Socket mySocket;
protected BufferedReader reader1;
protected PrintStream writer1;
public String book (String str) throws Exception{
InetAddress address = InetAddress.getByName("localhost");
mySocket = null;
InputStream inStream = null;
OutputStream outStream = null;
int port = getbookPort(str);
mySocket = new Socket(address, port);
inStream = mySocket.getInputStream();
outStream = mySocket.getOutputStream();
reader1 = new BufferedReader(new InputStreamReader(inStream));
writer1 = new PrintStream(outStream);
writer1.print(str);
writer1.flush();
writer1.close();
String respond = "";
while (true) {
try {
respond = reader1.readLine();
System.out.println(7);
return respond;
} catch (IOException e) {
System.out.println(8);
return "false";
}
}
}
private int getbookPort(String str) throws Exception {
// TODO Auto-generated method stub
return dbmanage.getbookPort(str);
}
public String order (String str) throws Exception{
//InetAddress address = BrokerServer.address;
InetAddress address = InetAddress.getByName("localhost");
mySocket = null;
InputStream inStream = null;
OutputStream outStream = null;
int port = getorderPort(str);
mySocket = new Socket(address, port);
inStream = mySocket.getInputStream();
outStream = mySocket.getOutputStream();
reader1 = new BufferedReader(new InputStreamReader(inStream));
writer1 = new PrintStream(outStream);
writer1.print(str);
writer1.flush();
String respond = "";
while (true) {
try {
respond = reader1.readLine();
return respond;
} catch (IOException e) {
return "false";
}
}
}
private int getorderPort(String str) throws Exception {
// TODO Auto-generated method stub
return dbmanage.getorderPort(str);
}
public String delete (String orderid) throws Exception{
//InetAddress address = BrokerServer.address;
InetAddress address = InetAddress.getByName("localhost");
mySocket = null;
InputStream inStream = null;
OutputStream outStream = null;
int port = getdeletePort(orderid);
mySocket = new Socket(address, port);
inStream = mySocket.getInputStream();
outStream = mySocket.getOutputStream();
reader1 = new BufferedReader(new InputStreamReader(inStream));
writer1 = new PrintStream(outStream);
writer1.print(orderid);
writer1.flush();
String respond = "";
while (true) {
try {
respond = reader1.readLine();
return respond;
} catch (IOException e) {
return "false";
}
}
}
private int getdeletePort(String orderid) throws Exception {
// TODO Auto-generated method stub
return dbmanage.getdeletePort(orderid);
}
DBManage dbmanage = new DBManage();
public boolean logIn(String str) throws Exception {
return dbmanage.logIn(str);
}
public boolean register (String str) throws Exception {
return dbmanage.register(str);
}
public String query () throws Exception{
return dbmanage.query();
}
public String queryhotel (String line) throws Exception{
return dbmanage.queryhotel(line);
}
public String manage (String str) throws Exception{
return dbmanage.manage(str);
}
}
package com.HotelServer;
import java.io.*;
import java.net.*;
import com.Constants.*;
public class HotelServer {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: Client address");
System.exit(1);
}
ServerSocket s = null;
if(args[0].equals("Melbourne") && args[1].equals("Hilton")){
try {
s = new ServerSocket(18892);
System.out.println("0");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Melbourne Hilton Server is Running...");
} else if (args[0].equals("Sydney") && args[1].equals("Chevron")){
try {
s = new ServerSocket(18893);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Sydney Chevron Server is Running...");
} else if (args[0].equals("Perth") && args[1].equals("Regent")){
try {
s = new ServerSocket(18894);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Perth Regent Server is Running...");
} else if (args[0].equals("Melbourne") && args[1].equals("Chevron")){
try {
s = new ServerSocket(18895);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Melbourne Chevron Server is Running...");
} else if (args[0].equals("Melbourne") && args[1].equals("Regent")){
try {
s = new ServerSocket(18896);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Melbourne Regent Server is Running...");
}
while (true) {
Socket incoming = null;
try {
incoming = s.accept();
} catch (IOException e) {
System.out.println(e);
continue;
}
new SocketHandler(incoming).start();
}
}
}
class SocketHandler extends Thread {
Socket incoming;
HotelServerHOPP fileServer = new HotelServerHOPP();
BufferedReader reader;
PrintStream writer;
SocketHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
try {
writer = new PrintStream(incoming.getOutputStream());
reader = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
System.out.println("Received request: " + line);
if (line.startsWith(HotelConstants.BOOK)) {
try {
book(losePrefix(line, HotelConstants.BOOK));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (line.startsWith(HotelConstants.ORDER)) {
try {
order(losePrefix(line, HotelConstants.ORDER));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (line.startsWith(HotelConstants.DELETE)) {
try {
delete(losePrefix(line, HotelConstants.DELETE));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (line.startsWith(HotelConstants.QUIT)) {
break;
} else {
writer.print(HotelConstants.ERROR
+ HotelConstants.CR_LF);
}
}
//incoming.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Given that the string starts with the prefix, get rid of the prefix and any
* following whitespace
*/
public String losePrefix(String str, String prefix) {
int index = prefix.length();
String ret = str.substring(index).trim();
return ret;
}
public void book (String str) throws Exception {
String respond = fileServer.book(str);
System.out.println(respond);
writer.print(respond + HotelConstants.CR_LF);
System.out.println(1);
writer.flush();
writer.close();
}
public void order (String str) throws Exception {
String respond = fileServer.order(str);
System.out.println(respond);
writer.print(respond + HotelConstants.CR_LF);
writer.flush();
}
public void delete (String str) throws Exception {
String respond = fileServer.delete(str);
System.out.println(respond);
writer.print(respond + HotelConstants.CR_LF);
writer.flush();
}
}
------解决思路----------------------
writer1.close();要放到while循环后面,你输出流关闭了,服务端就不等客户端了,执行完read就关闭了,这个时候你要从服务端读,就会报socket closed的异常,其实你可以在catch里面把异常信息打印出来