#include <Winsock2.h>
#include <Windows.h>
#include <tchar.h>
#include <string.h>
#include <iostream>
using namespace std;
#define BUFSIZE 512
#pragma comment(lib,"WS2_32.lib")
void main()
{
char recvBuf2[BUFSIZE];
HANDLE hFile;
DWORD dwResult;
DWORD dwBytesRead;
WORD wVersionRequested; //版本号
WSADATA wsaData; //数据
int err;
char name[20];
struct hostent *p;
wVersionRequested = MAKEWORD(1,1);
err = WSAStartup(wVersionRequested,&wsaData);
if( err != 0)
{
return;
}
if(LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
gethostname(name,20);
p=gethostbyname(name);
cout<<"计算机名:"<<name<<endl;
cout<<"服务器端IP:"<<(p->h_addr_list[0][0]&0x00ff)<<"."<<(p->h_addr_list[0][1]&0x00ff)<<"."
<<(p->h_addr_list[0][2]&0x00ff)<<"."<<(p->h_addr_list[0][3]&0x00ff)<<endl;
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); //建立套接字
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //绑定端口
listen(sockSrv,5); //转换socket套接子侦听套接子
cout<<"Server had opened....Waiting for connected"<<endl;
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char *sendBuf="Welcome to come";
cout<<"The IP: "<<inet_ntoa(addrClient.sin_addr)<<" is having connected"<<endl<<endl;
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
while(1){
char recvBuf[BUFSIZE];
char buf[BUFSIZE];
char order[10];
recv(sockConn,order,100,0);
switch(order[0])
{
case '1': //新建文件操作
cout<<"\t新建文件操作"<<endl;
recv(sockConn,recvBuf2,100,0);
cout<<"地址:"<<recvBuf2<<endl;
recv(sockConn,recvBuf,100,0);
cout<<"文件名:"<<recvBuf;
strcat(recvBuf2,recvBuf);
cout<<endl;
hFile = CreateFile((LPTSTR) recvBuf2, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
CREATE_ALWAYS, //Creates a new file, always
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
send(sockConn,"Could not create file ",strlen("Could not create file ")+1,0);
continue;
}
else send(sockConn,"Create file successfully.",strlen("Create file successfully.")+1,0);
cout<<"Create file successfully."<<endl<<endl;
CloseHandle(hFile);
break;
case'2': //写文件操作
cout<<"\t写文件操作"<<endl;
recv(sockConn,recvBuf2,100,0);
cout<<"地址:"<<recvBuf2<<endl;
recv(sockConn,recvBuf,100,0);
cout<<"文件名:"<<recvBuf<<endl;
strcat(recvBuf2,recvBuf);
hFile = CreateFile((LPTSTR) recvBuf2, // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"Could not open file error "<<GetLastError()<<endl;
send(sockConn,"Could not open file.",strlen("Could not open file.")+1,0);
continue;
}
else send(sockConn,"0",strlen("0")+1,0);
recv(sockConn,recvBuf,100,0);
cout<<"写的文件内容:";
cout<<recvBuf<<endl;
if(!WriteFile (hFile, recvBuf, strlen(recvBuf), &dwResult, NULL))
{
cout<<"Could not write to file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not write to file.",strlen("Could not write to file.")+1,0);
CloseHandle(hFile);
continue;
}
else{
cout<<"Wrote to file successfully."<<endl<<endl;
send(sockConn,"Wrote to file successfully.",strlen("Wrote to file successfully.")+1,0);
}
CloseHandle(hFile);
break;
case'3': //读文件操作
cout<<"\t读文件操作"<<endl;
recv(sockConn,recvBuf2,100,0);
cout<<"地址:"<<recvBuf2<<endl;
recv(sockConn,recvBuf,100,0);
cout<<"文件名:"<<recvBuf<<endl;
strcat(recvBuf2,recvBuf);
hFile = CreateFile((LPTSTR) recvBuf2, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"Could not open file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not open file",strlen("Could not open file")+1,0);
continue;
}
else send(sockConn,"0",strlen("0")+1,0);
if(!ReadFile(hFile, buf, BUFSIZE, &dwBytesRead, NULL))
{
cout<<"Could not read from file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not read from file",strlen("Could not read from file")+1,0);
CloseHandle(hFile);
continue;
}
else
{
buf[dwBytesRead]='\0';
cout<<"文件内容:"<<buf<<endl<<endl;
send(sockConn,buf,strlen(buf)+1,0);
}
CloseHandle(hFile);
break;
case'4': //复制文件内容操作
cout<<"\t复制文件内容操作"<<endl;
recv(sockConn,recvBuf2,100,0);
cout<<"原地址:"<<recvBuf2<<endl;
recv(sockConn,recvBuf,100,0);
cout<<"原文件名:"<<recvBuf<<endl;
strcat(recvBuf2,recvBuf);
hFile = CreateFile((LPTSTR) recvBuf2, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"Could not open file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not open file",strlen("Could not open file")+1,0);
continue;
}
else send(sockConn,"0",strlen("0")+1,0);
if(!ReadFile(hFile, buf, BUFSIZE, &dwBytesRead, NULL))
{
cout<<"Could not read from file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not read from file",strlen("Could not read from file")+1,0);
CloseHandle(hFile);
continue;
}
buf[dwBytesRead]='\0';
cout<<"Read this file successfully"<<endl;
send(sockConn,"Read this file successfully",strlen("Read this file successfully")+1,0);
CloseHandle(hFile);
recv(sockConn,recvBuf2,100,0);
cout<<"新地址:"<<recvBuf2<<endl;
recv(sockConn,recvBuf,100,0);
cout<<"复制到文件名:"<<recvBuf<<endl;
strcat(recvBuf2,recvBuf);
hFile = CreateFile((LPTSTR) recvBuf2, // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, //Creates a new file, always
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
send(sockConn,"Could not open file.",strlen("Could not open file.")+1,0);
continue;
}
else send(sockConn,"0",strlen("0")+1,0);
if(!WriteFile (hFile, buf, strlen(buf), &dwResult, NULL))
{
cout<<"Could not write to file error "<<GetLastError()<<endl<<endl;
send(sockConn,"Could not open file.",strlen("Could not open file.")+1,0);
CloseHandle(hFile);
continue;
}
else{
cout<<"Wrote this file successfully."<<endl<<endl;
send(sockConn,"Wrote this file successfully
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于socket远程文件系统.rar (26个子文件)
基于socket远程文件系统
TcpClient
TcpClient.plg 911B
TcpClient.dsp 4KB
TcpClient.cpp 5KB
TcpClient.dsw 541B
TcpClient.opt 48KB
Debug
TcpClient.ilk 757KB
TcpClient.pdb 1.04MB
vc60.idb 193KB
vc60.pdb 132KB
TcpClient.pch 5.16MB
TcpClient.exe 516KB
TcpClient.obj 104KB
TcpClient.ncb 41KB
Tcpsrv
Tcpsrv.dsp 4KB
Tcpsrv.opt 48KB
Tcpsrv.plg 896B
Tcpsrv.ncb 41KB
Tcpsrv.dsw 535B
Tcpsrv.cpp 11KB
Debug
Tcpsrv.obj 179KB
vc60.idb 193KB
Tcpsrv.exe 532KB
Tcpsrv.pch 3.48MB
Tcpsrv.ilk 779KB
vc60.pdb 140KB
Tcpsrv.pdb 1.05MB
共 26 条
- 1
资源评论
- zjsdtc9005052013-05-22嘛,倒也不能说描述错了,不过都是远程在本机上操作文件,我要找的是远程传送,不是我要的资源
- niping0932013-07-17还行吧,用了一段时间还行
- cheng_jian20012013-12-03远程在本机上操作文件
fayjame
- 粉丝: 0
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功