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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog