+ -
当前位置:首页 → 问答吧 → 使用socket传送图片, 档案大小总是会漏掉

使用socket传送图片, 档案大小总是会漏掉

时间:2009-11-17

来源:互联网

这是从良葛格socket范例程式来练习的
不过很奇怪档案大小总是会漏掉几个byte
照成图片打不开, 有时会漏有时不会, 不过漏掉的次数较多
看了很久, 好像是server那没接收完全,确切的原因还没找到
请各位指点一下
谢谢

client 程式码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.*;
import java.net.*;
 
public class Client {
  public static void main(String[] args) {
    try {
      System.out.println("简易档案传送...");
      String remoteHost = "127.0.0.1";
      int port = 222;
      File file = new File("c://photozip.jpg");
      System.out.printf("远端hh主机: %s%n", remoteHost);
      System.out.printf("远端主机连接埠: %d%n", port);
      System.out.printf("传送档案: %s%n", file.getName());
      Socket skt = new Socket(remoteHost, port);
      System.out.println("连线成功!尝试传送档案....");
      PrintStream printStream = new PrintStream(skt.getOutputStream());
      printStream.println(file.getName());
      System.out.print("OK! 传送档案....");
      BufferedInputStream inputStream = new BufferedInputStream(
          new FileInputStream(file));
      int readin=1;
      int readinx=0;
      while ((readin = inputStream.read()) != -1) {        
        printStream.write(readin);
        readinx=readinx+readin;
        Thread.yield();
      }
      System.out.println(readinx);
      printStream.flush();
      printStream.close();
      inputStream.close();
      skt.close();
      System.out.println("\n档案传送完毕!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 

server 程式码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
import java.net.*;
 
public class Server {
  public static void main(String[] args) {
    try {
      int port = 222;
      System.out.println("简易档案接收...");
      System.out.printf("将接收档案於连接埠: %d%n", port);
      ServerSocket serverSkt = new ServerSocket(port);
      while (true) {
        System.out.println("倾听中....");
        Socket clientSkt = serverSkt.accept();
        System.out.printf("与 %s 建立连线%n", clientSkt.getInetAddress()
            .toString());
        // 取得档案名称
        String fileName = "d://"+
                        new BufferedReader(new InputStreamReader(clientSkt.getInputStream())).readLine();
        
        BufferedInputStream inputStream = new BufferedInputStream(clientSkt.getInputStream());
        BufferedOutputStream outputStream = new BufferedOutputStream(
            new FileOutputStream(fileName));
        int readin;
        int readinx=0;
        while ((readin = inputStream.read()) != -1) {
          outputStream.write(readin);
          readinx=readinx+readin;
          Thread.yield();
        }
        System.out.println(readinx);
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        clientSkt.close();
        System.out.println("\n档案接收完毕!");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 

作者: edisondata3   发布时间: 2009-11-17

edisondata3 wrote:
这是从良葛格socket范例程式来练习的
不过很奇怪档案大小总是会漏掉几个byte
照成图片打不开, 有时会漏有时不会, 不过漏掉的次数较多
看了很久, 好像是server那没接收完全,确切的原因还没找到
请各位指点一下
谢谢


一样的问题(原因)

作者: Duncan   发布时间: 2009-11-17

感谢

作者: edisondata3   发布时间: 2009-11-17

请问版主有关於socket传送图片的范例吗
试过很多程式码都没办法....

作者: alian954   发布时间: 2011-11-29