关闭

大家来看看我写的这段程序,关于socket传输文件的问题

325人阅读 评论(0) 收藏 举报

发送端程序: 

UINT SendProc( LPVOID pParam )
{
 CMyClientDlg *MyDlg=(CMyClientDlg *)pParam;
 
 CFile file;
 CString Str;
 MyDlg->GetDlgItemText(IDC_EDIT1,Str);
 if(!file.Open(Str,CFile::modeRead | CFile::typeBinary))
 {
  AfxMessageBox("Can not open the Specifying file!");
 }
 CWnd *pWnd=(CStatic *)(MyDlg->GetDlgItem(IDC_FILESIZE));
 CString len;
 len.Format("%d Bytes",file.GetLength());
 pWnd->SetWindowText(len);

 char Buffer[4096];
 memset(Buffer,0,sizeof(Buffer));

 SOCKADDR_IN addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(8099);
 addr.sin_addr.S_un.S_addr=inet_addr("10.0.1.10");

 int nRead;
 int nTotalFileLength=file.GetLength();
 int nTotalLeftLength=nTotalFileLength;
 connect(MyDlg->m_sock,(SOCKADDR *)&addr,sizeof(SOCKADDR));
 int nleftlength=0;
 char FileLen[10];
 CString StrTransfer;
 CWnd *pS=(CStatic *)(MyDlg->GetDlgItem(IDC_FINISH));
 sprintf(FileLen,"%d",nTotalFileLength);
 send(MyDlg->m_sock,FileLen,10,0);

 MyDlg->hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
 char receive[4];
 int nRecv=recv(MyDlg->m_sock,receive,4,0);
 if(nRecv)
  SetEvent(MyDlg->hEvent);
 WaitForSingleObject(MyDlg->hEvent,INFINITE);
 do{
  nRead=file.Read(Buffer,sizeof(Buffer));
  nleftlength=send(MyDlg->m_sock,Buffer,strlen(Buffer)+1,0);
  StrTransfer.Format("Has Transfer %d Bytes",nleftlength);
  memset(Buffer,0,sizeof(Buffer));
  pS->SetWindowText(StrTransfer);
  nTotalLeftLength-=nRead;
 }
  while(nTotalLeftLength>0);
 
  closesocket(MyDlg->m_sock);
  return 1;
}

 

接受端:void CMyServerDlg::OnBtnStartup()
{
 // TODO: Add your control notification handler code here
 CString StrError;
 m_sock=socket(AF_INET,SOCK_STREAM,0);
 SOCKADDR_IN addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(8099);
 addr.sin_addr.S_un.S_addr=INADDR_ANY;
 int nbind=bind(m_sock,(SOCKADDR *)&addr,sizeof(SOCKADDR));
 if(nbind==SOCKET_ERROR)
 {
  StrError.Format("%d",WSAGetLastError());
  AfxMessageBox(StrError);
 }
 int nlisten=listen(m_sock,5);
 if(nlisten==SOCKET_ERROR)
 {
  StrError.Format("%d",WSAGetLastError());
  AfxMessageBox(StrError);
 }

 SOCKADDR_IN addrfrom;
 int nLen=sizeof(SOCKADDR_IN);
 SOCKET sockfrom=accept(m_sock,(SOCKADDR *)&addrfrom,(int *)&nLen);
 if(sockfrom==INVALID_SOCKET)
 {
  StrError.Format("%d",WSAGetLastError());
  AfxMessageBox(inet_ntoa(addrfrom.sin_addr)+StrError);
 }
 CString StrErr;
 
 char FileLen[10];
 memset(FileLen,0,10);
 int nrecv=recv(sockfrom,FileLen,10,0);
 if(nrecv==SOCKET_ERROR)
 {
  StrErr.Format("Recv Error %d",WSAGetLastError());
  AfxMessageBox(StrErr);
 }
 send(sockfrom,"yes",4,0);
 int nFileLength=0;
 nFileLength=atoi(FileLen);
 char Buffer[4096];
 memset(Buffer,0,sizeof(Buffer));

 int nLeftLength=nFileLength;
 int nRecv;
 CString StrTransfer;
 CWnd *pS=(CStatic *)GetDlgItem(IDC_FINISH);

 CFileDialog Dlg(FALSE);
 if(Dlg.DoModal()==IDOK)
 {
  CFile file(Dlg.GetPathName(),CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
  do
  {
   nRecv=recv(sockfrom,Buffer,sizeof(Buffer),0);
   if(nRecv==SOCKET_ERROR)
    AfxMessageBox("failed");
   file.Write(Buffer,nRecv);
   StrTransfer.Format("Has Transfer %d Bytes",nRecv);
   pS->SetWindowText(StrTransfer);
   nLeftLength=nLeftLength-nRecv;
   memset(Buffer,0,sizeof(Buffer));

  } while(nLeftLength>0);
  file.Close();
  closesocket(sockfrom);
 }
 }

接收文件有问题,只能正常接收.txt文件,其他的都有问题,我知道是算法不对,可我不知道该怎么改。大家一起看一看怎么样,顺便也学习一下?

我的邮箱 350109815@163.com

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:549次
    • 积分:18
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档