最近在做一个传输程序,不是很稳定?现把代码贴上来,希望高手可以给出一些指正!谢谢!

李海峡 2005-08-08 12:16:35
1.这是服务器接收线程函数
UINT ListenTcpThread(LPVOID lparam)
{
CSERVERDlg *pDlg=(CSERVERDlg *)lparam;

CSocket sockSrvr;
int createSucceed=sockSrvr.Create(pDlg->m_port);
if(createSucceed==0)
{
AfxMessageBox("创建服务错误!");
return -1;
}

int listenSucceed=sockSrvr.Listen(); //开始监听
if(listenSucceed==0)
{
AfxMessageBox("创建监听错误");
return -1;
}

CSocket recSo;
SOCKADDR_IN client;
int iAddrSize=sizeof(client);

int acceptSucceed=sockSrvr.Accept(recSo,(SOCKADDR *)&client,&iAddrSize); //接受连接并取得对方IP
if(acceptSucceed==0)
{
AfxMessageBox("接受客户端错误!");
return -1;
}
sockSrvr.Close();
/////////////////////判断发送的信息类型//////////////////////
char flag[2]={0};
if(recSo.Receive(flag,2)!=2)
{
AfxMessageBox("错误");

return -1;
}
pDlg->m_type=flag[0];

////////////////////////////////////////////////////////////

if(Flag==1) return 0;

pThreadLisen=::AfxBeginThread(ListenTcpThread,pDlg);
pDlg->ReceiveFileMsg(recSo,client);
return 0;

}

2.这是服务器端接收信息的函数:
int CSERVERDlg::ReceiveFileMsg(CSocket &recSo, SOCKADDR_IN &client)
{

bool iscon=true;
if(m_type=='F')
{

SaveYouFile(recSo,client);

}

if(m_type=='M')
{

char buff[1024]={0};
CString msg;
int ret=0;
for(;;)
{
ret=recSo.Receive(buff,1024);
if(ret <= 0 )
break;
msg+=buff;
}

CString strOut,strIn;
GetNamebyAddress(strIn,strOut);
CString youName;
youName.Format(inet_ntoa(client.sin_addr));
CString str=youName;

if(MySQL!=msg)
{
MySQL=msg;

iscon=true;

}else
{
iscon=false;

}

try
{

if(iscon)
{
m_list.SetTextColor(RGB(0,0,255));
m_list.InsertItem(0,str);
m_list.SetItemText(0,1,MySQL);
m_data->ExecuteSQL(MySQL);

}

}
catch(...)
{

}
}

recSo.Close();


return 0;
}

3.这是服务器端接收文件的函数
int CSERVERDlg::SaveYouFile(CSocket &recSo, SOCKADDR_IN &client)//接受文件
{

CString fname;

FILEINFO myFileInfo;

recSo.Receive(&myFileInfo,sizeof(FILEINFO));

int fileLength=myFileInfo.fileLength;//文件长度

CString strfileIp,strfileName,strfileLength;

strfileIp.Format(inet_ntoa(client.sin_addr));//发送者的IP地址
strfileName.Format(myFileInfo.fileName); //文件名
strfileLength.Format("%f",myFileInfo.fileLength/1024.0);

int strlen=strfileName.GetLength();
strlen-=19;

CString dirPath=strfileName;
dirPath=dirPath.Mid(0,strlen);

int i=0,j=0;
if(dirPath.Right(1)!="\\")dirPath+="\\";

try
{
while(i<dirPath.GetLength())
{ i=dirPath.Find("\\",j+1);
if(i==-1)i=dirPath.GetLength();
_mkdir(dirPath.Left(i).operator LPCTSTR());
j=i;

}
}catch(...)
{
}

strfileName=strfileName;

char buf[1024]={0};
int n=0; //接受的字节数 0表示结束
int temp=0;

try
{
CFile f(strfileName,CFile::modeCreate|CFile::modeWrite);//存文件

for(;;)
{
n=recSo.Receive(buf,1024); //接受
if(n <= 0) //0表示结束
break; //接受完毕
f.Write(buf,n);
temp+=n;

}
f.Close();
}catch(...)
{
}


if(temp==fileLength)
{
return 0;
}else
{
return -1;
}
}
...全文
151 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
teli_eurydice 2005-08-09
  • 打赏
  • 举报
