xiaobaiguoxing 2014-05-06 13:14 采纳率: 0%
浏览 1905

求问用C/C++实现以下JAVA代码的功能麻烦吗,求帮助...

是一个Socket的服务端,接收客户端发来的文件后,再返回一个文件。
package sock;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**

服务器
*/
public class Server extends ServerSocket{

private static final int PORT =2013;

private ServerSocket server;
// private ArrayList list = new ArrayList();
private DataInputStream dis;
private FileOutputStream fos;

public Server()throws Exception{
try {
try {
server =new ServerSocket(PORT);
System.out.println("服务器启动成功……");
while(true){
Socket client = server.accept();

       // list.add(client);

        new Thread(new FileFetcher(client)).start();
    }
}catch (Exception e) {
    e.printStackTrace();
}

}catch (Exception e) {
e.printStackTrace();
}
}

class FileFetcher implements Runnable{

private Socket client = null;

public FileFetcher(Socket client){
this.client = client;
}

@Override
public void run() {
// TODO Auto-generated method stub
try{
dis =new DataInputStream(client.getInputStream());
//文件名和长度
String fileName = dis.readUTF();
long fileLength = dis.readLong();

 File file = new File("E:/Server/" + client.getInetAddress() + "-" +fileName);
 fos =new FileOutputStream(file);

 byte[] sendBytes =new byte[1024];
 int transLen =0;
 System.out.println("----开始接收文件<" + fileName +">,文件大小为<" + fileLength +">----");
 while(true){
     int read =0;
     read = dis.read(sendBytes);
     if(read == -1)
         break;
     transLen += read;
     System.out.println("接收文件进度" +100 * transLen/fileLength +"%...");
     fos.write(sendBytes,0, read);
     fos.flush();

     if (transLen == fileLength){
         break;
     }
 }
 System.out.println("----接收文件<" + fileName +">成功-------");

 File result = dealWithFile(file);
 DataOutputStream out = new DataOutputStream( client.getOutputStream());

 if (result == null){
     String resultStr = "未处理文件,请重试!";

     System.out.println(resultStr);

     out.write(resultStr.getBytes());
     out.flush();
     out.close();
     return ;
 }

 FileInputStream in = new FileInputStream(result);
 out.writeUTF(result.getName());
 out.flush();
 out.writeLong(result.length());
 out.flush();
 byte[] getbytes = new byte[1024];
 while (true){
     int tempRead = 0;

     tempRead = in.read(getbytes);
     if (tempRead == -1){
         break;
     }
     out.write(getbytes, 0, tempRead);
 }

 out.flush();
 out.close();
 in.close();

 client.close();

} catch (Exception e){
    e.printStackTrace();
} finally {
    try{
    if(dis !=null)
        dis.close();
    if(fos !=null)
        fos.close();
    //server.close();
    } catch (Exception e){
        e.printStackTrace();
    }
}

}

private File dealWithFile(File file){
return new File("E:/Server/ad/1.jpg");
//return null;
}
}

public static void main(String[] args)throws Exception {
new Server();
}
}

  • 写回答

1条回答 默认 最新

  • 碼上道 2014-05-12 13:35
    关注

    这个是socket基本的功能,网上搜索比较多了。

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用