UDP socket实例

Server
#include <lwip/sockets.h> 

#define RECEIVER_PORT_NUM 6001 
#define RECEIVER_IP_ADDR "192.136.23.21" 

void main(void)
{

int socket_fd;
struct sockaddr_in sa,ra;

int recv_data; char data_buffer[80]; 
/* Creates an UDP socket (SOCK_DGRAM) with Internet Protocol Family (PF_INET).
 * Protocol family and Address family related. For example PF_INET Protocol Family and AF_INET family are coupled.
*/

socket_fd = socket(PF_INET, SOCK_DGRAM, 0);

if ( socket_fd < 0 )
{

printf("socket call failed");
exit(0);
}

memset(&sa, 0, sizeof(struct sockaddr_in));
ra.sin_family = AF_INET;
ra.sin_addr.s_addr = inet_addr(RECEIVER_IP_ADDR);
ra.sin_port = htons(RECEIVER_PORT_NUM);


/* Bind the UDP socket to the port RECEIVER_PORT_NUM and to the current
* machines IP address (Its defined by RECEIVER_PORT_NUM).
* Once bind is successful for UDP sockets application can operate
* on the socket descriptor for sending or receiving data.
*/
if (bind(socket_fd, (struct sockaddr *)&ra, sizeof(struct sockaddr_in)) == -1)
{

printf("Bind to Port Number %d ,IP address %s failed\n",RECEIVER_PORT_NUM,RECEIVER_IP_ADDR);
close(socket_fd);
exit(1);
}
/* RECEIVER_PORT_NUM is port on which Server waits for data to
* come in. It copies the received data into receive buffer and
* prints the received data as string. If no data is available it 
* blocks.recv calls typically return any availbale data on the socket instead of waiting for the entire data to come.
*/

recv_data = recv(socket_fd,data_buffer,sizeof(data_buffer),0);
if(recv_data > 0)
{

data_buffer[recv_data] = '\0';
printf("%s\n",data_buffer);
} 
close(socket_fd);
}
UDP Client Server Example

Client
#include <lwip/sockets.h> 

#define SENDER_PORT_NUM 6000 
#define SENDER_IP_ADDR "192.136.23.20"
#define RECEIVER_PORT_NUM 6001 
#define RECEIVER_IP_ADDR "192.136.23.21" 

void main(void)
{

int socket_fd;
struct sockaddr_in sa;

int sent_data; char recv_data_buffer[80]; 
/* Creates an UDP socket (SOCK_DGRAM) with Internet Protocol Family (PF_INET).
 * Protocol family and Address family related. For example PF_INET Protocol Family and AF_INET family are coupled.
*/
socket_fd = socket(PF_INET, SOCK_DGRAM, 0);

if ( socket_fd < 0 )
{

printf("socket call failed");
exit(0);
}

memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(SENDER_IP_ADDR);
sa.sin_port = htons(SENDER_PORT_NUM);


/* Bind the TCP socket to the port SENDER_PORT_NUM and to the current
* machines IP address (Its defined by SENDER_IP_ADDR). 
* Once bind is successful for UDP sockets application can operate
* on the socket descriptor for sending or receiving data.
*/
if (bind(socket_fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == -1)
{
printf("Bind to Port Number %d ,IP address %s failed\n",SENDER_PORT_NUM,SENDER_IP_ADDR);
close(socket_fd);
exit(1);
}

memset(&ra, 0, sizeof(struct sockaddr_in));
ra.sin_family = AF_INET;
ra.sin_addr.s_addr = inet_addr(RECEIVER_IP_ADDR);
ra.sin_port = htons(RECEIVER_PORT_NUM);


strcpy(data_buffer,"Hello World");
sent_data = sendto(socket_fd, data_buffer,sizeof("Hello World",0,(struct sockaddr*)&ra,sizeof(ra)));
if(sent_data < 0)
{
printf("send failed\n");
close(socket_fd);
exit(2);
} 
close(socket_fd);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值