java sendfile_Java Socket.sendfilen方法代码示例

import org.apache.tomcat.jni.Socket; //导入方法依赖的package包/类

/**

* Add the sendfile data to the sendfile poller. Note that in most cases,

* the initial non blocking calls to sendfile will return right away, and

* will be handled asynchronously inside the kernel. As a result,

* the poller will never be used.

*

* @param data containing the reference to the data which should be snet

* @return true if all the data has been sent right away, and false

* otherwise

*/

public boolean add(SendfileData data) {

// Initialize fd from data given

try {

data.fdpool = Socket.pool(data.socket);

data.fd = File.open

(data.fileName, File.APR_FOPEN_READ

| File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,

0, data.fdpool);

data.pos = data.start;

// Set the socket to nonblocking mode

Socket.timeoutSet(data.socket, 0);

while (true) {

long nw = Socket.sendfilen(data.socket, data.fd,

data.pos, data.end - data.pos, 0);

if (nw < 0) {

if (!(-nw == Status.EAGAIN)) {

Pool.destroy(data.fdpool);

data.socket = 0;

return false;

} else {

// Break the loop and add the socket to poller.

break;

}

} else {

data.pos = data.pos + nw;

if (data.pos >= data.end) {

// Entire file has been sent

Pool.destroy(data.fdpool);

// Set back socket to blocking mode

Socket.timeoutSet(

data.socket, getSoTimeout() * 1000);

return true;

}

}

}

} catch (Exception e) {

log.warn(sm.getString("endpoint.sendfile.error"), e);

return false;

}

// Add socket to the list. Newly added sockets will wait

// at most for pollTime before being polled

synchronized (this) {

addS.add(data);

this.notify();

}

return false;

}

优化并改编以下代码,使其和原来有部分出入但实现效果相同: 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、付费专栏及课程。

余额充值