java客户端
package client; import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket; /**
* 文件发送客户端主程序
* @author admin_Hzw
*
*/
public class client { /**
* 程序main方法
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
int length = ;
double sumL = ;
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
FileInputStream fis = null;
boolean bool = false;
try {
File file = new File("F:\\img\\6.jpg"); //要传输的文件路径
long l = file.length();
socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1", ));
dos = new DataOutputStream(socket.getOutputStream());
fis = new FileInputStream(file);
sendBytes = new byte[];
while ((length = fis.read(sendBytes, , sendBytes.length)) > ) {
sumL += length;
System.out.println("已传输:"+((sumL/l)*)+"%");
dos.write(sendBytes, , length);
dos.flush();
}
//虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较
if(sumL==l){
bool = true;
}
}catch (Exception e) {
System.out.println("客户端文件传输异常");
bool = false;
e.printStackTrace();
} finally{
if (dos != null)
dos.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
}
System.out.println(bool?"成功":"失败");
}
}
C++客户端
#include <Winsock2.h>
#include <cstdio>
#include<iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsaData;
SOCKET sockClient;//客户端Socket
SOCKADDR_IN addrServer;//服务端地址
WSAStartup(MAKEWORD(, ), &wsaData); //一个socket的初始化函数
sockClient = socket(AF_INET, SOCK_STREAM, );//新建客户端socket,AF_INET使用IPv4进行通讯
//定义要连接的服务端地址
addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons();//连接端口6000
//连接到服务端
connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
//发送数据
char message[] = "HelloSocket!";
char messagerecv[] = "";
send(sockClient, message, strlen(message) + , );
recv(sockClient, messagerecv, , );
printf("%s\n", messagerecv); //发送文件
string file_name = "F:\\img\\6.jpg";
FILE *fp = fopen(file_name.c_str(), "rb");
int num = ;
while (!feof(fp))
{
num = fread(message, , , fp);
send(sockClient, message, num, );
} /*
//文件写操作
string file_name = "F:\\img\\7.jpg";
FILE *fp = fopen(file_name.c_str(), "wb");
int num = 0;
while (1)
{
num = recv(sockClient, message, 1000, 0);
if (num == 0)
break;
fwrite(message, 1, num, fp);
}
*/ //关闭socket
fclose(fp);
closesocket(sockClient);
WSACleanup();
}
C++服务端
#include <Winsock2.h>
#include <cstdio>
#include<iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsaData;
SOCKET sockServer;
SOCKADDR_IN addrServer;
SOCKET sockClient;
SOCKADDR_IN addrClient;
WSAStartup(MAKEWORD(, ), &wsaData);
sockServer = socket(AF_INET, SOCK_STREAM, );
addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//INADDR_ANY表示任何IP
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons();//绑定端口6000
bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR)); //Listen监听端
listen(sockServer, );//5为等待连接数目
printf("服务器已启动:\n监听中...\n");
int len = sizeof(SOCKADDR);
char message[];//发送至客户端的字符串
char recvBuf[];//接受客户端返回的字符串 //会阻塞进程,直到有客户端连接上来为止
sockClient = accept(sockServer, (SOCKADDR*)&addrClient, &len);
//接收并打印客户端数据
//recv(sockClient, recvBuf, 1000, 0);
sprintf(message, "%s", "gua");
printf("%s\n", message);
//send(sockClient, message, 1000, 0);
printf("%s\n", recvBuf); /*
//发送文件
string file_name = "F:\\img\\6.jpg";
FILE *fp = fopen(file_name.c_str(), "rb");
int num = 0;
while (!feof(fp))
{
num = fread(sendBuf, 1, 1000, fp);
send(sockClient, sendBuf, num, 0);
}
*/ //文件写操作
string file_name = "F:\\img\\7.jpg";
FILE *fp = fopen(file_name.c_str(), "wb");
int num = ;
while ()
{
num = recv(sockClient, message, , );
if (num == )
break;
fwrite(message, , num, fp);
} fclose(fp);
//关闭socket
closesocket(sockClient);
WSACleanup();
}