正文

简单的Socket服务客户端2007-08-21 14:52:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/28730.html

分享到:

有阻塞的程序,部分,不好意思

=============================================server

void CSeverClientDlg::OnButton_ThreadOn()
{
 UpdateData(true);//刷新端口

 if(m_edit_Port=="")
 {
  AfxMessageBox("请输入端口");
 }
 else
 { 
  port=atoi(m_edit_Port); 
  deal();
 }

 

}

void CSeverClientDlg::deal()
{
 CString linkPort="["+m_edit_Port+"]";
 CString nowPing;

 if(IsListening==true)
 {
  AfxMessageBox("已经在监听");
 }
 else
 {
 ServerSocket=socket(AF_INET,SOCK_STREAM,0);
 ClientSocket=socket(AF_INET,SOCK_STREAM,0);

 struct sockaddr_in ServerAddr;

 if( ClientSocket == -1 )
 {
  AfxMessageBox( "accept调用失败!\n" );
  exit( 0 );
 }

 ServerAddr.sin_family = AF_INET;
 ServerAddr.sin_port = htons(port);//监视的端口号2008
 ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);//本地IP

 if( (bind(ServerSocket,( struct sockaddr *)&ServerAddr,sizeof(ServerAddr))) <0)
 {
  AfxMessageBox("bind failed!");
 }

 if (listen(ServerSocket,5) < 0)
 {
  AfxMessageBox("listen error");
 }

 if(WSAAsyncSelect(ServerSocket,m_hWnd,WM_MY_MSG,FD_WRITE|FD_CLOSE|FD_READ|FD_ACCEPT)==0)
  AfxMessageBox("已经开始监听端口   :"+linkPort);
  
 nowPing="打开端口并开始监听\r\n";
 m_edit_Ping+=nowPing;
 UpdateData(false);
 IsListening=true;
 }//if
 
}

void CSeverClientDlg::OnButton_Cut_Listen()
{
 if(IsListening==false)
 {
  AfxMessageBox("目前没有监听");
 }
 else
 {
  IsListening=false;
  m_edit_Ping+="已经断开监听了\r\n";
  UpdateData(false);
  send(ClientSocket,"2",strlen("2")+1,0);
  closesocket(ServerSocket);
  closesocket(ClientSocket);
 }
}


LRESULT CSeverClientDlg::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
 int   nLength;
 int numbytes;
 char buf[ MAXDATASIZE ];
 CString str,str2;
 CEdit *  pEdit;
 CString rPeerAddress;
 CString cport;

//switch
 switch(LOWORD(lParam))
 {
 case FD_READ:
 numbytes = recv( ClientSocket, buf, MAXDATASIZE, 0 );//recv接收
 
 if( numbytes == -1 )
 {
  AfxMessageBox( "recv失败!" );
  WSACleanup( );
  exit( 0 );
 }

 buf[ numbytes  ] = '\0';
 str.Format("%s",buf);
 str+="\r\n";
 
 pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
 pEdit->GetWindowText(str2); 
 str2+=str;;
 pEdit->SetWindowText(str2);
 pEdit->SetSel(str2.GetLength(),   str2.GetLength());

 break;

 case FD_WRITE:

 break;
 
 case FD_ACCEPT:
 {
 struct sockaddr_in ClientAddr;
 int sin_size = sizeof( struct sockaddr_in );
 ClientSocket = accept( ServerSocket, ( struct sockaddr * )&ClientAddr, &sin_size ); 

 m_edit_Ping+="________________________有客户端连接上_________________________\r\n";
 rPeerAddress=inet_ntoa(ClientAddr.sin_addr);
 cport.Format("%u",ntohs(ClientAddr.sin_port));
 m_edit_Ping+="客户端的IP为   :"+rPeerAddress;
 m_edit_Ping+="客户端端口为   :"+cport+"\r\n";

 UpdateData(false);
 send(ClientSocket,"1",strlen("1")+1,0);
 break;
 }

 }//switch

 return 0;


}

======================================================client

void CClientSocketDlg::OnBUTTONRequest()
{
 CString str;

 if(IsRequest==true)
 {
  AfxMessageBox("已经连接了.");
 }
 else
 {
 UpdateData(true);
 if(m_edit_IP=="")
 {
  AfxMessageBox("请输入IP,在IP框里面.");
 
 }
 else
 {
  if(m_edit_Port=="")
  {
   AfxMessageBox("请输入端口,在端口框输入.");
  }
  else
  {
   IsRequest=true;
   IP  =m_edit_IP;
   Port=atoi(m_edit_Port);
   deal();
  }
 }
 }//if(isR......)

}

void CClientSocketDlg::deal()
{
// SOCKET ClientSocket;
 ClientSocket=socket(PF_INET,SOCK_STREAM,0);

 struct sockaddr_in ServerAddr;
// int numbytes;
// char buf[ MAXDATASIZE ];
 CString str;

 ServerAddr.sin_family      = AF_INET;
 ServerAddr.sin_port        = htons(Port);
 ServerAddr.sin_addr.s_addr = inet_addr(IP);
 memset( &( ServerAddr.sin_zero ), 0, sizeof(ServerAddr.sin_zero ) );

 if(ClientSocket<0)
 {
  AfxMessageBox("Client failed!");
 }

 if( connect( ClientSocket, (struct sockaddr *) & ServerAddr, sizeof(struct sockaddr)) == -1 )
 {
  AfxMessageBox("请查看服务器端端口是否打开");
  WSACleanup( );
  exit( 0 );
 }
 else
 {
  m_edit_TrueOrFalse="连接成功!";
  UpdateData(false);
 }

 WSAAsyncSelect(ClientSocket,m_hWnd,WM_MESSAGE1,FD_WRITE|FD_CLOSE|FD_READ|FD_ACCEPT);

// closesocket( ClientSocket );
// WSACleanup( );

}

void CClientSocketDlg::OnBUTTONCutDown()
{
 if(closesocket(ClientSocket)==0)
 {
 m_edit_TrueOrFalse="已经断开连接";
 IsRequest=false;
 UpdateData(false);
 }
 
}

void CClientSocketDlg::OnBUTTONSend()
{
 CString str;
 
 UpdateData(true);
 if(m_edit_Send=="")
 {
  AfxMessageBox("内容不为空.");
 }
 else
 {
 UpdateData(true);

 send(ClientSocket,m_edit_Send,strlen(m_edit_Send)+1,0);
 m_edit_Send="";

 UpdateData(false);
 }
 
}

LRESULT CClientSocketDlg::DoMyMessage(WPARAM wParam,LPARAM lParam)

 int numbytes;
 char buf[ MAXDATASIZE ];
 CString str;
 int i;

 switch(LOWORD(lParam))
 {
 case FD_READ:
 numbytes = recv( ClientSocket, buf, MAXDATASIZE, 0 );//recv接收
 
 if( numbytes == -1 )
 {
  AfxMessageBox( "recv失败!" );
  WSACleanup( );
  exit( 0 );
 }

 buf[ numbytes ] = '\0';

 if(CString(buf)==CString("1"))
 {
  m_edit_Accept="谢谢你的连接";
 }
 else
  if(CString(buf)==CString("2"))
 {
  m_edit_Accept="服务器端已经断开连接,请不要在此时进行连接,否则程序自动退出";
  IsRequest=false;
  closesocket(ClientSocket);
 }

 UpdateData(false);

 break;

 case FD_WRITE:break;
  
 case FD_ACCEPT:break;

 default: break;
 }
 return 0;
}

阅读(3735) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册