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

📄 udp_send.c

📁 unix 下用pro*c tuxedo 开发的东西
💻 C
字号:
/*
 * BANKING TRANSACTION SWITCHING SYSTEM (version 1.0).
 *
 * (C) COPYRIGHT International Business Machines Corp. 1997
 * All Rights Reserved
 * Licensed Materials - Property of IBM
 */

/*
 ***************************************************************************
 * (1)  File Name      : udp_send.c                                        *
 * (2)  Module ID      :                                                   *
 * (3)  Module Name    : Send UDP package module.                          *
 *                                                                         *
 * (4)  Purpose        : 1. Initialize a UDP socket to send UDP package.   *
 *                       2. Broadcast UDP message to destination.          *
 *                       3. Close UDP socket.                              *
 *                                                                         *
 * (5)  Author         : Chen Li (IBM China)                               *
 * (6)  Date Created   : 1997/06/01                                        *
 * (7)  Version        : 1.0                                               *
 * (8)  Environment    : AIX 4.x                                           *
 * (9)  Warnings       :                                                   *
 ***************************************************************************
 * (10) Changed Logs   :                                                   *
 *      Change No.   Date          Author          Reason For Change       *
 *                                                                         *
 ***************************************************************************
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/select.h>

#include <sys/types.h>

#include <sys/socket.h>
#include <sys/socketvar.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/proc.h>

#include "udp_send.h"
#include "prread.h"

#define MAXSTRLEN 300

char  udp_send_port[51];	/* service name which UDP package
                                   will be sent to */
char  udp_bind_port[51];	/* service name which use to bind
                                   UDP port */
char  nets[50][51];		/* address array which UDP package will
                                   be sent to */
int   totalnet = 0;		/* total destination address */

char ServerAddress[MAXSTRLEN]="0.0.0.0";  /* use any net address */
int broadcastPort ; 		/* port which UDP package will be sent
                                   to  */
int UDP_Port ;                  /* port which UDP socket use to bind */
int UDP_Socket;			/* UDP socket */


/****************************************************************
                                                                 
    FUMCTION DESCRIPTION: Initialize an UDP socket to send UDP  
                          package 

    PATAMETERS: void    

    RETURN CODES:                                     
       1 :  initialize UDP socket successfully
      -1 :  create socket error
      -2 :  bind socket error
      -3 :  setsockpot() return error 
      -4 :  not define service name in /etc/service

 ***************************************************************/
short UDP_Init()
{
    int i;
    struct sockaddr_in ClieUDPAddr;
    int CanBroadcast = 1;
    struct servent *ptrServiceEntry;

    if ((ptrServiceEntry = getservbyname(udp_bind_port, "udp")) == NULL)
        return -4;
    else
        UDP_Port = ptrServiceEntry->s_port;
    
    memset(&ClieUDPAddr, 0, sizeof(ClieUDPAddr));
    ClieUDPAddr.sin_family = AF_INET;
    ClieUDPAddr.sin_addr.s_addr = inet_addr(ServerAddress);
    ClieUDPAddr.sin_port = htons(UDP_Port);

    if ((UDP_Socket = socket( AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        UDP_Socket = 0;
        printf("?!==>Failed to obtain a UDP Sockect, errno is %d\n\n", errno);
        return -1;
    }

/*
    i = 0;
    while(1)
    {
        if (bind(UDP_Socket, &ClieUDPAddr, sizeof(ClieUDPAddr)) == -1)
        {
#ifdef DEBUG
printf("bind try  ....\n");
#endif
            i++;
            if ( i < 100 )
                ClieUDPAddr.sin_port = htons(UDP_Port+i);
            else
            {
#ifdef DEBUG
printf("bind fail ....\n");
#endif
                return -2;
            } 
        }
        else
            break;
    }
*/ 
    if (setsockopt(UDP_Socket, SOL_SOCKET, SO_BROADCAST,
            (char*)&CanBroadcast, sizeof(CanBroadcast)) < 0)
    {
        printf("?!==>Failed to set Sockect, errno is %d\n\n", errno);
        return -3;
    }

    return 1;
}

/****************************************************************
                                                                 
    FUNCTION   DESCRIPTION: Scan the input string 'src', If there
			    is a character '|' in 'src' string,
			    replace it as '||' in 'dest' string.

    PARAMETERS : void    

    RETURN CODES : void                                

****************************************************************/
void UDP_Close()
{
    close(UDP_Socket);
}

/****************************************************************
                                                                 
    FUMCTION   DESCRIPTION: Broadcast UDP message 'Msg' to
                            destination defined in 'nets' array

    PATAMETERS:     
      INPUT PATAMENTERS
        Msg : pointer to the message will be sent
        MsgLen : length of the message

    RETURN CODES                                     
        1 : send message successfully

***************************************************************/

short SysBoraccastToNet(int netid, const char *Msg, int MsgLen)
{
     if (netid < totalnet && netid >=0)
         SysBroadcastTo(Msg, MsgLen, nets[netid]);
     return 1; 
}

short SysBroadcast(const char *Msg, int MsgLen) 
{ 
    int i;

    for ( i = 0; i < totalnet; i++ )
    {
        SysBroadcastTo(Msg, MsgLen, nets[i]);
    }
    return 1;
}

/****************************************************************
                                                                 
    FUMCTION   DESCRIPTION: Broadcast UDP message 'Msg' to
                            destination 'NetAddr'

    PATAMETERS:     
      INPUT PATAMENTERS
        Msg :  pointer of message which will be sent 
        MsgLen : length of message
        NetAddr : destination address
 
    RETURN CODES                                     
      1 : success
     -1 : call function sendto() return error
     -2 : send service name not defined in etc/services
 
    FUNCTIONS CALLED: SysBroadcast()  
                                                                 
***************************************************************/
short SysBroadcastTo(const char *Msg, int MsgLen, const char *NetAddr)
{
    struct sockaddr_in   To;
    struct servent *ptrServiceEntry;

    if ((ptrServiceEntry = getservbyname(udp_send_port, "udp")) == NULL)
        return -2;
    else
        broadcastPort = ptrServiceEntry->s_port;

    memset(&To, 0, sizeof(To));
    To.sin_family = AF_INET;
    To.sin_port = htons(broadcastPort);
    To.sin_addr.s_addr = inet_addr(NetAddr);

    if (sendto(UDP_Socket, Msg, MsgLen, 0,  
           (struct sockaddr *) &To, sizeof(To)) == -1)
    {
        printf("?!==>Failed to broatcast data, errno is %d\n\n", errno);
        return -1;
    }
    return 1;
}
 
/****************************************************************
                                                                 
    FUMCTION   DESCRIPTION:  Read net config file 

    PATAMETERS:     
      INPUT PATAMENTERS
        fn : filename of net config file
       
    RETURN CODES  
      1 : success
      else : read config file fail                                   
                                                                 
***************************************************************/
short ReadNetCfgFile(char *fn)
{   /*

    int i;
    char fn[MAXSTRLEN];
    FILE *infile;
    char *ptr;

    ptr = getenv("CATSCONFIG");
    
    if (ptr == NULL)
        return -1;  
    if ( strlen(ptr) > (MAXSTRLEN -strlen(netCfgFn)) )
        return -2;

    strcpy(fn, ptr);
    strcat(fn, "/");
    strcat(fn, netCfgFn);  

    infile = fopen(fn, "r");
    if (infile == NULL)
        return -3;
    printf("CONFIG:%s\n", fn);

    fclose(infile);
    return 1;
    */
    
    return ( readprofile(fn));
}  

⌨️ 快捷键说明

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