简体   繁体   English

Java套接字缓冲区和字符串

[英]java socket buffer and string

For a homework i try to send a file and some parameter corresponding to this file. 对于一门功课我尝试发送一个文件,对应于该文件中的一些参数。 So, i send my file and after my string. 所以,我发送我的文件,并在我的字符串之后。 The problem is my paramater go to my file and not in my variable. 问题是我的参数进入我的文件,而不是我的变量。 I understand the problem, my loop while continue to write in the file as long as he receives something, but i want to stop it and have my parameter outside from my file. 我理解了这个问题,只要他收到一些东西,我的循环就继续写入文件,但是我想停止它,并让我的参数不在文件外。

Here my code client: 这是我的代码客户端:

public static void transfert(InputStream in, OutputStream out) throws IOException{
        PrintWriter printOut;
        byte buf[] = new byte[1024];   
        int n;
        while((n=in.read(buf))!=-1)
            out.write(buf,0,n);


        printOut = new PrintWriter(out);
        printOut.println("add");
        System.out.println("envoie !!!");
        printOut.println("1");
        printOut.println("3");
       // out.write(getBytes("add"),0,0);
        printOut.flush();
    }

and here my server code : 这是我的服务器代码:

public static void transfert(InputStream in, OutputStream out, boolean closeOnExit) throws IOException
    {        
        byte buf[] = new byte[1024];

        int n;
        while((n=in.read(buf))!=-1)
            out.write(buf,0,n);

        buffIn = new BufferedReader (new InputStreamReader(in));
        String nom_methode = buffIn.readLine();
        String param1 = buffIn.readLine();
        String param2 = buffIn.readLine();
        System.out.println("methode:"+nom_methode+"param1:"+param1+"param2:"+param2);

        if (closeOnExit)
        {
            in.close();
            out.close();
        }     
    }

edit: I still miss something, now i have an error with my thread, i think the problem is from my loop wich read my file in input. 编辑:我仍然想念一些东西,现在我的线程有错误,我认为问题出在我的循环中,它在输入中读取了我的文件。 Moreover, actually param still go in my file and not in my param... Why the loop dont stop after EOF ? 而且,实际上参数仍然存在于我的文件中而不是我的参数中……为什么循环在EOF之后没有停止?

error: 错误:

Exception in thread "Thread-0" java.lang.IndexOutOfBoundsException
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.java:318)
    at serveurthread.AcceptClient.transfert(AcceptClient.java:45)
    at serveurthread.AcceptClient.run(AcceptClient.java:84)
    at java.lang.Thread.run(Thread.java:722)

client:
 public static void transfert(InputStream in, OutputStream out) throws IOException{
        PrintWriter printOut;
        printOut = new PrintWriter(out);
        byte buf[] = new byte[1024];   
        int n;
        while((n=in.read(buf))!=-1)
            out.write(buf,0,n);


        printOut.print('\u0004');
        printOut.flush();
        printOut.println("add");   
        System.out.println("envoie !!!");
        printOut.println("1");
        printOut.println("3");
       // out.write(getBytes("add"),0,0);
        printOut.flush();
    }

server: 服务器:

public static void transfert(InputStream in, OutputStream out, boolean closeOnExit) throws IOException
    {        
        byte buf[] = new byte[1024];

    int n;
    while((n=in.read(buf))!= (int)'\u0004'){
        out.write(buf,0,n);
    }

    buffIn = new BufferedReader (new InputStreamReader(in));
    String nom_methode = buffIn.readLine();
    String param1 = buffIn.readLine();
    String param2 = buffIn.readLine();
    System.out.println("methode:"+nom_methode+"param1:"+param1+"param2:"+param2);

    if (closeOnExit)
    {
        in.close();
        out.close();
    }     
}

You are going to need some kind of marker in your stream that signifies the end of the file. 您将需要在流中使用某种标记来指示文件结束。 I suggest '\' which is the EOF character. 我建议使用'\'这是EOF字符。 Basically, write this character after writing the file, then adjust the loop in the server like this: 基本上,在写入文件后写入此字符,然后像这样调整服务器中的循环:

while((n=in.read(buf))!=(int)'\u0004')
    out.write(buf,0,n);

Now, the server stops reading the file when necessary, then can get around to reading the string. 现在,服务器在必要时停止读取文件,然后可以四处读取字符串。

Also, read returns a count of bytes read, not a char. 同样, read返回读取的字节数,而不是char。 I would pass one byte at a time through, by declaring bytes instead of byte arrays. 通过声明字节而不是字节数组,我一次可以传递一个字节。

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

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