⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sock.cpp

📁 调用线程pthread 加锁和解锁的功能
💻 CPP
字号:
#include <string.h>//pour memcpy au moins
#include <stdio.h>//perror
#include <sys/types.h>
#include <sys/socket.h>//constantes, sockaddr generique, socket...
#include <stdlib.h>
#include "sock.h"

/////////////////////////////////////////////////////////////////
int Sock::sockaddLocRempli( const char * nomServ, const char *protoServ){

   struct servent *srv; /* pour recuperer les param du service */
 
   adrSock.sin_family = sDom;
      /* on recupere sa propre adresse internet */
   adrSock.sin_addr.s_addr = INADDR_ANY;
      /* recherche des parametres du service qu'on veut assurer*/
   srv = getservbyname (nomServ, protoServ);
   if (srv == NULL){
     cout <<nomServ<< "non trouve dans services\n";
     exit (0);
      }
   adrSock.sin_port = srv->s_port; /* numero de port du service */
   return sizeof(adrSock);
}

/////////////////////////////////////////////////////////////////
int Sock::sockaddLocRempli( short numPort){

   adrSock.sin_family = sDom;
      /* on recupere sa propre adresse internet */
   adrSock.sin_addr.s_addr = htonl(INADDR_ANY);
   adrSock.sin_port = htons(numPort); /* numero de port direct */
   return sizeof(adrSock);

}

/////////////////////////////////////////////////////////////////
int Sock::bind(int desc, const char * nomServ, const char *protoServ){
   int lg = sockaddLocRempli(nomServ, protoServ);
   if ((sRetour=::bind(desc,(struct sockaddr *)&adrSock,lg))<0)
     perror ("pb bind");
   return sRetour;
}

/////////////////////////////////////////////////////////////////
int Sock::bind(int desc, short nport){
  int lg = sockaddLocRempli(nport);
if ((sRetour=::bind(desc,(struct sockaddr *)&adrSock,lg))<0)
     perror ("pb bind");
	return sRetour;
}

/////////////////////////////////////////////////////////////////
void Sock::initparam(){
  sRetour=-1;
}

/////////////////////////////////////////////////////////////////
Sock::Sock(int tip, int protocole){
//tip contient SOCK_DGRAM ou SOCK_STREAM ou ...
  initparam();
  if ((sDesc=socket(sProto, tip, protocole))<0) perror("pb socket");
  else sRetour=1;//car sRetour relatif a bind et ici sans bind
}

/////////////////////////////////////////////////////////////////
Sock::Sock(int tip, short nport, int protocole){
  initparam();
  if ((sDesc=socket(sProto, tip, protocole))<0) perror("pb socket");
  else bind(sDesc, nport);
}

/////////////////////////////////////////////////////////////////
Sock::Sock(int tip, const char *nomServ, const char *protoServ, int protocole){
  initparam();
  if ((sDesc=socket(sProto, tip, protocole))<0) perror("pb socket");
  else bind(sDesc, nomServ, protoServ);
};

/////////////////////////////////////////////////////////////////
Sock::~Sock(){}

/////////////////////////////////////////////////////////////////
bool Sock::good(){
  return (sRetour>=0 && sDesc>0);
}

/////////////////////////////////////////////////////////////////
int Sock::getsDesc(){
  return sDesc;
}

/////////////////////////////////////////////////////////////////
int Sock::getsRetour(){
  return sRetour;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -