简体   繁体   English

我想停止从Socket中的数据输入流中读取

[英]I want to stop reading from data input stream in Socket

I wrote a program that sends messages between two computer. 我编写了一个在两台计算机之间发送消息的程序。 Sending messages is fine. 发送消息很好。 But when i call exit() method from MessageClient class, it throws 'EOF exception' from ServerThread and 'SocketException: Socket Closed' from ClientThread. 但是,当我从MessageClient类调用exit()方法时,它会从ServerThread中引发“ EOF异常”,并从ClientThread中引发“ SocketException:套接字关闭”。 'while loop' in both Threads are infinite and I want to stop when i call exit() method. 两个线程中的“ while循环”是无限的,我想在调用exit()方法时停止。 How do i fix this? 我该如何解决?

Main: 主要:

package team.kha.lan_message;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner scan;
    String cmd;
    while(true){
        System.out.print(">> ");
        scan = new Scanner(System.in);
        cmd = scan.nextLine();

        if(cmd.equals("server")){
            new MessageServer();
            scan.close();
            break;  
        }
        else if(cmd.equals("client")){
            new MessageClient();
            scan.close();
            break;
        }
        else if(cmd.equals("exit")){
            scan.close();
            break;
        }
        else{
            System.out.println("Wrong Usage!");
            System.out.println("\t Usage: <server> - start [Address] [Port]");
            System.out.println("\t Usage: <client> - connect [Address] [Port]");
            System.out.println("\t                 - send [Message]");
            System.out.println("\t                 - exit\n");
            }
        }
    }
}

MessageServer: MessageServer:

 package team.kha.lan_message;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class MessageServer {

Scanner scan;
String cmd;
String[] params;
String ipAddress;
int port;
DataInputStream din;
DataOutputStream dout;
Socket s;
ServerSocket ss;
boolean started = false;

MessageServer(){
    while(true){
        System.out.print("(server):");
        scan = new Scanner(System.in);
        cmd = scan.nextLine();

        if(!cmd.startsWith(" ")){
            params = cmd.split(" ");

            if(params[0].equals("start") && params.length == 3 && started == false){
                startServer();
            }
            else if(params[0].equals("send") && params.length >= 2 && started == true){
                sendMessage();
            }
            else if(params[0].equals("exit") && params.length == 1){
                exit();
                break;
            }
        }
    }
}

private void exit() {
    try {
        if(started == true){
            s.close();
            ss.close();
        }
        scan.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void sendMessage() {
    String msgout = "";

    for(int i = 1 ; i < params.length ;i++){
        if(i == params.length-1){
            msgout += params[i];
        }
        else{
            msgout += params[i] + " ";
        }
    }
    try {
        dout.writeUTF(msgout);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void startServer() {
    ipAddress = params[1];
    port = Integer.parseInt(params[2]);

    try {
        ss = new ServerSocket();
        ss.bind(new InetSocketAddress(ipAddress , port));
        s = ss.accept();
        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());

        new ServerThread(din);

        started = true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

ServerThread: ServerThread:

package team.kha.lan_message;

import java.io.DataInputStream;
import java.io.IOException;

public class ServerThread extends Thread {

private DataInputStream din;

ServerThread(DataInputStream din){
    this.din = din;
    start();
}


@Override
public void run() {
    while(true){
        try {
            System.out.print("\n(client):"+din.readUTF()+ "\n(server):");
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }  
        }
    }



}

MessageClient: MessageClient:

package team.kha.lan_message;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class MessageClient {

Scanner scan;
String cmd;
String[] params;
String ipAddress;
int port;
DataInputStream din;
DataOutputStream dout;
Socket s;
boolean connected = false;

MessageClient(){

    while(true){
        System.out.print("(client):");
        scan = new Scanner(System.in);
        cmd = scan.nextLine();

        if(!cmd.startsWith(" ")){
            params = cmd.split(" ");

            if(params[0].equals("connect") && params.length == 3 && connected == false){
                connectServer();
            }
            else if(params[0].equals("send") && params.length >= 2 && connected == true){
                sendMessage();
            }
            else if(params[0].equals("exit") && params.length == 1){
                exit();
                break;
            }
        }
    }
}

private void exit() {
    try {
        if(connected == true){
            s.close();
        }
        scan.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void sendMessage() {
    String msgout = "";
    for(int i = 1 ; i < params.length ;i++){
        if(i == params.length-1){
            msgout += params[i];
        }
        else{
            msgout += params[i] + " ";
        }
    }
    try {
        dout.writeUTF(msgout);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void connectServer() {
    ipAddress = params[1];
    port = Integer.parseInt(params[2]);

    try {
        s = new Socket(ipAddress , port);

        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());

        new ClientThread(din);
        connected = true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

ClientThread: ClientThread:

package team.kha.lan_message;

import java.io.DataInputStream;
import java.io.IOException;

public class ClientThread extends Thread{

private DataInputStream din;

ClientThread(DataInputStream din){
    this.din = din;
    start();
}


@Override
public void run() {
    while(true){
        try {
            System.out.print("\n(server):"+din.readUTF()+ "\n(client):");
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    }

}

There is nothing to fix. 没有要修复的东西。 That's what is supposed to happen. 那是应该发生的。 You read past end of stream => you get EOFException . 阅读流的末尾=>会得到EOFException Just catch it separately, and break . 只需单独抓住它,然后break

Catching and ignoring IOExceptions inside a loop is almost always incorrect. 在循环内捕获和忽略IOExceptions几乎总是不正确的。 The only one that isn't fatal to the connection is SocketTimeoutException . 唯一不会致命的连接是SocketTimeoutException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM