创建基于UDP协议的socket通讯

 
#ifndef UDP_H
#define UDP_H
 
#include "all.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "prl.h"
 
class Udp
{
 public:
    Udp();
    ~Udp();
 
    void udp_init();//char* ip,unsigned short int port);
    void udp_send();
    void udp_receive();
   
    Prl *m_Protocol;
 private:
    int sd, rc, n, slen;//,cliLen;
 // socklen_t cliLen;
    struct sockaddr_in servAddr;
    unsigned char commid;
 // struct sockaddr_in cliAddr;
};
 
#endif
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#include"udp.h"
#include"all.h"
#include "dispdateconf.h"
#include <iostream.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <assert.h>
 
Udp::Udp()
{
    sd=0;
    rc=0;
    n=0;
    slen=0;
   commid=0;
    m_Protocol=new Prl();
}
 
Udp::~Udp()
{
    if(m_Protocol)
    {
      delete m_Protocol;
      m_Protocol=0;
    }
}
    
void Udp::udp_init()//char *ip,unsigned short int port)
{
 
 /* get client IP address */
 /* cliAddr.sin_family =AF_INET;
  
    struct in_addr* netip;
    netip=new in_addr;
    inet_aton(ip,netip);
    cliAddr.sin_addr.s_addr = netip->s_addr;
 
    cliAddr.sin_port = htons(port);
 
    bzero(&(cliAddr.sin_zero),8);
    cliLen = sizeof(cliAddr);       */
 
 
    /* socket creation use SOCK_DRAM*/
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd<0) {
        if(DEBUG) printf("%s: cannot open socket with SOCK_DGRAM /n","server");
        exit(1);
     }
    
    /* bind local server port */
   servAddr.sin_family = AF_INET;                    //
   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);   //ip
   servAddr.sin_port = htons(LOCAL_SERVER_PORT);         //
 // if(DEBUG) printf("/nkan/n");
   rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
   if(rc<0) {
     if(DEBUG) printf("%s: cannot bind port number %d /n","server", LOCAL_SERVER_PORT);
 
     exit(1);
   }
   if(DEBUG) printf("%s: Waiting from GPRS data: %u/n","server",LOCAL_SERVER_PORT);
   //delete [] netip; 
}
void Udp::udp_receive()
{
    memset(receive_buffer,0x0,MAX_MSG);
     
    if(DEBUG) printf("/nreceive/n");
//    n = recvfrom(sd, receive_buffer, MAX_MSG, 0,NULL,NULL);
    cliLen = sizeof(cliAddr);
    n = recvfrom(sd, receive_buffer, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
   if(n<0) {
      if(DEBUG) printf("%s: cannot receive data /n","./server");
    }                
else
{                   
    // if(DEBUG) printf("%s:The data is %s /n",arg,receive_buffer);
    if(DEBUG) printf("%s:The data come from: %s/n", "./server",inet_ntoa(cliAddr.sin_addr));
 
    pthread_mutex_lock(&write_mutex);
    if(flag_write==0)    pthread_cond_signal(&cond_writerover);
    flag_write=flag_write+1;
    pthread_mutex_unlock(&write_mutex);
    }
}
 
void Udp::udp_send()
{
    slen=sendto(sd,send_buffer,MAX_MSG,0,(struct sockaddr *) &cliAddr_send,cliLen_send);
    if(DEBUG) printf("/nThe data send to: %s/n",inet_ntoa(cliAddr.sin_addr));
   
if(slen<0)
{
      if(DEBUG) printf("%s: cannot send data /n","server");
    }
else
{                                     
      if(DEBUG) printf("/nsend ok! /n");
    }
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值