After the UDP client establishes the socket, it will directly send data with sendto function, and there is also an operation implied,
That is, before sending data, UDP will first select a separate UDP port for the socket (at 1024
-5000), set the socket to the bound state. If a UDP client establishes a socket
First, the bind function is used to indicate the local address / port, which can also be used to force UDP to use the pointer
Send data to the specified port. (in fact, UDP doesn't matter about servers and clients. The boundary here is already modeled
It's burnt.
/ / server side
#include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #define MAXLINE 80 #define SERV_PORT 502 void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; for(;;) { len = clilen; /* waiting for receive data */ n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); mesg[n-1]=0; printf("recvfrom data:%s\n",mesg); /* sent data back to client */ sendto(sockfd, mesg, n, 0, pcliaddr, len); } } int main(void) { int sockfd; struct sockaddr_in servaddr, cliaddr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */ /* init servaddr */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); /* bind address and port to socket */ if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { perror("bind error"); exit(1); } printf("UPD servese is bind success and do_echo now\n"); do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)); return 0; } //============================================= //Client 1 uses local fixed port #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h> #include <unistd.h> #define MAXLINE 80 #define SERV_PORT 8888 void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; /* connect to server */ if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1) { perror("connect error"); exit(1); } printf("Please input your str want to send:\n"); while(fgets(sendline, MAXLINE, fp) != NULL) { /* read a line and send to server */ write(sockfd, sendline, strlen(sendline)); /* receive data from server */ n = read(sockfd, recvline, MAXLINE); if(n == -1) { perror("read error"); exit(1); } recvline[n] = 0; /* terminate string */ printf("receive str:\n"); fputs(recvline, stdout); printf("\n==================================\n"); printf("Please input your str want to send:\n"); } } int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; /* check args */ if(argc != 2) { printf("usage: udpclient <IPaddress>\n"); exit(1); } /* init servaddr */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) { printf("[%s] is not a valid IPaddress\n", argv[1]); exit(1); } sockfd = socket(AF_INET, SOCK_DGRAM, 0); printf("cli is ready,now do funtion do_cli! \n", argv[1]); do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); return 0;
}
//==================================
//Client 2 code uses random port //http://haibor1x.blog.163.com/blog/static/763407200751052458572/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define SERV_PORT 502 #define MAXLINE 80 void do_cli(FILE *fp, int sockfd, sockaddr_in toAddr) { int n; struct sockaddr_in fromAddr; char sendline[MAXLINE], recvline[MAXLINE + 1]; printf("Please input your str want to send:\n"); while(fgets(sendline, MAXLINE, fp) != NULL) { /* read a line and send to server */ //write(sockfd, sendline, strlen(sendline)); if(sendto(sockfd,sendline,strlen(sendline),0, (struct sockaddr*)&toAddr,sizeof(toAddr)) != strlen(sendline)) { printf("sendto() Function use failed.\r\n"); close(sockfd); exit(1); } printf("wait for data ...\n"); /* receive data from server */ //n = read(sockfd, recvline, MAXLINE); unsigned int fromLen = sizeof(fromAddr); if(recvfrom(sockfd,recvline,MAXLINE,0, (struct sockaddr*)&fromAddr,&fromLen)<0) { printf(" ()recvfrom()Function use failed.\r\n"); close(sockfd); exit(1); } else{ printf("get nothing data ...\n"); } recvline[strlen(sendline)-1] = 0; printf("receive str:\n"); fputs(recvline, stdout); printf("\n==================================\n"); printf("Please input your str want to send:\n"); } } int main(int argc, char *argv[]) { int sock; //Opposite address used in sendto struct sockaddr_in toAddr; //Opposite host address used in recvfrom struct sockaddr_in fromAddr; sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(sock < 0) { printf(" Failed to create socket.\r\n"); exit(1); } memset(&toAddr,0,sizeof(toAddr)); toAddr.sin_family=AF_INET; toAddr.sin_addr.s_addr=inet_addr("192.168.0.110"); toAddr.sin_port = htons(SERV_PORT); do_cli(stdin, sock, toAddr); close(sock); return 0; }