基本echo服务器、客户机-服务器关系

vfh0ocws  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(323)

因此,作为我的项目,我必须编写一个客户机类和一个简单的服务器类来回送客户机编写的消息。
出于某种原因,我要么得到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
cigdeys3

cigdeys31#

工作方式:
服务器打开 ServerSocket 并接受这方面的联系 ServerSocket .
每个连接(新客户机)[应该在单独的线程中处理]都是一个单独的新客户机 Socket 与自己的i/o通道连接。
对于每一个新的 Socket 您应该打开i/o通道一次,并保持它们打开,直到客户端断开连接。
如果你遵循这个逻辑,一切都会成功的。

fiei3ece

fiei3ece2#

我已经分享了一个很好的聊天程序,它有一个服务器,使用tcp协议与多个客户端进行通信。
请看一看带有多客户端通信的java服务器。
它可能会帮助你。同时我在查你的密码。
在您的代码中,在与单个客户机连接之后,您已经关闭了套接字。
解决方法如下:

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

相关问题