代码拉取完成,页面将自动刷新
/*
* File: udp-client.c
* Author: Songqing Hua
*
* (C) 2023 Songqing Hua.
* https://blog.csdn.net/whowin/
*
* TCP client for a combinatorial TCP and UDP server
* Compiled with gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1).
* Tested on Linux 5.4.0-139-generic #156-Ubuntu SMP Fri Jan 20 17:27:18 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux.
*
* To compile: $ gcc -Wall udp-client.c -o udp-client
* Usage: $ ./udp-client
*
* Example source code for article 《使用select实现的UDP/TCP组合服务器》
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVER_IP "192.168.2.112"
#define PORT 5000
#define BUF_SIZE 1024
int main() {
int sockfd;
char buffer[BUF_SIZE];
char *message = "Hello Server";
struct sockaddr_in server_addr;
int n, len;
// Step 1: Creating socket file descriptor
//=========================================
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
// Step 2: Send a message to the server
//======================================
memset(&server_addr, 0, sizeof(server_addr));
// Filling server information
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
// send hello message to server
sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr));
// Step 3: Wait until a response from the server is received
//===========================================================
// receive server's response
len = sizeof(struct sockaddr_in);
n = 0;
memset(buffer, 0, BUF_SIZE);
n = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *)&server_addr, (socklen_t *)&len);
buffer[n] = '\0';
printf("Message from server: %s\n", buffer);
// Step 4: Close socket descriptor and exit
//==========================================
close(sockfd);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。