Sends a UDP packet to the specified port on the specified server

Source: Internet
Author: User
Tags strtok htons

Function: Reads a file file and sends a UDP packet to the specified port on the specified server.
2015.9.26 OK

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>

#define SERVER_PORT 8000
int server_port = 8000;

int getserveraddr (char * Addrname,char *addrport)
{
Char sztemp[512]={};
Char ipaddr[512]={};
Char port[512]={};
Char *CFA;

FILE *file_fa =fopen ("./send.conf", "r+");
if (file_fa==null)//if it fails
{
printf ("Open send.conf Error! \ n ");
Exit (1); Abort Program
}

if ((Fgets (SZTEMP,255,FILE_FA)) = NULL)//read one line at a time
{
printf ("sztemp=%s\n", sztemp);

CFA = Strtok (Sztemp, "");
sprintf (port, "%s", CFA);
CFA = Strtok (NULL, "");
sprintf (ipaddr, "%s", CFA);
CFA = Strtok (NULL, "");
sprintf (port, "%s", CFA);

printf ("ipaddr=%s port=%s \ n", ipaddr,port);

}
sprintf (Addrname, "%s", ipaddr);
sprintf (Addrport, "%s", port);
Fclose (FILE_FA);
return 1;
}
int main (int argc,char **argv)
{
int CLI_SOCKFD;
int Len;
Socklen_t Addrlen;
Char seraddr[14];
Char serport[14];
struct sockaddr_in cli_addr;
Char buffer[256];

GETSERVERADDR (Seraddr,serport);
printf ("getserveraddr=%s,port=%s\n", Seraddr,serport);

/* Build socket*/
Cli_sockfd=socket (af_inet,sock_dgram,0);
if (cli_sockfd<0)
{
printf ("I cannot socket success\n");
return 1;
}

/* Fill in sockaddr_in*/
addrlen=sizeof (struct sockaddr_in);
Bzero (&cli_addr,addrlen);
Cli_addr.sin_family=af_inet;
CLI_ADDR.SIN_ADDR.S_ADDR=INET_ADDR (SERADDR);//Convert string to IPV4 address of 32-bit binary network byte order
Cli_addr.sin_addr.s_addr=htonl (Inaddr_any);
Cli_addr.sin_port=htons (Atoi (Serport));//host byte order translates to network byte order
Cli_addr.sin_port=htons (Server_port);

Bzero (buffer,sizeof (buffer));
/* Get string from standard input device */
Len=read (stdin_fileno,buffer,sizeof (buffer));
sprintf (buffer, "%s", "Nihao");
len = sizeof (buffer) +1;
/* Send string to server side */
while (1)
{
printf ("sendword=%s\n", buffer);
SendTo (cli_sockfd,buffer,len,0, (struct sockaddr*) &cli_addr,addrlen);
Sleep (2);
}
/* Receive the server-side returned string */
Len=recvfrom (cli_sockfd,buffer,sizeof (buffer), 0, (struct sockaddr*) &cli_addr,&addrlen);
printf ("Receive from%s\n", Inet_ntoa (CLI_ADDR.SIN_ADDR));
printf ("Receive:%s", buffer);
Close (CLI_SOCKFD);
}

Sends a UDP packet to the specified port on the specified server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.