1. udp_socket_发送数据

//udp_socket_发送数据

#include <stdio.h>  
#include<sys/socket.h>//socket  
#include<unistd.h>//close  
#include<netinet/in.h>//struct sockaddr_in  
#include<string.h>//strlen bzero  
#include<arpa/inet.h>//htons  inet_pton  
int main(int argc, char const *argv[])  
{  
    //创建一个用于UDP通信的套接字sockfd  
    //AF_INET IPv4  AF_INET6 IPv6  
    //SOCK_DGRAM udp编程     
    //0 自动识别协议类型  
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
    if(sockfd < 0)  
    {  
        perror("scoket");  
        return 0;  
    }  
  
    printf("sockfd = %d\n", sockfd);  
  
    //使用sendto  10.0.122.200   8080 发送udp数据  
    //定义一个ipv4套接字地址结构  
    struct sockaddr_in dstAddr;  
    //先将dstAddr清0  
    //memset(&dstAddr, 0, sizeof(dstAddr));  
    bzero(&dstAddr, sizeof(dstAddr));  
    //赋值协议  
    dstAddr.sin_family = AF_INET;//IPv4协议  
    //赋值 目的端口 8080  
    dstAddr.sin_port = htons(8080);  
    //赋值 目的IP 10.0.122.200  
    inet_pton(AF_INET, "10.0.122.200", &dstAddr.sin_addr.s_addr);  
  
  
    char msg[] = "hello udp data";  
    //如果 sockfd没有认为指定端口 ip 那么在第一次调用sendto的时候系统会自动添加随机端口 和自身IP  
    sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *)&dstAddr, sizeof(dstAddr));  
  
    //关闭套接字  
    close(sockfd);  
    return 0;  
}  
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值