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. }
复制代码
. .
今天看到群里的老秦同学在布局自己的网站项目,这个同学还是比较奇怪的,他就喜欢用这些奇怪的域名。比如前几天看到有用.in域名,个人网站他用的.me域名不奇怪,这个还是常见的。今天看到他在做的一个范文网站的域名,居然用的是 .asia 后缀。问到其理由,是有不错好记的前缀。这里简单的搜索到.ASIA域名的新注册价格是有促销的,大约35元首年左右,续费大约是80元左右,这个成本算的话,比COM域名还贵。...
欧路云怎么样?欧路云主要运行弹性云服务器,可自由定制配置,可选加拿大的480G超高防系列,也可以选择美国(200G高防)系列,也有速度直逼内地的香港CN2系列。所有配置都可以在下单的时候自行根据项目 需求来定制自由升级降级 (降级按天数配置费用 退款回预存款)。2021年7月14日美国 CERA 弹性云服务器 上新 联通CUVIP 线路!8折特惠中!点击进入:欧路云官方网站地址付款方式:PayPa...
wordpress高级全行业大气外贸主题,wordpress通用全行业高级外贸企业在线询单自适应主题建站程序,完善的外贸企业建站功能模块 + 高效通用的后台自定义设置,更实用的移动设备特色功能模块 + 更适于欧美国外用户操作体验 大气简洁的网站风格设计 + 高效优化的网站程序结构,更利于Goolge等SEO搜索优化和站点收录排名。点击进入:wordpress高级全行业大气外贸主题主题价格:¥398...