【问题标题】:TCP can't receive text messages after sending a file from server in Java用Java从服务器发送文件后TCP无法接收文本消息
【发布时间】:2019-02-09 05:07:31
【问题描述】:

我是套接字编程的新手,我有一个我无法理解的问题。我正在尝试将文件从服务器发送到客户端。之后,服务器和客户端开始聊天,直到客户端说“再见”。当我启动程序时,它运行良好,直到客户端发送“再见”。消息并关闭其套接字。它给出了“无法获得连接到:localhost”的 I/O 异常。

当我尝试调试以查看发生了什么时,即使文件传输完成,它也会保留在 while(at line 38) 语句中。文件传递正确。

服务器代码:

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

public class ServerTCP extends Thread
{ 
    protected Socket clientSocket;

public static void main(String[] args) throws IOException 
{ 
    ServerSocket serverSocket = null; 

    try { 
        serverSocket = new ServerSocket(10008); 
        System.out.println ("Connection Socket Created");
        try { 
            while (true)
            {
                System.out.println ("Waiting for Connection");
                new ServerTCP (serverSocket.accept()); 
            }
        } 
        catch (IOException e) 
        { 
            System.err.println("Accept failed."); 
            System.exit(1); 
        } 
    } 
    catch (IOException e) 
    { 
        System.err.println("Could not listen on port: 10008."); 
        System.exit(1); 
    } 
    finally
    {
        try {
            serverSocket.close(); 
        }
        catch (IOException e)
        { 
            System.err.println("Could not close port: 10008."); 
            System.exit(1); 
        } 
    }
}

private ServerTCP (Socket clientSoc)
{
    clientSocket = clientSoc;
    start();
}

public void run()
{
    System.out.println ("New Communication Thread Started");
    // İlave kısım
    DataInputStream dataIn = null;
    DataOutputStream dataOut = null;

    try {
        //echoSocket = new Socket(serverHostname, 10008);
        dataIn = new DataInputStream(clientSocket.getInputStream());
        dataOut = new DataOutputStream(clientSocket.getOutputStream());

        File file = new File("tmp.txt");
        byte[] contents = new byte[1024];

        FileInputStream fis = new FileInputStream(file);

        int count = 0;
        while( (count =  fis.read(contents)) > 0 ){
            dataOut.write(contents, 0, count);
            dataOut.flush();
        }
        contents[0] = 0;
        count = 1;
        dataOut.write(contents, 0, count);
        dataOut.flush();

        Scanner sc = new Scanner(System.in);
        String clientMessage = null;
        while( !(clientMessage = dataIn.readUTF()).equals("Bye.") )
        {
            System.out.println(clientMessage);
            dataOut.writeUTF(sc.nextLine());
        }

        sc.close();
        fis.close();
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                + "the connection to: ");
        System.exit(1);
    } finally {
        try {
            if (dataOut != null)
                dataOut.close();
            if(dataIn != null)
                dataIn.close();
            clientSocket.close(); 
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



} 
}

客户代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

 public class Client {
public static void main(String[] args) throws IOException {

    String serverHostname = new String ("localhost");

    if (args.length > 0)
        serverHostname = args[0];
    System.out.println ("Attemping to connect to host " +
            serverHostname + " on port 10008.");

    Socket echoSocket = null;
    DataInputStream in = null;
    DataOutputStream dataOut= null;

    try {
        echoSocket = new Socket(serverHostname, 10008);
        in = new DataInputStream(echoSocket.getInputStream());
        dataOut = new DataOutputStream(echoSocket.getOutputStream());

        String clientMessage = "tmp";
        File file = new File(clientMessage + "_got" + ".txt");
        System.out.println(clientMessage + "_got" + ".txt created.");

        FileOutputStream foStream = new FileOutputStream(file);

        byte[] buff = new byte[1024];

        int count = 0;
        while ((count = in.read(buff)) > 0) {
            if(buff[0] == 0)
                break;
            foStream.write(buff, 0, count);
            foStream.flush();
        }

        Scanner keyboard = new Scanner(System.in);
        String msg = "";
        while(true){
            msg = keyboard.next();
            System.out.println("Client: " + msg);
            dataOut.writeUTF(msg);
            System.out.println("Server: " + in.readUTF());
            if( msg.equalsIgnoreCase("Bye.") )
                break;
        }

        System.out.println("asds");
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                + "the connection to: " + serverHostname);
        System.exit(1);
    } finally {
        try {
            if (dataOut != null)dataOut.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    in.close();
    echoSocket.close();
}

}

编辑

我的例外是:

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
    at java.io.DataInputStream.readUTF(DataInputStream.java:572)
    at java.io.DataInputStream.readUTF(DataInputStream.java:547)
    at Client.main(Client.java:51)
Couldn't get I/O for the connection to: localhost

【问题讨论】:

  • '它给出了“无法获得 I/O 以连接到:localhost”异常。'不,它没有。这是您自己设计的愚蠢信息,或者是从古代 Sun 教程中复制的。打印异常附带的消息。那会告诉你发生了什么。当你遇到异常时,不要自己编造消息。提供了一个更好的,这是针对具体情况的。
  • 并且不要在循环内冲洗。
  • 这是五年前的问题。这不是很明显,它是一个古老的:)

标签: java sockets tcp file-transfer datainputstream


【解决方案1】:

您已在“while”循环中读取输入直到流结束,然后您尝试使用 readUTF() 读取更多输入。没有了。您的应用程序协议有问题。需要在文件之前发送文件长度,读取发送文件时只读取这么多字节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多