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. }
复制代码
. .
昔日数据,国内商家,成立于2020年,主要销售湖北十堰和香港HKBN的云服务器,采用KVM虚拟化技术构架,不限制流量。当前夏季促销活动,全部首月5折促销,活动截止于8月11日。官方网站:https://www.xrapi.cn/5折优惠码:XR2021湖北十堰云服务器托管于湖北十堰市IDC数据中心,母鸡采用e5 2651v2,SSD MLC企业硬盘、 rdid5阵列为数据护航,100G高防,超出防...
RAKsmart怎么样?RAKsmart香港机房新增了付费的DDoS高防保护服务,香港服务器默认接入20Mbps的大陆优化带宽(电信走CN2、联通和移动走BGP)。高防服务器需要在下单页面的IP Addresses Option里面选择购买,分:40Gbps大陆优化高防IP-$461/月、100Gbps国际BGP高防IP-$692/月,有兴趣的可以根据自己的需求来选择!点击进入:RAKsmart官...
imidc怎么样?imidc彩虹数据或彩虹网络现在促销旗下日本多IP站群独立服务器,原价159美元的机器现在只需要88美元,而且给13个独立IPv4,30Mbps直连带宽,不限制月流量!IMIDC又名为彩虹数据,rainbow cloud,香港本土运营商,全线产品都是商家自营的,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非...