码迷,mamicode.com
首页 > 编程语言 > 详细

C++,java信息,文件传输

时间:2017-05-09 00:59:54      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:成功   inet_addr   fclose   地址   sizeof   new   client   sage   cat   

java客户端

技术分享
 1 package client;
 2 
 3 import java.io.DataOutputStream;  
 4 import java.io.File;  
 5 import java.io.FileInputStream;  
 6 import java.io.IOException;  
 7 import java.net.InetSocketAddress;  
 8 import java.net.Socket;  
 9   
10 /** 
11  * 文件发送客户端主程序 
12  * @author admin_Hzw 
13  * 
14  */  
15 public class client {  
16       
17     /** 
18      * 程序main方法 
19      * @param args 
20      * @throws IOException 
21      */  
22     public static void main(String[] args) throws IOException {  
23         int length = 0;  
24         double sumL = 0 ;  
25         byte[] sendBytes = null;  
26         Socket socket = null;  
27         DataOutputStream dos = null;  
28         FileInputStream fis = null;  
29         boolean bool = false;  
30         try {  
31             File file = new File("F:\\img\\6.jpg"); //要传输的文件路径  
32             long l = file.length();   
33             socket = new Socket();    
34             socket.connect(new InetSocketAddress("127.0.0.1", 6000));  
35             dos = new DataOutputStream(socket.getOutputStream());  
36             fis = new FileInputStream(file);        
37             sendBytes = new byte[1024];    
38             while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {  
39                 sumL += length;    
40                 System.out.println("已传输:"+((sumL/l)*100)+"%");  
41                 dos.write(sendBytes, 0, length);  
42                 dos.flush();  
43             }   
44             //虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较  
45             if(sumL==l){  
46                 bool = true;  
47             }  
48         }catch (Exception e) {  
49             System.out.println("客户端文件传输异常");  
50             bool = false;  
51             e.printStackTrace();    
52         } finally{    
53             if (dos != null)  
54                 dos.close();  
55             if (fis != null)  
56                 fis.close();     
57             if (socket != null)  
58                 socket.close();      
59         }  
60         System.out.println(bool?"成功":"失败");  
61     }  
62 }  
View Code

C++客户端

技术分享
 1 #include <Winsock2.h>
 2 #include <cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 #pragma comment(lib,"ws2_32.lib")
 6 void main()
 7 {
 8     WSADATA wsaData;
 9     SOCKET sockClient;//客户端Socket
10     SOCKADDR_IN addrServer;//服务端地址
11     WSAStartup(MAKEWORD(2, 2), &wsaData);    //一个socket的初始化函数
12     sockClient = socket(AF_INET, SOCK_STREAM, 0);//新建客户端socket,AF_INET使用IPv4进行通讯
13     //定义要连接的服务端地址
14     addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
15     addrServer.sin_family = AF_INET;
16     addrServer.sin_port = htons(6000);//连接端口6000
17     //连接到服务端
18     connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
19     //发送数据
20     char message[1000] = "HelloSocket!";
21     char messagerecv[1000] = "";
22     send(sockClient, message, strlen(message) + 1, 0);
23     recv(sockClient, messagerecv, 1000, 0);
24     printf("%s\n", messagerecv);
25     
26     
27     //发送文件
28     string file_name = "F:\\img\\6.jpg";
29     FILE *fp = fopen(file_name.c_str(), "rb");
30     int num = 0;
31     while (!feof(fp))
32     {
33         num = fread(message, 1, 1000, fp);
34         send(sockClient, message, num, 0);
35     }
36     
37 
38     /*
39     //文件写操作
40     string file_name = "F:\\img\\7.jpg";
41     FILE *fp = fopen(file_name.c_str(), "wb");
42     int num = 0;
43     while (1)
44     {
45         num = recv(sockClient, message, 1000, 0);
46         if (num == 0)
47             break;
48         fwrite(message, 1, num, fp);
49     }
50     */
51 
52 
53 
54     //关闭socket
55     fclose(fp);
56     closesocket(sockClient);
57     WSACleanup();
58 }
View Code

C++服务端

技术分享
 1 #include <Winsock2.h>
 2 #include <cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 #pragma comment(lib,"ws2_32.lib")
 6 void main()
 7 {
 8     WSADATA wsaData;
 9     SOCKET sockServer;
10     SOCKADDR_IN addrServer;
11     SOCKET sockClient;
12     SOCKADDR_IN addrClient;
13     WSAStartup(MAKEWORD(2, 2), &wsaData);
14     sockServer = socket(AF_INET, SOCK_STREAM, 0);
15     addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//INADDR_ANY表示任何IP
16     addrServer.sin_family = AF_INET;
17     addrServer.sin_port = htons(6000);//绑定端口6000
18     bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
19 
20     //Listen监听端
21     listen(sockServer, 5);//5为等待连接数目
22     printf("服务器已启动:\n监听中...\n");
23     int len = sizeof(SOCKADDR);
24     char message[1000];//发送至客户端的字符串
25     char recvBuf[1000];//接受客户端返回的字符串
26 
27     //会阻塞进程,直到有客户端连接上来为止
28     sockClient = accept(sockServer, (SOCKADDR*)&addrClient, &len);
29     //接收并打印客户端数据
30     //recv(sockClient, recvBuf, 1000, 0);
31     sprintf(message, "%s", "gua");
32     printf("%s\n", message);
33     //send(sockClient, message, 1000, 0);
34     printf("%s\n", recvBuf);
35 
36     /*
37     //发送文件
38     string file_name = "F:\\img\\6.jpg";
39     FILE *fp = fopen(file_name.c_str(), "rb");
40     int num = 0;
41     while (!feof(fp))
42     {
43         num = fread(sendBuf, 1, 1000, fp);
44         send(sockClient, sendBuf, num, 0);
45     }
46     */
47 
48     //文件写操作
49     string file_name = "F:\\img\\7.jpg";
50     FILE *fp = fopen(file_name.c_str(), "wb");
51     int num = 0;
52     while (1)
53     {
54         num = recv(sockClient, message, 1000, 0);
55         if (num == 0)
56             break;
57         fwrite(message, 1, num, fp);
58     }
59 
60 
61     fclose(fp);
62     //关闭socket
63     closesocket(sockClient);
64     WSACleanup();
65 }
View Code

 

C++,java信息,文件传输

标签:成功   inet_addr   fclose   地址   sizeof   new   client   sage   cat   

原文地址:http://www.cnblogs.com/cnblogs321114287/p/6828308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!