因此,作为我的项目,我必须编写一个客户端类和一个简单的服务器类,它将由客户端编写ECHO消息。
出于某种原因,我要么得到I / O异常,要么关闭套接字的异常循环。
我真的很感激一些帮助,因为我连续两天都在努力解决这个问题而无法找到任何解决方案。
我的服务器类:
import java.io.*;
import java.net.*;
import java.util.regex.Pattern;
public class SimpleServer {
private ServerSocket ss = null;
private BufferedReader in = null;
private PrintWriter out = null;
public SimpleServer(String host, int port) {
try {
InetSocketAddress isa = new InetSocketAddress(host, port);
ss = new ServerSocket();
ss.bind(isa);
} catch (IOException exc) {
System.out.println("Error in constructor");
System.exit(1);
}
System.out.println("Server started.");
System.out.println("on port: " + ss.getLocalPort());
System.out.println("bind address: " + ss.getInetAddress());
serviceConnections();
}//constructor
private void serviceConnections() {
boolean serverRunning = true;
while(serverRunning) {
try {
Socket conn = ss.accept();
System.out.println("Connection established!");
serviceRequests(conn);
} catch (Exception exc) {
exc.printStackTrace();
}
try { ss.close(); } catch (Exception exc) {}
}
}//serviceConnections
private void serviceRequests(Socket connection) throws IOException {
try {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out = new PrintWriter(connection.getOutputStream(), true);
String line = in.readLine();
out.println(line);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
try {
in.close();
out.close();
connection.close();
connection = null;
} catch(Exception exc) {}
}
}//serviceReq
public static void main(String[] args) {
String host = "localhost";
int port = 2401;
new SimpleServer(host,port);
}//main
}//class
我的客户端类:
import java.net.*;
import java.io.*;
public class SimpleServerClient {
private Socket sock = null;
private PrintWriter out = null;
private BufferedReader in = null;
public SimpleServerClient (String host, int port) {
try {
sock = new Socket(host, port);
out = new PrintWriter(sock.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
// System.out.println("Connected to the: " + sock.getInetAddress() );
makeRequest("ECHO Howdy boy");
} catch (UnknownHostException e) {
System.err.println("Unknown host: "+host);
System.exit(2);
} catch (IOException e) {
System.err.println("I/O error for");
System.exit(3);
} catch (Exception exc) {
exc.printStackTrace();
System.exit(4);
}
}//constructor
private void makeRequest(String req) throws IOException {
System.out.println("Request: " + req);
out.println(req);
String resp = in.readLine();
System.out.println(resp);
disconnect();
}//method
public void disconnect() {
try {
out.close();
sock.close();
in.close();
} catch(IOException exc) {
System.out.println("Error while closing");
System.exit(5);
}
}//method
public static void main(String[] args) {
new SimpleServerClient("localhost", 2401);
}//main
}//class
答案 0 :(得分:0)
它(应该)的工作方式:
服务器打开ServerSocket
并接受ServerSocket
上的连接
每个连接(新客户端)[应在单独的线程中处理]是与其自己的I / O通道的单独的新Socket
连接。
对于每个新的Socket
,您应该打开一次I / O通道并保持打开状态,直到客户端断开连接。
如果遵循这个逻辑,一切都会奏效。
答案 1 :(得分:0)
我已经分享了一个很好的聊天程序,其中有一台服务器使用TCP协议与多个客户端进行通信。
请查看Java Server with Multiclient communication.
它可能对你有所帮助。与此同时,我正在寻找你的代码。
在您的代码中,您在与单个客户端连接后关闭了套接字。
以下是修复:
private void serviceConnections() {
boolean serverRunning = true;
while (serverRunning) {
try {
Socket conn = ss.accept();
System.out.println("Connection established!");
serviceRequests(conn);
} catch (Exception exc) {
exc.printStackTrace();
}
}
try {
ss.close();
} catch (Exception exc) {
}
}// serviceConnections