[Linux] applet: UDP The client transmits multiple strings to the server

Source: Internet
Author: User
Tags server port htons

Or the homework of the experiment ~

Total two files required, this is the client

1#include <stdio.h>2#include <sys/socket.h>3#include <arpa/inet.h>4#include <stdlib.h>5#include <string.h>6#include <unistd.h>7 8 #defineEchomax 2559 Ten intMainintargcChar*argv[]) { One     intsock; A     structsockaddr_in echoservaddr; -     structsockaddr_in fromaddr; -Unsigned ShortEchoservport; theUnsignedintfromsize; -     Char*Servip; -     Char*echostring; -     Charechobuffer[echomax+1]; +     intechostringlen=0; -     intRespstringlen; +     intI//For counting A  at     if(argc<3) { -printf"usage:%s <server ip> <echo port> [<echo words>]\n", argv[0]); -Exit1); -     } -  -SERVIP = argv[1]; in      -      for(i=3; i<argc;i++){ toEchostringlen = Echostringlen +strlen (Argv[i]); +     } -     if(Echostringlen >Echomax) theprintf"Echo Word too long.\n"); *  $Echoservport = Atoi (argv[2]);Panax Notoginseng  -     if((sock = socket (pf_inet, SOCK_DGRAM, IPPROTO_UDP) <0) theprintf"socket () failed.\n"); +memset (&AMP;ECHOSERVADDR,0,sizeof(ECHOSERVADDR)); Aechoservaddr.sin_family =af_inet; theECHOSERVADDR.SIN_ADDR.S_ADDR =inet_addr (SERVIP); +Echoservaddr.sin_port =htons (echoservport); -  $      for(i=3; i<argc;i++) { $echostring =Argv[i]; -         if(SendTo (Sock,echostring,strlen (echostring),0,(structSOCKADDR *) &echoservaddr,sizeof(ECHOSERVADDR))) !=strlen (echostring)) -printf"sendto () sent a different number of bytes than expected.\n"); the         } -     }WuyiEchobuffer[strlen (echostring)] =' /'; the Close (sock); -Exit0); Wu } -     

This is the server

#include <stdio.h>#include<sys/socket.h>#include<arpa/inet.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#defineEchomax 255intMainintargcChar*argv[]) {    intsock; structsockaddr_in echoservaddr; structsockaddr_in echoclntaddr; unsignedintCliaddrlen; CharEchobuffer[echomax]; unsigned ShortEchoservport; intrecvmsgsize; intI//For counting    if(ARGC! =2) {printf ("Usage:%s <udp SERVER port>\n", argv[0]); Exit (1); } echoservport= Atoi (argv[1]); if(sock = socket (Pf_inet,sock_dgram,0)) <0) printf ("socket () failed.\n"); memset (&AMP;ECHOSERVADDR,0,sizeof(ECHOSERVADDR)); Echoservaddr.sin_family=af_inet; EchoServAddr.sin_addr.s_addr=htonl (Inaddr_any); Echoservaddr.sin_port=htons (Echoservport); if(Bind (sock, (structSOCKADDR *) &echoservaddr,sizeof(ECHOSERVADDR))) <0) printf ("bind () failed.\n");  for (;;) {Cliaddrlen=sizeof(ECHOCLNTADDR); if(Recvmsgsize = Recvfrom (Sock,echobuffer,strlen (Echobuffer),0,(structSOCKADDR *) &echoclntaddr,&cliaddrlen) <0) printf ("recvfrom () failed.\n"); printf ("Handling Client%s\n", Inet_ntoa (ECHOCLNTADDR.SIN_ADDR)); printf ("Message:");  for(i=0; i<recvmsgsize;i++) {printf ("%c", Echobuffer[i]); if(i== (recvmsgsize-1)) printf ("\ n"); }} printf ("\ n");}

Examples of Use:

For example, the string you want to transfer is Hello, world!

Then you need to run server first:

./server 1500

1500 is the port number (in fact, under the permission of ordinary users, using a >1024 port can be)

Then run client: (press ALT+F2 to switch to a new window to run)

./client 127.0.0.1-Hello, world!

Then alt+f1 back to the original window and you can see:

Handling client 127.0.0.1

Message:hello,

Handling client 127.0.0.1

message:world!

[Linux] applet: UDP The client transmits multiple strings to the 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.