Home > other >  Based on vc + + socket picture, how to make the program can transport large images
Based on vc + + socket picture, how to make the program can transport large images

Time:10-10


//send
 

CFileDialog fileDlg (TRUE);
FileDlg. M_ofn. LpstrTitle="open file";
FileDlg. M_ofn. LpstrFilter="Text Files (*.txt) \ 0 *. TXT \ 0 all Files (*. *) \ 0 *. * \ \ 0 0";
If (IDOK==fileDlg. DoModal ())
{
CFile file (fileDlg GetFileName (), CFile: : modeRead);
Cstrings path="";
Path=fileDlg. GetPathName ();//the file path name
DwFileLen=file. GetLength ();
SetDlgItemText (IDC_EDIT_SEND, path);

Char * pbufs=new char [dwFileLen];
File. Read (pbufs dwFileLen);
If (SetTimer (1100, NULL))
{
Int tt;
Tt=send (sockClient, pbufs dwFileLen, 0).
If (SOCKET_ERROR==tt)
{
MessageBox (" failure ");
}
SetDlgItemInt (IDC_EDIT_sum1 dwFileLen);
}
}


//receive
 SOCKET sock=((RECVPARAM *) lpParameter) - & gt; The sock. 
HWND HWND=((RECVPARAM *) lpParameter) - & gt; The HWND;
The delete lpParameter;
SOCKADDR_IN addrClient;
Int len=sizeof (SOCKADDR);
Int retval;
While (TRUE)
{
The SOCKET sockConn=accept (the sock, (SOCKADDR *) & amp; AddrClient, & amp; Len);
Char recvBuf [102400].
Char sendBuf [200].
Sprintf (sendBuf, "% s", inet_ntoa (addrClient. Sin_addr));
Send (sockConn, sendBuf, strlen (sendBuf) + 1, 0).
Sleep (1000);
Retval=recv (102400, 0 sockConn, recvBuf);
If (SOCKET_ERROR==retval)
{
AfxMessageBox (" receiving data failure ");
break;
}
If (0==retval)
{
break;
}
CFileDialog fileDlg (FALSE);
FileDlg. M_ofn. LpstrFilter="All Files (*. *) \ 0 *. TXT \ \ 0 0 *. * \ \ 0 0";
FileDlg. M_ofn. LpstrDefExt="BMP";
FileDlg. M_ofn. LpstrTitle="save receive file";
If (IDOK==fileDlg. DoModal ())
{
CFile file (fileDlg GetFileName (), CFile: : modeCreate | CFile: : modeWrite);
File. The Write (recvBuf, retval);
File. The Close ();
}
  • Related