#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define SERV_PORT 25555 //服務(wù)器接聽端口號(hào) #define BACKLOG 20 //請(qǐng)求隊(duì)列中允許請(qǐng)求數(shù) #define BUF_SIZE 256 //緩沖區(qū)大小 int main(int argc, char *argv[]) { int ret; time_t tt; struct tm *ttm; char buf[BUF_SIZE]; int sockfd; //定義sock描述符 struct sockaddr_in serv_addr; //服務(wù)器IP地址和端口信息 if(argc != 2) { printf("命令行輸入有誤\n"); //命令行帶IP return 1; } /**創(chuàng)建套接字**/ sockfd = socket(AF_INET, SOCK_DGRAM, 0); //TCP/IP協(xié)議,數(shù)據(jù)流套接字 if(sockfd == -1) //判斷socket函數(shù)的返回值 { printf("調(diào)用socket函數(shù)創(chuàng)建鏈接失敗.\n"); return 0; } /**建立連接**/ bzero(&serv_addr, sizeof serv_addr); serv_addr.sin_family = AF_INET; //TCP/IP協(xié)議 serv_addr.sin_port = htons(SERV_PORT); //設(shè)定端口號(hào) //serv_addr.sin_addr.s_addr = INADDR_ANY; //使用回環(huán)地址 127.0.0.1 inet_aton(argv[1], (struct sockaddr *)&serv_addr.sin_addr.s_addr); //設(shè)定IP地址 while(1) { bzero(buf, sizeof buf); //首先清除緩沖區(qū) tt = time(NULL); ttm = localtime(&tt); strcpy(buf, asctime(ttm)); //拷貝緩沖區(qū)數(shù)據(jù) sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof serv_addr); //接收數(shù)據(jù),然后放入緩沖區(qū) sleep(2); } close(sockfd); return 0; }
當(dāng)前文章:[Linux網(wǎng)絡(luò)編程]UDP編程--UDP通信程序客戶端
鏈接地址:http://muchs.cn/article32/pdjepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、動(dòng)態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、虛擬主機(jī)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)