Java中socket传输对象时,不能传递的问题
求教为什么数据传递不过去。下面是代码客户端publicclassClient2{publicstaticvoidmain(String[]args){try{InetAd...
求教为什么数据传递不过去。下面是代码
客户端
public class Client2 {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
Socket s = new Socket(address, 33333);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// 封装键盘录入
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
Properties reqProp = new Properties();
Properties resProp = new Properties();
String req = null;
String res = null;
System.out.print("请输入...");
// 发送请求到服务器
req = br.readLine();
reqProp.setProperty("request", req);
reqProp.store(out, null);
//写了这句可以传递一次,不写第一次就传递不过去
// s.shutdownOutput();
// 接受服务器的响应
resProp.load(in);
res = resProp.getProperty("response");
if(res == null){
break;
}
System.out.println(res);
if("over".equals(res)){
break;
}
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务器端:
public class Server2 {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(33333);
System.out.println("服务器已启动...");
Socket s = ss.accept();
// 封装键盘录入
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
while (true) {
Properties reqProp = new Properties();
Properties resProp = new Properties();
String req = null;
String res = null;
// 接受客户端的请求
resProp.load(in);
req = resProp.getProperty("request");
if (req == null) {
break;
}
System.out.println(req);
if (req.equals("over")) {
break;
}
// 回写给客户端的响应
res = br.readLine();
reqProp.setProperty("response", res);
reqProp.store(out, null);
//写了这句可以传递一次,不写一次都传递不了
// s.shutdownOutput();
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
客户端
public class Client2 {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
Socket s = new Socket(address, 33333);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// 封装键盘录入
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
Properties reqProp = new Properties();
Properties resProp = new Properties();
String req = null;
String res = null;
System.out.print("请输入...");
// 发送请求到服务器
req = br.readLine();
reqProp.setProperty("request", req);
reqProp.store(out, null);
//写了这句可以传递一次,不写第一次就传递不过去
// s.shutdownOutput();
// 接受服务器的响应
resProp.load(in);
res = resProp.getProperty("response");
if(res == null){
break;
}
System.out.println(res);
if("over".equals(res)){
break;
}
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务器端:
public class Server2 {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(33333);
System.out.println("服务器已启动...");
Socket s = ss.accept();
// 封装键盘录入
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
while (true) {
Properties reqProp = new Properties();
Properties resProp = new Properties();
String req = null;
String res = null;
// 接受客户端的请求
resProp.load(in);
req = resProp.getProperty("request");
if (req == null) {
break;
}
System.out.println(req);
if (req.equals("over")) {
break;
}
// 回写给客户端的响应
res = br.readLine();
reqProp.setProperty("response", res);
reqProp.store(out, null);
//写了这句可以传递一次,不写一次都传递不了
// s.shutdownOutput();
}
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询