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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号