TCP协议服务端多线程技术
代码
UploadTask. java
1. import java.net.Socket;
2. import java. io. InputStream;
3. import java. io.FileOutputStream;
4. import java. io.File;
5. import java. io.OutputStream;
6.
7. public class UploadTask implements Runnable
8. {
9. private Socket s;
10.
11. public UploadTask(Socket s) {
12. this. s = s;
13. }
14.
15. public void run() {
16.
17. int count = 0;
18.
19. String ip = s.getInetAddress() .getHostAddress() ;
20. System.out.println(ip + ". . . . . .connected") ;
21.
22. try{
23. InputStream in = s.getInputStream() ;
24.
25. File dir = new File("c:\\pic") ;
26. if(!dir.exists() ) {
27. dir.mkdirs() ;
28. }
29. File file = new File(dir, ip + ".bmp") ;
30.
31. //如果文件已经存在于服务端。
32. while(file.exists() ) {
33. file = new File(dir, ip + "(" + (++count) + ") .bmp") ;
34. }
35.
36. FileOutputStream fos = new FileOutputStream(file) ;
37.
38. byte[] buf = new byte[1024] ;
39.
40. int len = 0;
41.
42. while( (len = in.read(buf) ) != -1) {
43. fos.write(buf,0, len) ;
44. }
45.
46. OutputStream out = s.getOutputStream() ;
47.
48. out.write("上传成功".getBytes() ) ;
49.
50. fos. close() ;
51. s. close() ;
52. }catch(Exception e) {
53. e.printStackTrace() ;
54. }
55. }
56. }
复制代码
UploadServerDemo. java
1. import java. io. IOException;
2. import java.net.ServerSocket;
3. import java.net.Socket;
4. import java. io. InputStream;
5. import java. io.File;
6. import java. io.FileOutputStream;
7. import java. io.OutputStream;
8. import java. io.FileNotFoundException;
9. import java. io. IOException;
10.
11. public class UploadPicServer
12. {
13. public static voidmain(String[] args) throws FileNotFoundException, IOException{
14.
15. ServerSocket ss = new ServerSocket(10006) ;
16.
17. while(true) {
18. Socket s = ss.accept() ;
19. new Thread(new UploadTask(s) ) . start() ;
20. }
21.
22. //ss. close() ;
23. }
24. }
复制代码
. .
RAKsmart 商家从原本只有专注于独立服务器后看到产品线比较单薄,后来陆续有增加站群服务器、高防服务器、VPS主机,以及现在也有在新增云服务器、裸机云服务器等等。机房也有增加到拥有洛杉矶、圣何塞、日本、韩国、中国香港等多个机房。在年前也有介绍到RAKsmart商家有提供年付129元的云服务器套餐,年后我们看到居然再次刷新年付云服务器低价格。我们看到云服务器低至年79元,如果有需要便宜云服务器的...
TmhHost 商家是一家成立于2019年的国人主机品牌。目前主营的是美国VPS以及美国、香港、韩国、菲律宾的独立服务器等,其中VPS业务涵盖香港CN2、香港NTT、美国CN2回程高防、美国CN2 GIA、日本软银、韩国cn2等,均为亚太中国直连优质线路,TmhHost提供全中文界面,支持支付宝付款。 TmhHost黑五优惠活动发布了,全场云服务器、独立服务器提供8折,另有充值返现、特价服务器促销...
官方网站:点击访问星梦云活动官网活动方案:机房CPU内存硬盘带宽IP防护流量原价活动价开通方式成都电信优化线路4vCPU4G40G+50G10Mbps1个100G不限流量210元/月 99元/月点击自助购买成都电信优化线路8vCPU8G40G+100G15Mbps1个100G不限流量370元/月 160元/月点击自助购买成都电信优化线路16vCPU16G40G+100G20Mb...