📄 socketclientudp.c
字号:
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define ECHOMAX 255 /* Longest string to echo */
//void DieWithError(char "message can't b sent"); /* External error handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
char *echoString; /* String to send to echo server */
char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */
int echoStringLen; /* Length of string to echo */
int respStringLen; /* Length of received response */
if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr,"Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
//exit(1);
}
servIP="192.168.0.138";
echoString="你好!啊";
printf("servIP:%s\r\n",servIP);
printf("echoString:%s\r\n",echoString);
//servIP = argv[1]; /* First arg: server IP address (dotted quad) */
//echoString = argv[2]; /* Second arg: string to echo */
if ((echoStringLen = strlen(echoString)) > ECHOMAX) /* Check input length */
printf("word too length!\r\n");
echoServPort = 8000; /* 7 is the well-known port for the echo service */
/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
printf("socket fail!\r\n");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Send the string to the server */
if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr)) != echoStringLen)
printf("sendto fail!\r\n");
/* Recv a response */
fromSize = sizeof(fromAddr);
if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0,
(struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen)
printf("recv fail!\r\n");
if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
{
fprintf(stderr,"Error: received a packet from unknown source.\n");
exit(1);
}
/* null-terminate the received data */
echoBuffer[respStringLen] = '\0';
printf("respStringLen:%d\n\r",respStringLen);
printf("Received: %s\n", echoBuffer); /* Print the echoed arg */
close(sock);
exit(0);
/*
struct timeval val;
fcntl(sock, F_SETFL, O_NONBLOCK);
while (1) {
FD_ZERO(&fdset);
FD_SET(sock, &fdset);
val.tv_sec = 1;
val.tv_usec = 0;
if (select(sock + 1, &fdset, NULL, NULL, &val) < 0) {
printf("select error: ");
continue;
}
if (FD_ISSET(sock, &fdset)) {
fromSize = sizeof(fromAddr);
respStringLen = recvfrom (sock, echoBuffer, sizeof(echoBuffer), 0,
(struct sockaddr *)&fromAddr, &fromSize);
if (respStringLen <= 0) {
p_error("socket closed by remote client");
close(sock);
exit(0);
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -