udp_socket

udp_client

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main()
{
  int fd;
  char buf[1024];
  struct sockaddr_in addr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if ( fd < 0 )
  {
    printf("socket err\n");
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8888);
  addr.sin_addr.s_addr = inet_addr("192.168.2.11");

  while ( 1 )
  {
    memset(buf, 0x00, sizeof(buf));
    sendto(fd, "hello", strlen("hello"), 0, (struct sockaddr*)&addr, sizeof(addr));
    recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
    printf("buf = %s\n", buf);
  }
}

udp_service

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main()
{
  int fd, cfd, ret;
  char buf[1024];
  struct sockaddr_in addr, client;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if ( fd < 0 )
  {
    printf("socket err\n");
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8888);
  addr.sin_addr.s_addr = inet_addr("192.168.2.11");
  ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
  if ( ret < 0 )
  {
    printf("bind err\n");
  }
  socklen_t len = sizeof(addr);
  while ( 1 )
  {
    memset(buf, 0x00, sizeof(buf));
    cfd = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&client, &len);
    printf("buf = %s\n", buf);
    ret = sendto(fd, "nihao", strlen("nihao"), 0, (struct sockaddr*)&client, len);
    if (ret < 0)
    {
      printf("send err\n");
    }
  }
}

udp用的是recvfrom和sendto函數,第一次參數是一個文件描述符,後面兩個是目的地址/接口,tcp用的是recv和send往socket綁定的地方收發

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化并改编以下代码,使其和原来有部分出入但实现效果相同: 1. import socket 2. 3. 4. def receive(): 5. # 创建套接字 6. udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 7. 8. # 准备数据9. file_name = input("Please input the save file name:") 10. 11. # 发送数据 12. ip = input("Please input the sender's ipv4 address:") 13. udp_socket.sendto(file_name.encode('gbk'), (ip, 7788)) 14. 15. # 接收数据 16. recv_data = udp_socket.recvfrom(1024) 17. file_data = recv_data[0] 18. with open(file_name, 'wb') as f: 19. f.write(file_data) 20. print("Receive successfully!") 21. # 关闭套接字 22. udp_socket.close() 23. 24. 25.def send(): 26. # 创建套接字 27. udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 28. 29. # 绑定本地信息 30. localaddr = ('', 7788) 31. udp_socket.bind(localaddr) 32. 33. # 接收数据 34. while True: 35. recv_data = udp_socket.recvfrom(1024) 36. recv_msg = recv_data[0] 37. send_addr = recv_data[1] 38. print("%s:%s" % (str(send_addr), recv_msg.decode('gbk'))) 39. 40. # 读取文件并传输文件 41. with open(recv_msg.decode('gbk'), 'rb') as f: 42. file_data = f.read() 43. udp_socket.sendto(file_data, send_addr) 44. 45. print("Send successfully!") 46. break 47. 48. # 关闭套接字 49. udp_socket.close() 50. 51. 52.if name == 'main': 3553. while True: 54. answer = input("This is a simple program relying on the Udp protocol, \nif you want to send the file," 55. "please input 1,\n if you want to receive th e file, please input 2, \n if you want exit, " 56. "please input 0:\n") 57. if answer == '0': 58. break 59. if answer == '1': 60. send() 61. if answer == '2': 62. receive()
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值