#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");
}
}