星期日, 2月 08, 2009

CPP的socket設定

Server.exe PortNumber,例如Server 8000
  Client.exe IPAddress PortNumber,例如Client 127.0.0.1 8000
  然後在用戶端的命令列輸入字串,用戶端將會把消息送到伺服器,伺服器再把消息傳回用戶端。
  伺服器端,Server.cpp
  //Server.cpp
  #include
  #include
  #include
  #pragma comment(lib,"ws2_32.lib")
  int main(int argc, char* argv[]){
  //判斷是否輸入了PORT號
  if(argc!=2){
  printf("Usage: %s PortNumber\n",argv[0]);
  exit(-1);
  }
  //將PORT號轉化為整數
  short port;
  if((port = atoi(argv[1]))==0){
  printf("PORT號指定為整數");
  exit(-1);
  }
  WSADATA wsa;
  //初始化套接字DLL
  if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
  printf("Socket初始化失敗!");
  exit(-1);
  }
  //創建Socket
  SOCKET serverSocket;
  if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
  printf("創建Socket失敗!");
  exit(-1);
  }
  struct sockaddr_in serverAddress;
  memset(&serverAddress,0,sizeof(sockaddr_in));
  serverAddress.sin_family=AF_INET;
  serverAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
  serverAddress.sin_port = htons(port);
  //綁定
  if(bind(serverSocket,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
  printf("Socket绑定到PORT失敗!端口: %d\n",port);
  exit(-1);
  }
  //進入監聽狀態
  if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR){
  printf("監聽失敗!");
  exit(-1);
  }
  printf("Server %d is listening......\n",port);
  SOCKET clientSocket;//用來和用戶端通信的Socket
  struct sockaddr_in clientAddress;//用來和用戶端通信的Socket地址
  memset(&clientAddress,0,sizeof(clientAddress));
  int addrlen = sizeof(clientAddress);
  //接受連接
  if((clientSocket=accept(serverSocket,(sockaddr*)&clientAddress,&addrlen))==INVALID_SOCKET){
  printf("接受客户端連接失敗!");
  exit(-1);
  }
  printf("Accept connection from %s\n",inet_ntoa(clientAddress.sin_addr));
  char buf[4096];
  while(1){
  //接收數據
  int bytes;
  if((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR){
  printf("接收數據失敗!\n");
  exit(-1);
  }
  buf[bytes]='\0';
  printf("Message from %s: %s\n",inet_ntoa(clientAddress.sin_addr),buf);
  if(send(clientSocket,buf,bytes,0)==SOCKET_ERROR){
  printf("傳送數據失败!");
  exit(-1);
  }
  }
  //清理Socket占用的資源
  WSACleanup();
  return 0;
  }
  用戶端,Client.cpp
  //Client.cpp
  #include
  #include
  #include
  #pragma comment(lib,"ws2_32.lib")
  int main(int argc, char* argv[]){
  //判斷是否輸入了IP地址和Port
  if(argc!=3){
  printf("Usage: %s IPAddress PortNumber\n",argv[0]);
  exit(-1);
  }
  //把字符的IP地址轉化为u_long
  unsigned long ip;
  if((ip=inet_addr(argv[1]))==INADDR_NONE){
  printf("不合法的IP地址:%s",argv[1]);
  exit(-1);
  }
  //把port轉化成整数
  short port;
  if((port = atoi(argv[2]))==0){
  printf("Port有誤!");
  exit(-1);
  }
  printf("Connecting to %s:%d......\n",inet_ntoa(*(in_addr*)&ip),port);
  WSADATA wsa;
  //初始化套接字DLL
  if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
  printf("套接字初始化失败!");
  exit(-1);
  }
  //创建套接字
  SOCKET sock;
  if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
  printf("创建套接字失败!");
  exit(-1);
  }
  struct sockaddr_in serverAddress;
  memset(&serverAddress,0,sizeof(sockaddr_in));
  serverAddress.sin_family=AF_INET;
  serverAddress.sin_addr.S_un.S_addr = ip;
  serverAddress.sin_port = htons(port);
  //建立和服务器的连接
  if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
  printf("建立连接失败!");
  exit(-1);
  }
  char buf[4096];
  while(1){
  printf(">");
  //从控制台读取一行数据
  gets(buf);
  //发送给服务器
  if(send(sock,buf,strlen(buf),0)==SOCKET_ERROR){
  printf("发送数据失败!");
  exit(-1);
  }
  int bytes;
  if((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR){
  printf("接收數據失败!\n");
  exit(-1);
  }
  buf[bytes]='\0';
  printf("Message from %s: %s\n",inet_ntoa(serverAddress.sin_addr),buf);
  }
  //清理套接字占用的资源
  WSACleanup();
  return 0;
  }

參考資料:
小新a家
http://www.wretch.cc/blog/mntkey/21030760

如何使用DevCpp寫Socket程式
http://www.twbbs.net.tw/2090793.html

沒有留言: