我正在尝试使用客户端登录创建一个简单的客户端 - 服务器聊天应用程序。我已经上网几天了,正在寻找关于我目前遇到的问题的答案。我已经尝试了所有建议和“解决方案”,但我仍然无法让它正常运行。
问题出在溪流上。如果在将字符串发送到服务器后关闭客户端输出流,则服务器会成功读取该字符串。但是,由于套接字已关闭,服务器无法发回响应。如果我不关闭客户端输出流,则服务器无法读取客户端发送的任何数据。我试过摆弄我的代码,但无济于事。
这是我的客户代码:
import java.io.*;
import java.net.*;
import java.net.Socket;
public class Client {
public static BufferedReader bf;
public static PrintWriter pw;
public static Socket s;
public static ClientLogin login = new ClientLogin();
public String me;
public static boolean connect(String ip, int port){
try {
s = new Socket(ip, port);
pw = new PrintWriter(s.getOutputStream(), true);
bf = new BufferedReader(new InputStreamReader(s.getInputStream()));
new ServerListener().start();
}catch (ConnectException ce){
return false;
} catch (UnknownHostException ex) {
return false;
} catch (IOException ex) {
return false;
}
return true;
}
public void send(String msg){
try{
if(msg.length()>0){
pw.write(msg);
System.out.println("client message sent.. " + msg);
}
}catch(Exception e){
System.out.println("Error in client sender: " + e);
}
}
}
class ServerListener extends Thread{
@Override
public void run(){
String res = "";
String input;
try{
System.out.println("client listening....");
input = Client.bf.readLine();
System.out.println(input);
if(input.startsWith("LOGIN")){
ClientLogin cl = new ClientLogin();
cl.login(input);
}else{
ClientLogin.errorLabel.setText(input);
}
}catch(Exception e){
System.out.println("Error in Client listener: " + e); e.printStackTrace();
}
}
}
服务器代码:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.LinkedList;
public class ChatServer {
public static final LinkedList<Socket> sockets = new LinkedList<>();
private static Dbdriver db;
private static int users;
static clientThread cl = new clientThread();
public static InetAddress ia;
public ChatServer(){
db = new Dbdriver(); //database connection
db.getClass();
try{
users = db.countUsers();
}catch(Exception e){ e.printStackTrace(); }
}
//START SERVER
public static void startServer(){
clientThread s = new clientThread();
s.start();
}
public static void stopServer(){
try {
synchronized(ChatServer.sockets){
for(Socket ss:sockets){
if(ss.isConnected()){
MessengerEngine.pw.close();
ss.close();
}
}
sockets.clear();
}
cl.theServer.close();
} catch (IOException ex) {ex.printStackTrace(); }
}
}
class clientThread extends Thread {
public static ServerSocket theServer;
@Override
public void run() {
int port = 8888;
try{
theServer = new ServerSocket(port);
while(true){
MessengerEngine engine = new MessengerEngine(theServer.accept());
engine.start();
}
}catch(SocketException se){
}catch(Exception e){
}
}
}
class MessengerEngine extends Thread{
public static Dbdriver theDriver;
private Socket theSocket;
private BufferedReader bf;
private PrintWriter pw;
public String ipadd;
public MessengerEngine(Socket s){
theSocket = s;
synchronized(ChatServer.sockets){
ChatServer.sockets.add(theSocket);
}
try{
bf = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
pw = new PrintWriter(theSocket.getOutputStream(), true);
ipadd = s.getInetAddress().getHostAddress();
AdminServer.serverLog.setText(AdminServer.serverLog.getText() + ipadd +" has connected." +"\n");
}catch(Exception e){
System.out.println("Socket.accept error");
}
}
@Override
public void run(){
String res = ""; //server response
try{
String input = bf.readLine();
System.out.println("String from client: " + input);
String[] inputs= input.split("-");
if(input.startsWith("LOGIN")){ //LOGIN - "LOGIN-name-pass-ip"
Dbdriver db = new Dbdriver();
res = db.userLogin(inputs[1], inputs[2], inputs[3]);
System.out.println("Client logging in sent: " + inputs[1] + ", " + inputs[2] +", " + inputs[3]);
send(res);
}
}catch(IOException e){
e.printStackTrace();
}
}
public void send(String msg){
System.out.println("Server trying to send: "+ msg);
pw.write(msg);
System.out.println("server sent message..");
}
}
来自客户端的所有数据都来自文本字段(用户名,密码)。我希望服务器通过从数据库中检查来验证客户端登录。从文本字段收集并验证登录工作完美。我唯一的问题是客户端输入流..我想。帮助一个菜鸟编程学生?请。
服务器抛出异常:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at server.MessengerEngine.run(ChatServer.java:102)
ChatServer.java:102&gt;&gt; String input = bf.readLine();
答案 0 :(得分:-1)
在通信完成之前,您不得关闭流。它总是会导致关闭套接字。您需要做的是在向数据输出流写入内容后使用dos.flush()
以使服务器获取信息并在完成通信后关闭dos。