0

i'm trying to make a server client program in java. I'm trying to run on same system. It sends a text file from client to server including names etc. But when the server gets the file it just prints "[]" characters. Does that mean null or does that mean corrupted data?

Here is the server;

package temel;

import java.io.File;
import java.io.InputStream;
import java.net.Socket;
import java.net.ServerSocket;

public class VeriAlma {

  private static int port;

  private static Socket socket;
  public VeriAlma(int port)
  {
      VeriAlma.port=port;
  }
  public static void veriAl() {
    try {
        ServerSocket listener = new ServerSocket(port);

                socket = listener.accept(); 

    }
    catch (java.lang.Exception ex) {
      ex.printStackTrace(System.out);
    }
  }

  public String run() {
    try {
      InputStream in = socket.getInputStream();


        String file_name = "x.txt";

        File file=new File(file_name);

        ByteStream.toFile(in, file);
      return file_name;
    }
    catch (java.lang.Exception ex) {
      ex.printStackTrace(System.out);
      return "asdasd";
    }
  }
  public void setPort(int port)
  {
      VeriAlma.port=port;

  }
}

Here is the client

package temel;
import java.io.*;
import java.net.*;
import java.lang.*;

import java.io.OutputStream;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;

import java.net.Socket;

public class VeriGonderme {

      // host and port of receiver
      private static int    port;
      private static String host;

      public VeriGonderme(int port,String host)
      {
          this.port=port;
          this.host=host;
      }
      public void veriGonder(String dosyaIsmi) {
        try {
          Socket       socket = new Socket(host, port);
          OutputStream os     = socket.getOutputStream();


          ByteStream.toStream(os, 1);


            ByteStream.toStream(os, dosyaIsmi);
            ByteStream.toStream(os, new File(dosyaIsmi));
          }catch (Exception ex) {
          ex.printStackTrace();
        }
        }
      public void setPort(int port)
      {
          this.port=port;

      }
      public void setHost(String host)
      {
          this.host=host;

      }

      }

host is "localhost" port is 4444

This is the original code i modified it a little bit

4

1 回答 1

0

但是当服务器获取文件时,它只打印“[]”字符。

我希望看到一些这样的字符,因为您将值1作为 4 字节二进制文件发送。即\0\0\0\u0001 如果你删除它并且只发送一次文件,它可能会像你预期的那样做。

于 2012-08-09T15:14:15.617 回答