Select服务器代码实现

         select服务器实现

     服务器端selectserver.c(支持读写逻辑)

     

     

     

     

     客户端selectclient.c

     

     select服务器优缺点:

     优点: 
    (1)select()的可移植性更好,在某些Unix系统上不支持poll() ; 
    (2)select() 对于超时值提供了更好的精度:微秒,而poll是毫秒; 
    (3)select服务器不需要建立多个线程、进程就可以实现一对多的通信; 
    (4)select服务器可以同时等待多个文件描述符,效率比起多进程多线程来说要高很多。

     缺点: 
    (1)每次调用select,都需要把fd集合从用户态拷贝到内核态,这个开销在fd很多时会很大 ; 
    (2)同时每次调用select都需要在内核遍历传递进来的所有fd,这个开销在fd很多时也很大 ; 
    (3)select支持的文件描述符数量太小了,默认是1024。

     但相对于TCP单进程版本、TCP多线程版本和TCP多进程版本,select服务器效率高于以上三个;

     

     

     

     

好的,关于android上传视频到服务器代码实现,以下是一个简单的示例: 1. 在android应用中,首先需要选择一个视频文件并打开它,可以使用Android Intent来实现: ``` Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select a Video "), SELECT_VIDEO); ``` 2. 选择完视频文件后,就需要将其上传到服务器了,可以使用HTTPURLConnection来进行上传: ``` public int uploadVideo(String sourceFileUri) { String upLoadServerUri = "http://example.com/your_php_script.php"; String fileName = sourceFileUri; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int serverResponseCode = 0; try { FileInputStream fileInputStream = new FileInputStream(new File(sourceFileUri)); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileInputStream.read(buffer)) > 0) { dos.write(buffer, 0, bytesRead); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); fileInputStream.close(); dos.flush(); dos.close(); } catch (Exception e) { e.printStackTrace(); } return serverResponseCode; } ``` 这段代码将视频文件通过HTTP POST请求上传到服务器上,需要替换upLoadServerUri为你的服务器地址,同时还需要在你的服务器上编写相应的PHP脚本,来处理上传的视频。 希望这个简单示例能够帮助到你,如果还有什么问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值