DOS下的文件传输(以bit传送)

 
server端
#include "Winsock2.h"
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void main(int argc,char **argv)
{
WSADATA wsaData;
int err;
­
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if ( err != 0 )
{
  return;
}
int servfd,clifd;
int len;
char buf[256];
struct sockaddr_in serv,cli,test;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(10000);
servfd=socket(AF_INET,SOCK_STREAM,0);
bind(servfd,(struct sockaddr *)&serv,sizeof(struct sockaddr_in));
listen(servfd,SOMAXCONN);
for(;;)
{
  len=sizeof(SOCKADDR);
  getsockname(servfd,(sockaddr *)&test,&len);
  cout<
  clifd=accept(servfd,(sockaddr *)&cli,&len);
  recv(clifd,buf,sizeof(buf),0);
  int i=atoi(buf);
  int rec=0;
  char pFileName;
  ofstream f;
  f.open("f://1.mp3",ios_base::binary);
  for(int j=0;j
  {
   memset(&pFileName,0,sizeof(pFileName));
   rec= recv(clifd,&pFileName,1,0);
   if(rec!= SOCKET_ERROR )
   {
    cout<<"good!"<
    f.write(&pFileName,rec);
   }
   else
   {
    cout<<"error!"<
    break;
   }
  }
}
  closesocket(clifd);
}
客户端
#include "Winsock2.h"
#include "iostream.h"
#include
#include
#include
#include
#pragma comment(lib,"Mswsock.lib")
#pragma comment(lib,"ws2_32.lib")
void main(int argc,char **argv)
{
WSADATA wsaData;
int err;
­
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if ( err != 0 )
{                
  return;
}
int servfd;
char buf[256];
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr=inet_addr("192.168.142.181");
serv.sin_port=htons(10000);
servfd=socket(AF_INET,SOCK_STREAM,0);
//bind(servfd,(struct sockaddr *)&serv,sizeof(serv));
struct stat statbuf;
stat("d://1.mp3", &statbuf);
char buf1[256];
sprintf(buf1,"%ld",statbuf.st_size);
cout<
connect(servfd,(sockaddr *)&serv,sizeof(serv));
send(servfd,buf1,sizeof(buf1),0);
while(true)
{
  
  HANDLE hFile = CreateFile("d://1.mp3", GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
  TransmitFile(servfd,hFile,0,1024,NULL,NULL,TF_REUSE_SOCKET|TF_DISCONNECT);
  cin>>buf;
  send(servfd,buf,sizeof(buf),0);
  cout<
  
}
closesocket(servfd);
}
 
 
-----------------------------------------------------------------------------------------------
 
 
 
 
改为了线程还是没用! e22.gif
#include "Winsock2.h"
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32.lib")
typedef struct parm
{
SOCKET s;
int i;
}Parm;
DWORD WINAPI ClientThread(LPVOID parm1)
{
Parm *parm;
parm =(Parm *)parm1;
SOCKET clifd=parm->s;
int i=parm->i;
char pFileName;
cout<ofstream f;
f.open("f://1.avi",ios_base::binary);
cout<<"recv..";
int rec=0;
for(int j=0;j{
  memset(&pFileName,0,sizeof(pFileName));
  rec= recv(clifd,&pFileName,1,0);
  if(rec!= SOCKET_ERROR )
  {
   if(j%1000==0)
    cout<<".";
   f.write(&pFileName,rec);
  }
  else
{
   cout<<"error!"<   break;
  }
  
}
cout<<"end sending!"<return 0;
}
void main(int argc,char **argv)
{
WSADATA wsaData;
HANDLE hThread;
DWORD dwThreadId;
int err;

err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if ( err != 0 )
{
  return;
}
int servfd,clifd;
int len;
char buf[256];
struct sockaddr_in serv,cli,test;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(10000);
servfd=socket(AF_INET,SOCK_STREAM,0);
bind(servfd,(struct sockaddr *)&serv,sizeof(struct sockaddr_in));
listen(servfd,SOMAXCONN);
for(;;)
{
  len=sizeof(SOCKADDR);
  getsockname(servfd,(sockaddr *)&test,&len);
  cout<  clifd=accept(servfd,(sockaddr *)&cli,&len);
  recv(clifd,buf,sizeof(buf),0);
 int i=atoi(buf);
  Parm *parm = new Parm;
  parm->s=clifd;
  parm->i=i;

  hThread=CreateThread(NULL,0,ClientThread,(LPVOID)parm,0,&dwThreadId);

}
  closesocket(clifd);
}

你可能感兴趣的:(DOS下的文件传输(以bit传送))