2 xiaobaiguoxing xiaobaiguoxing 于 2014.05.06 21:14 提问

求问用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个回答

jerry_dqh
jerry_dqh   Rxr 2014.05.12 21:35

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!