UDP--使用例子--服,客端双用

Posted on 2010-08-14 16:53 傅先生 阅读(188) 评论(0)  编辑 收藏 引用


/////////////////客端源码/////////////////

#include "iostream"
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define Const_BufLength 1024
#define Const_Port 12345

BOOL InitSocket(void);
WSADATA wsaData;
SOCKET m_socket;
sockaddr_in SerAdd,CliAdd;
int main()
{
 
 if(!InitSocket())
 {
  cout<<"套接字初始化出错,即将退出"<<endl;
  WSACleanup();
  system("pause");
  return -1;
 }
 int iLength=sizeof(SerAdd);
 int retval;
 char strSendMsg[Const_BufLength];
 char strRecvMsg[Const_BufLength];


 while(1)
 {
  memset(strSendMsg,0,sizeof(strSendMsg));
  cout<<"请输入要发送的:"<<endl;
  cin>>strSendMsg;
  if(strcmp(strSendMsg,"exit")==0)
  {
   strcpy(strSendMsg,"我是客户端,我要下线了");
   retval=sendto(m_socket,strSendMsg,sizeof(strSendMsg),0,(SOCKADDR*)&SerAdd,sizeof(CliAdd));
   closesocket(m_socket);
   WSACleanup();
   return 0;
  }
  retval=sendto(m_socket,strSendMsg,sizeof(strSendMsg),0,(SOCKADDR*)&SerAdd,sizeof(CliAdd));
  if(retval==SOCKET_ERROR)
  {
   cout<<"发送失败,错误ID为:"<<WSAGetLastError()<<endl;
   cout<<"程序出错,即将退出"<<endl;
   break;
  }
  else if(retval==0)
  {
   break;
  }
  else
  {
   cout<<"发送成功!"<<endl;
  }


  cout<<"等待服务器回应"<<endl;
  memset(strRecvMsg,0,sizeof(strRecvMsg));
  retval=recvfrom(m_socket,strRecvMsg,Const_BufLength,0,(SOCKADDR*)&SerAdd,&iLength);
  if(retval==SOCKET_ERROR)
  {
   cout<<"接受失败,错误ID为:"<<WSAGetLastError()<<endl;
   cout<<"程序出错,即将退出"<<endl;
   break;
  }
  else if(retval==0)
  {
   return 0;
  }
  else
  {
   cout<<"IP:Port\t"<<inet_ntoa(CliAdd.sin_addr)<<":"<<ntohs(CliAdd.sin_port)<<'\t'<<strRecvMsg<<endl;
  }
 }
 closesocket(m_socket);
 WSACleanup();
 return 0;
}


BOOL InitSocket(void)
{
 if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
 {
  cout<<"加载winsock失败,错误ID为:"<<WSAGetLastError()<<endl;
  return FALSE;
 }
 m_socket=socket(AF_INET,SOCK_DGRAM,0);
 if(INVALID_SOCKET==m_socket)
 {
  cout<<"套接字创建失败!错误ID为:"<<WSAGetLastError()<<endl;
  return FALSE;
 }
 SerAdd.sin_family=AF_INET;
 SerAdd.sin_port=htons(Const_Port);
 SerAdd.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
 return TRUE;
}



//////////////////////////服务端源码////////////////////////////
#include "iostream"
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define Const_BufLength 1024
#define Const_Port 12345

BOOL InitSocket(void);
WSADATA wsaData;
SOCKET m_socket;
sockaddr_in SerAdd,CliAdd;
int main()
{
 
 if(!InitSocket())
 {
  cout<<"套接字初始化出错,即将退出"<<endl;
  WSACleanup();
  system("pause");
  return -1;
 }
 int iLength=sizeof(CliAdd);
 int retval;
 char strSendMsg[Const_BufLength];
 char strRecvMsg[Const_BufLength];


 while(1)
 {
  cout<<"等待客户端回应"<<endl;
  memset(strRecvMsg,0,sizeof(strRecvMsg));

  retval=recvfrom(m_socket,strRecvMsg,Const_BufLength,0,(SOCKADDR*)&CliAdd,&iLength);
  if(retval==SOCKET_ERROR)
  {
   cout<<"接受失败,错误ID为:"<<WSAGetLastError()<<endl;
   break;
  }
  else if(retval==0)
  {
   break;
  }
  else
  {
   cout<<"IP:Port\t"<<inet_ntoa(CliAdd.sin_addr)<<":"<<ntohs(CliAdd.sin_port)<<'\t'<<strRecvMsg<<endl;
  }


  memset(strSendMsg,0,sizeof(strSendMsg));
  cout<<"收到客户消息,直接转发消息"<<endl;
  strcpy(strSendMsg,strRecvMsg);
  if(strcmp(strSendMsg,"exit")==0)
  {
   strcpy(strSendMsg,"收到客户下线的消息,我是服务器,我也下线了");
   retval=sendto(m_socket,strSendMsg,sizeof(strSendMsg),0,(SOCKADDR*)&CliAdd,sizeof(CliAdd));
   closesocket(m_socket);
   WSACleanup();
   return 0;
  }
  retval=sendto(m_socket,strSendMsg,sizeof(strSendMsg),0,(SOCKADDR*)&CliAdd,sizeof(CliAdd));
  if(retval==SOCKET_ERROR)
  {
   cout<<"发送失败,错误ID为:"<<WSAGetLastError()<<endl;
   break;
  }
  else if(retval==0)
  {
   break;
  }
  else
  {
   cout<<"发送成功!"<<endl;
  }
 }
 closesocket(m_socket);
 WSACleanup();
 return 0;
}


BOOL InitSocket(void)
{
 if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
 {
  cout<<"加载winsock失败,错误ID为:"<<WSAGetLastError()<<endl;
  exit(0);
 }
 m_socket=socket(AF_INET,SOCK_DGRAM,0);
 if(INVALID_SOCKET==m_socket)
 {
  cout<<"套接字创建失败!错误ID为:"<<WSAGetLastError()<<endl;
  return FALSE;
 }
 SerAdd.sin_family=AF_INET;
 SerAdd.sin_port=htons(Const_Port);
 SerAdd.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

 int retval;
 retval=bind(m_socket,(SOCKADDR*)&SerAdd,sizeof(SOCKADDR));
 if(SOCKET_ERROR==retval)
 {
  closesocket(m_socket);
  cout<<"端口绑定失败! 错误ID为:"<<WSAGetLastError()<<endl;
  return FALSE;
 }
 return TRUE;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


posts - 54, comments - 5, trackbacks - 0, articles - 2

Copyright © 傅先生