18,356
社区成员
发帖
与我相关
我的任务
分享
void Cserver_mfcDlg::OnBnClickedListen()//监听本地端口
{
// TODO: 在此添加控件通知处理程序代码
m_hThread_Server=CreateThread(NULL,100,Thread_Server,(void*)this,NULL,NULL); //建立本地服务器端线程监听
}
DWORD Cserver_mfcDlg::Thread_Server(LPVOID lpParam)
{
Cserver_mfcDlg *dlg=(Cserver_mfcDlg*)AfxGetApp()->GetMainWnd();
dlg->MessageBox(_T("thread_server!"),_T("tip"));
dlg->lsn_port.GetWindowTextA(dlg->lsn_port_str); //获取端口号
int server_port=0;
ACE::init();
if(dlg->lsn_port_str=="")
{
server_port=10000; //如果没填,则默认10000号
}
else
{
server_port=_ttoi(dlg->lsn_port_str);
}
ServerAddr.set(server_port,"localhost");
if(ServerAcceptor.open(ServerAddr,1)!=0) //绑定端口
{
//cout<<"band port failed!"<<endl;
dlg->MessageBox(_T("band port failed!"),_T("band tip"));
//return -1;
}
else
{
dlg->MessageBox(_T("band port success!"),_T("band tip"));
ACE_Time_Value timeout(20,0);
if(ServerAcceptor.accept(ServerPeer)==-1) //与客户端建立连接
{
dlg->MessageBox(_T("server connect failed!"),_T("band tip"));
}
else
{
dlg->MessageBox(_T("server connect success!"),_T("band tip"));
while((s_rev_byte=ServerPeer.recv(serverbuf,sizeof(serverbuf)))!=-1)
{
ACE_INET_Addr rome_addr;
ServerPeer.get_local_addr(rome_addr); //获取远端连接ip
CString str_buf;
dlg->msg_record_str+=rome_addr.get_host_name();
str_buf=serverbuf;
str_buf=":"+str_buf.Left(s_rev_byte)+"\r\n";
//str_buf.Format("%s%s",buf,"\n"); //获取接受消息内容
dlg->msg_record_str+=str_buf;
dlg->msg_record.SetWindowTextA(dlg->msg_record_str); //在窗口中显示
}
}
ACE_Thread_Manager::instance ()->cancel_all ();
ACE_Time_Value timeout2(30);
ACE_Thread_Manager::instance()->wait(&timeout2);
ACE::fini();
return NULL;
}
void Cserver_mfcDlg::OnBnClickedConnect() //客户端连接
{
// TODO: 在此添加控件通知处理程序代码 ACE_LOCALHOST "192.168.1.82"
ACE::init();
//UpdateData(FALSE); //获取需要连接的对方的ip
CString str_ip="";
int client_port=0;
remote_ip.GetAddress(remote_C_IP);
con_port.GetWindowTextA(con_port_str);
if(remote_C_IP==0)//如果没有输入指定连接ip,则默认192.168.1.171
{
str_ip="192.168.1.82";
}
else
{
remote_ip.GetAddress(remote_C_IP); //获取输入的ip
str_ip.Format("%d.%d.%d.%d",(remote_C_IP>>24)&0xff,
(remote_C_IP>>16)&0xff,
(remote_C_IP>>8)&0xff,
(remote_C_IP)&0xff);
}
if(con_port_str=="")
{
client_port=10000;
}
else
{
client_port=_ttoi(con_port_str);
}
ClientAddr.set(client_port,str_ip); //需连接对方服务器的ip 就是这里有问题
//ACE_SOCK_Connector connector;
ACE_Time_Value timeout(10,0);
//ACE_SOCK_Stream str_send;
ofstream logstream ("./error.log"); //输出错误信息
//MessageBox("connection success!","connection tip");
if(connector.connect(ClientPeer,ClientAddr,&timeout)==-1)
{
MessageBox("client connection failed!","connection tip");
//logstream<<str_send;
//log
//int errorno;
//strerror(errno);
ACE_LOG_MSG->msg_ostream (&logstream);
ACE_DEBUG((LM_INFO, ACE_TEXT ("some text\n")));//此处无输出
logstream<<"test again\n";
logstream<<strerror(errno); //此处输出为no error
}
else
{
MessageBox("client connection success!","connection tip");
//m_hThread_Client=CreateThread(NULL,100,Thread_Client,(void*)this,NULL,NULL);
}
ACE::fini();
}