基本的echo服务器,客户端 - 服务器关系 [英] Basic echo server, client-server relationship

查看:238
本文介绍了基本的echo服务器,客户端 - 服务器关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我的项目,我不得不写一个客户端类和一个简单的服务器类,将由客户端写的ECHO消息。

So as my project I had to write a client class and a simple server class that will ECHO message written by the client.

由于某种原因,我要么得到我/ O异常或套接字关闭的异常循环。

For some reason I either get I/O exception or a loop of exceptions that socket's closed.

我真的很感谢一些帮助,因为我努力的这个程序2天直接,找不到任何解决方案。

I would really appreciate some help, because I struggle with this program for 2 days straight and cannot find any solutions.

我的服务器类:

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

我的客户端类: / p>

My client 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


推荐答案

我已经分享了一个很好的聊天程序,有一个服务器使用TCP协议与多个客户端通信。

I have already shared a nice Chat Program having one server that is communicating with multiple clients using TCP protocol.

请看看具有多方通信的Java服务器。

它可能会帮助你。

在您的代码中,您在与单个客户端连接后关闭了套接字。

In you code you have closed the socket after connected with single client.

这是修复:

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

这篇关于基本的echo服务器,客户端 - 服务器关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