Socket communication programming for Linux system UDP

Source: Internet
Author: User

Sender:

/*
* FILE:MAIN.C
* Author:tianshuai
*
* Created on November 29, 2011, 10:34
*
* Main implementation: Send 20 text messages, and then send a termination message
*/

#include <stdio.h>
#include <stdlib.h>

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

int port=6789;
int main (int argc, char** argv) {
int socket_descriptor; Set interface Description Word
int iter=0;
Char buf[80];
struct sockaddr_in address;//address for handling network traffic

Bzero (&address,sizeof (address));
Address.sin_family=af_inet;
ADDRESS.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1");//It's not the same here.
Address.sin_port=htons (port);

Create a UDP socket

Socket_descriptor=socket (af_inet,sock_dgram,0);//ipv4 sock_dgram datagram Sockets (UDP protocol)

for (iter=0;iter<=20;iter++)
{
/*
* SPRINTF (S, "%8d%8d", 123, 4567); Generated: "123 4567"
* After formatting to a string into the S
*/
sprintf (BUF, "Data packet with ID%d\n", ITER);

/*int PASCAL far sendto (SOCKET s, const char far* buf, int len, int. flags,const struct sockaddr far* to, int tolen);
* S: A descriptive word for the socket interface.
* BUF: The buffer containing the data to be sent.
* Len:buf The length of the data in the buffer.
* Flags: Call way flag bit.
* To: (optional) pointer to the address of the destination socket interface.
* The length of the address referred to by tolen:to.
*/
SendTo (Socket_descriptor,buf,sizeof (BUF), 0, (struct sockaddr *) &address,sizeof (address));
}

sprintf (buf, "stop\n");
SendTo (Socket_descriptor,buf,sizeof (BUF), 0, (struct sockaddr *) &address,sizeof (address));//Send Stop command
Close (Socket_descriptor);
printf ("Messages sent,terminating\n");

Exit (0);

return (exit_success);
}

Receiving Party:

/*
* FILE:MAIN.C
* Author:tianshuai
*
* Created on November 29, 2011, 10:34
*/

#include <stdio.h>
#include <stdlib.h>

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

int port=6789;

int main (int argc, char** argv) {

int Sin_len;
Char message[256];

int socket_descriptor;
struct sockaddr_in sin;
printf ("Waiting for data form sender \ n");

Bzero (&sin,sizeof (sin));
Sin.sin_family=af_inet;
Sin.sin_addr.s_addr=htonl (Inaddr_any);
Sin.sin_port=htons (port);
Sin_len=sizeof (SIN);

Socket_descriptor=socket (af_inet,sock_dgram,0);
Bind (Socket_descriptor, (struct sockaddr *) &sin,sizeof (sin));

while (1)
{
Recvfrom (socket_descriptor,message,sizeof (message), 0, (struct sockaddr *) &sin,&sin_len);
printf ("Response from server:%s\n", message);
if (strncmp (message, "Stop", 4) = = 0)//The message received is "stop"
{

printf ("Sender has told me to end the connection\n");
Break
}
}

Close (Socket_descriptor);
Exit (0);

return (exit_success);
}


Socket communication programming for Linux system UDP

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.