一个简单的在本地传输文件的网络程序
经常在CSDN贴吧看见很多关于网络之间传输的例子(比如传输截屏文件),所以很想写一个作为学习。
此程序设定很简单,就是在本地传输。当然,要扩展到网络也很简单,改个ip就ok了。
基本思路:
双方遵循一个包头格式,根据包头确定之后的操作。
服务器代码:
[cpp]
#include <string>
#include <io.h>
#define PORT 6666
struct PacketHeader
{
char code[10]; // 指令:"EOF"=断开连接 "PIC"=接收图片
char name[50]; // 名字:包括后缀
LONG32 size; // 大小:字节
PacketHeader()
{
memset(code,0,sizeof(code));
memset(name,0,sizeof(name));
size = 0;
}
};
// 在本地传输图片
void server()
{
WSADATA wd;
int ret = WSAStartup(MAKEWORD(2,2),&wd);
if(ret != 0)
{
cout<<"Initialize Winsock Failed! "<<GetLastError()<<endl;;
return;
}
cout<<"WSAStartup OK"<<endl;
sockaddr_in addr_serv,addr_cli;
int sock_listen,sock_accept;
try{
sock_listen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock_listen == SOCKET_ERROR)
{
cout<<"create socket failed! "<<GetLastError()<<endl;
throw 1;
}
cout<<"socket OK"<<endl;
memset((void*)&addr_serv,0,sizeof(sockaddr));
addr_serv.sin_family = AF_INET;
addr_serv.sin_addr.S_un.S_addr = INADDR_ANY;
addr_serv.sin_port = htons(PORT);
ret = bind(sock_listen,(sockaddr*)&addr_serv,sizeof(addr_serv));
if(ret != 0)
{
cout<<"bind failed! "<<GetLastError()<<endl;
throw 2;
}
cout<<"bind OK"<<endl;
ret = listen(sock_listen,1);
if(ret != 0)
{
cout<<"listen failed! "<<GetLastError()<<endl;
throw 2;
}
cout<<"listen OK"<<endl;
int addrlen = sizeof(addr_cli);
sock_accept = accept(sock_listen,(sockaddr*)&addr_cli,&addrlen);
if(sock_accept == INVALID_SOCKET)
{
cout<<"accept failed! "<<GetLastError()<<endl;
throw 2;
}
cout<<"accept OK"<<endl;
closesocket(sock_listen); //停止监听
while(true)
{
cout<<"输入:(EOF表示断开连接,文件名表示发送文件,例如sumos.txt)"<<endl;
string input;
cin>>input;
if(input.compare("EOF") == 0)
{
PacketHeader ph;
strcpy_s(ph.code,"EOF");
send(sock_accept,(const char*)&ph,sizeof(ph),0);
break;
}
else
{
FILE* fp = NULL;
fopen_s(&fp,input.c_str(),"rb");
if(fp == NULL)
{
cout<<"打开失败"<<endl;
continue;
}
else
{
LONG32 sz = _filelength(_fileno(fp));
&n
补充:软件开发 , C++ ,