回复
帮你UP
dirdirdir3 2005-08-09
  • 打赏
  • 举报
回复
你要自己定义一个协议来分出来每一个包,不然发生粘包就会出错了。
畅想畅享 2005-08-09
  • 打赏
  • 举报
回复
CSocket对多线程支持不好,可以从CSocket派生一个类。
李海峡 2005-08-08
  • 打赏
  • 举报
回复
高手们指点一二啊!在线等哦!
李海峡 2005-08-08
  • 打赏
  • 举报
回复
To : DentistryDoctor(My heart will fly,in the sky.)
邮件已发出,请查收!多谢了!
李海峡 2005-08-08
  • 打赏
  • 举报
回复
有时出现如下问题:
1.当服务器的cpu资源占用了50%以上时,客户端传图片过来会存在丢失一些图片的现象.
2.前天测试时,有个点始终传不上图片,原来是这个点的传输出现问题,导致服务器出现了‘烫烫烫烫烫'的一个目录,因为服务器我是自动创建目录,接收目录信息时出现了乱码。现在这个目录占用了几时个G的空间,其实这个目录下没有图片。删除不了这个目录。
3.服务器端有时会出现C++异常。

我的这个程序客户端目前有8个点在往服务器传送数据!
dirdirdir3 2005-08-08
  • 打赏
  • 举报
回复
怎么样不稳定?是出错还是数据发送不对还是......
DentistryDoctor 2005-08-08
  • 打赏
  • 举报
回复
这样不太好看,还不如将代码发给偶。

DentistryDoctor@msn.com
李海峡 2005-08-08
  • 打赏
  • 举报
回复
1.这是客户端发送线程函数
UINT SendMsgThread(LPVOID lparam) //TCP发送信息线程
{
while(SendMsgFlag)
{

CRecordset r1(m_data);

CString string="select * from capture where isup is null";
r1.Open(AFX_DB_USE_DEFAULT_TYPE,string,0);

CString file1,file2,file3,file4;
CString Passtime,Address,Roadname;
CString strSQL;

if(r1.GetRecordCount()>0)
{
r1.MoveFirst();

r1.GetFieldValue(1,Passtime);
r1.GetFieldValue(2,Address);
r1.GetFieldValue(3,Roadname);
r1.GetFieldValue(4,file1);
r1.GetFieldValue(5,file2);
r1.GetFieldValue(6,file3);
r1.GetFieldValue(7,file4);

Passtime.TrimRight();//违章时间
Address.TrimRight();//违章地点
Roadname.TrimRight();//车道名称
file1.TrimRight();//全景图片A
file2.TrimRight();//全景图片B
file3.TrimRight();//全景图片C
file4.TrimRight();//特写图片A

CString strpath;
int strlen=file1.GetLength()-19;
strpath=file1.Mid(0,strlen);

strSQL="insert into Capture(PassTime,Address,RoadName,FullVideoA_Path,FullVideoB_Path,FullVideoC_Path,LocalVideoA_Path,LocalVideoB_Path) values('"+Passtime+"','"+Address+"','"+Roadname+"','"+file1+"','"+file2+"','"+file3+"','"+file4+"','"+file4+"')";


if(SENDMESSAGE(strSQL)==0)
{

CString strSQL2="UPDATE Capture Set ISUP='是' Where PassTime='"+ Passtime + "' and Roadname='"+ Roadname + "'";

bool isok1=true;
bool isok2=true;
bool isok3=true;
bool isok4=true;


if(SENDFILE(file1,strpath) == -1 )
{
isok1=false;
}
m_list.SetTextColor(RGB(0,0,255));
m_list.InsertItem(0,file1);
m_list.SetItemText(0,1,error);

Sleep(1000);

if(SENDFILE(file2,strpath) == -1 )
{
isok2=false;
}
m_list.SetTextColor(RGB(0,0,255));
m_list.InsertItem(0,file2);
m_list.SetItemText(0,1,error);

Sleep(1000);

if(SENDFILE(file3,strpath) == -1 )
{
isok3=false;
}
m_list.SetTextColor(RGB(0,0,255));
m_list.InsertItem(0,file3);
m_list.SetItemText(0,1,error);

Sleep(1000);

if(SENDFILE(file4,strpath) ==-1 )
{
isok4=false;
}

m_list.SetTextColor(RGB(0,0,255));
m_list.InsertItem(0,file4);
m_list.SetItemText(0,1,error);

Sleep(1000);

if(isok1 && isok2 && isok3 && isok4)
{
m_data->ExecuteSQL(strSQL2);
m_list.SetItemText(0,2,"执行");

}
}
}else
{
Sleep(500);
}

}

return 0;
}


