java中socket.shutdownOutput()使用后如果还想用输出流怎么办
问题如下:按照我所学的服务器端要想接收到消息就要调用socket.shutdownOutput(),但是这样的话第二次输入用户信息,服务器就收不到了,怎么解决,小弟新手,...
问题如下:按照我所学的服务器端要想接收到消息就要调用socket.shutdownOutput(),但是这样的话第二次输入用户信息,服务器就收不到了,怎么解决,小弟新手,求教。
控制台输出信息
【请输入用户信息:fa
我是客户端,服务器的响应为:对不起,没有该用户,登录失败!
请输入用户信息:fa
java.net.SocketException: Cannot send after socket shutdown: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at loginCheck.LoginClient.main(LoginClient.java:27)】
自己编写的代码如下(省略各种导包代码):
//服务器
public class LoginServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(9900);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String info=null;
String reply1="对不起,没有该用户,登录失败!";
String reply2="欢迎你,登录成功!";
if(!((info=br.readLine())==null)){
System.out.println("我是服务器,客户登录信息为:"+info);
if(!(info.equals("用户1"))){
System.out.println("对不起,没有该用户,已通知客户端登录失败!");
os.write(reply1.getBytes());
}else{
System.out.println("存在该用户,登录成功!");
os.write(reply2.getBytes());
}
}
br.close();
is.close();
os.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端
public class LoginClient {
public static void main(String[] args) {
try {
Scanner input=new Scanner(System.in);
Socket socket=new Socket("localHost",9900);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String reply=null;
do{
System.out.print("请输入用户信息:");
String info=input.next();
os.write(info.getBytes());
socket.shutdownOutput();
if((reply=br.readLine())!=null){
System.out.println("我是客户端,服务器的响应为:"+reply);
}
}while(reply.equals("对不起,没有该用户,登录失败!"));
br.close();
is.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本人菜鸟,大神有解决办法麻烦写详细点,最好是把我的代码改一下发出来 展开
控制台输出信息
【请输入用户信息:fa
我是客户端,服务器的响应为:对不起,没有该用户,登录失败!
请输入用户信息:fa
java.net.SocketException: Cannot send after socket shutdown: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at loginCheck.LoginClient.main(LoginClient.java:27)】
自己编写的代码如下(省略各种导包代码):
//服务器
public class LoginServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(9900);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String info=null;
String reply1="对不起,没有该用户,登录失败!";
String reply2="欢迎你,登录成功!";
if(!((info=br.readLine())==null)){
System.out.println("我是服务器,客户登录信息为:"+info);
if(!(info.equals("用户1"))){
System.out.println("对不起,没有该用户,已通知客户端登录失败!");
os.write(reply1.getBytes());
}else{
System.out.println("存在该用户,登录成功!");
os.write(reply2.getBytes());
}
}
br.close();
is.close();
os.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端
public class LoginClient {
public static void main(String[] args) {
try {
Scanner input=new Scanner(System.in);
Socket socket=new Socket("localHost",9900);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String reply=null;
do{
System.out.print("请输入用户信息:");
String info=input.next();
os.write(info.getBytes());
socket.shutdownOutput();
if((reply=br.readLine())!=null){
System.out.println("我是客户端,服务器的响应为:"+reply);
}
}while(reply.equals("对不起,没有该用户,登录失败!"));
br.close();
is.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本人菜鸟,大神有解决办法麻烦写详细点,最好是把我的代码改一下发出来 展开
2个回答
展开全部
服务器端
package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ReviewServer {
public static void main(String[] args) {
try {
ServerSocket server= new ServerSocket(8900);
Socket socket = server.accept();
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String reply;
String info;
while((info=br.readLine())!=null){
if(info.equals("用户1")){
reply="欢迎你,登录成功!";
System.out.println("我是服务器,客户登录信息为: "+info);
System.out.println("存在该用户,登录成功");
bw.write(reply);
bw.flush();
}else{
reply="对不起,没有该用户,登录失败";
System.out.println("我是服务器,客户登录信息为: "+info);
System.out.println("对不起,没有该用户,已通知客户端登录失败");
bw.write(reply);
bw.flush();
}
}
os.close();
bw.close();
is.close();
br.close();
socket.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端
package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ReviewSocket {
public static void main(String[] args) {
Socket socket;
try {
socket = new Socket("localhost",8900);
OutputStream os = socket.getOutputStream();
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(os));
InputStream is= socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info ="用户2";
bw.write(info);
bw.flush();
socket.shutdownOutput();
String reply ;
while((reply=br.readLine())!=null){
System.out.println("我是客户端服务器的响应: "+reply);
}
os.close();
bw.close();
is.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
亲对比一下 就能发现你的问题了
package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ReviewServer {
public static void main(String[] args) {
try {
ServerSocket server= new ServerSocket(8900);
Socket socket = server.accept();
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String reply;
String info;
while((info=br.readLine())!=null){
if(info.equals("用户1")){
reply="欢迎你,登录成功!";
System.out.println("我是服务器,客户登录信息为: "+info);
System.out.println("存在该用户,登录成功");
bw.write(reply);
bw.flush();
}else{
reply="对不起,没有该用户,登录失败";
System.out.println("我是服务器,客户登录信息为: "+info);
System.out.println("对不起,没有该用户,已通知客户端登录失败");
bw.write(reply);
bw.flush();
}
}
os.close();
bw.close();
is.close();
br.close();
socket.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端
package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ReviewSocket {
public static void main(String[] args) {
Socket socket;
try {
socket = new Socket("localhost",8900);
OutputStream os = socket.getOutputStream();
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(os));
InputStream is= socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info ="用户2";
bw.write(info);
bw.flush();
socket.shutdownOutput();
String reply ;
while((reply=br.readLine())!=null){
System.out.println("我是客户端服务器的响应: "+reply);
}
os.close();
bw.close();
is.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
亲对比一下 就能发现你的问题了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-22
展开全部
都关闭了,那重新连接。
追问
要重新建立socket的话,代码就繁琐了,有别的方法吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询