2.这是发送端发送信息的函数
UINT SENDMESSAGE(CString str)//发送SQL命令
{
CSocket sockClient;
int l=sockClient.Create();

int conn=sockClient.Connect(m_ip,m_port);
if(conn==0)
{
sockClient.ShutDown(2);
sockClient.Close();
return -1;

}


int end=0;

///***************************************************
end=sockClient.Send("M",2);
if(end==SOCKET_ERROR)
{
return -1;
}
else if(end!=2)
{
return -1;
}
///****************************************************

end=sockClient.Send(str,str.GetLength());
if(end==SOCKET_ERROR)
{
return -1;
}else
{
return 0;

}

sockClient.Close();


}

3.这是发送端发送文件的函数
UINT SENDFILE(CString str,CString strPath)//发送图片文件
{

//此处需要判断文件是否存在,否则在接收时会出错
CFileFind fd;
BOOL bExist;
bExist=fd.FindFile(str); //判断文件是否存在
fd.Close ();
if ( !bExist )
{
error = "文件不存在!";
return 0; //跳过
}
//此处需要判断文件是否存在,否则在接收时会出错

CSocket sockClient;
sockClient.Create();
CString ip;

sockClient.Connect(m_ip,m_port);

//首先发送标记F为文件,2
int end=0;
end=sockClient.Send("F",2);

///////////////////////////////////////////////////////////////////发送标志是否成功
if(end==SOCKET_ERROR)
{
error="SOCKET_ERROR错误!";
return -1;
}else if(end!=2)
{
error="发送错误,长度不对!";
return -1;
}

///////////////////////////////////////////////////////////////////
CFile myFile;
FILEINFO myFileInfo;
CString m_filename=str;

if(!myFile.Open(m_filename, CFile::modeRead | CFile::typeBinary))
{
error="打开文件错误!";
return -1;
}
myFileInfo.fileLength=myFile.GetLength(); //得到文件大小
strcpy(myFileInfo.fileName,strPath+myFile.GetFileName());//得到文件名称


sockClient.Send(&myFileInfo,sizeof(myFileInfo)); //发送文件信息

myFile.Seek(0,CFile::begin);
char m_buf[1024]={0};
CString strError;
int num=0;
end=0;
int temp=0;

for(;;)
{
num=myFile.Read(m_buf, 1024);
if(num==0) break;
end=sockClient.Send(m_buf, num);
temp+=end;

if(end==SOCKET_ERROR)
{

error = "发送文件失败!";
return -1;

}
}

if(temp==myFileInfo.fileLength)
{
error = "文件发送成功!";
}else
{
error="文件长度错误!";
return -1;
}

myFile.Close();
sockClient.Close();

return 0;



}
lixiaosan 2005-08-08
  • 打赏
  • 举报
回复
太长了。。。
yexingren 2005-08-08
  • 打赏
  • 举报
回复

给俺也发一份 行不 !yexingren23@126.com

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