TCP使用(二)多用户发送文件和登录
2013-06-24 08:40
267 查看
服务器端接收多个客户端同时发送的文件
血的教训:
在服务器端未添加while (true) {Socket s = ss.accept();new Thread(new MyThread(s)).start();}
导致程序运行的次,服务器端就关闭
客户端:
服务器端:
TCP验证多用户登录:
示例:
说明:服务器端同时验证多个用户登录,限制登录三次,客户端控制台输入用户名进行登录
血的教训:
在使用printWriter的时候 new PrintWriter(os,true);第二个值为true的时候,使用方法println()会自动刷入数据。
如果使用writer()方法还需要flush()手动刷入,并且该方法无法自动加入换行符
客户端:
服务器端:
血的教训:
在服务器端未添加while (true) {Socket s = ss.accept();new Thread(new MyThread(s)).start();}
导致程序运行的次,服务器端就关闭
客户端:
public static void main(String[] args)throws Exception { File file = new File("c:\\QQ.jpg"); if(!(file.exists() && file.isFile())) { return ; } if(!file.getName().endsWith(".jpg")) { return ; } if(file.length()>1024*1024*5) { return ; } Socket s = new Socket("192.168.1.117",10009); FileInputStream fis = new FileInputStream(file); OutputStream out = s.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1) { out.write(buf,0,len); } s.shutdownOutput(); InputStream in = s.getInputStream(); byte[] bufIn = new byte[1024]; int num = in.read(bufIn); System.out.println(new String(bufIn,0,num)); fis.close(); s.close(); }
服务器端:
public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(10009); while (true) { Socket s = ss.accept(); new Thread(new MyThread(s)).start(); } }
public class MyThread implements Runnable { Socket s; public MyThread(Socket s) { super(); this.s = s; } public void run() { int count = 1; String ip = s.getInetAddress().getHostAddress(); try { System.out.println(ip + "....connected"); InputStream in = s.getInputStream(); File dir = new File("d:\\pic"); dir.mkdirs(); File file = new File(dir, ip + "(" + (count) + ")" + ".jpg"); while (file.exists()) file = new File(dir, ip + "(" + (count++) + ")" + ".jpg"); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { fos.write(buf, 0, len); } OutputStream out = s.getOutputStream(); out.write("上传成功".getBytes()); fos.close(); s.close(); } catch (Exception e) { throw new RuntimeException(ip + "上传失败"); } } }
TCP验证多用户登录:
示例:
说明:服务器端同时验证多个用户登录,限制登录三次,客户端控制台输入用户名进行登录
血的教训:
在使用printWriter的时候 new PrintWriter(os,true);第二个值为true的时候,使用方法println()会自动刷入数据。
如果使用writer()方法还需要flush()手动刷入,并且该方法无法自动加入换行符
客户端:
public class Demo25 { public static void main(String[] args) throws IOException { try { Socket s = new Socket("192.168.1.117", 10009); BufferedReader bwIn = new BufferedReader(new InputStreamReader(s .getInputStream())); BufferedReader brSys = new BufferedReader(new InputStreamReader( System.in)); BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s .getOutputStream())); for (int i = 0; i < 3; i++) { String name = brSys.readLine(); System.out.print(name); if (name == null) { System.out.println("用户名不能为空"); } bwOut.write(name); bwOut.newLine(); bwOut.flush(); String request = bwIn.readLine(); System.out.println(request); } System.out.println("登录超过三次,自动退出"); // bwOut.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } }
服务器端:
public class Demo26 { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(10009); while (true) { Socket s = ss.accept(); new Thread(new Demo26Thread(s)).start(); } } catch (IOException e) { e.printStackTrace(); } } }
public class Demo26Thread implements Runnable { public Socket s; public Demo26Thread(Socket s) { super(); this.s = s; } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(s .getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream(),true); boolean isLogin = false; for (int i = 0; i < 3; i++) { String name = br.readLine(); System.out.println("Server:" + name); String loginName = null; BufferedReader brFile = new BufferedReader( new InputStreamReader(new FileInputStream( "D:\\login.txt"))); while ((loginName = brFile.readLine()) != null) { isLogin = name.equals(loginName); if (isLogin) { break; } } if (isLogin) { System.out.println(name + "isLogin"); // pw.println("Login Success"); pw.write("Login Success\r\n"); break; } else { pw.write("Login defeated\r\n"); // pw.println("Login defeated"); } pw.flush(); } pw.close(); s.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // TODO Auto-generated method stub } }
相关文章推荐
- 普通域用户登录Windows7提示已使用临时配置文件登录的解决方法
- (101)Tcp网络编程练习:上传文件、上传图片、用户登录
- 用户系列之四:用户登录过程之配置文件的使用详解
- c#--log.txt文件中记录用户登录时使用的浏览器版本
- 使用UPD 和 TCP 发送接收大文件
- 使用触发器实现记录oracle用户登录失败信息到alert.log日志文件
- phpmyadmin登录后显示“使用配置文件中定义的控制用户连接失败。”错误提示
- 备份指定文件到用户主目录,使用tar命令压缩并发送邮件
- 使用root用户登录到AWS EC2服务器,上传文件到/var/www目录
- python-使用用户与密码登录验证的检索特定字段ver1(含文件操作知识)
- win 7 删除了用户文件夹后出现的&quot;你已使用临时配置文件登录&quot;提示的解决方法
- IntelliJ IDEA 请求的操作无法在使用用户映射区域打开的文件上执行
- 【SSH网上商城项目实战25】使用java email给用户发送邮件
- Oracle sqldevelop的使用(数据即.dmp文件的导入,用户创建,创建连接)
- 使用javabean实现用户登录
- Android 基本控件的使用一(用户的登录界面)
- Kubernetes用户指南(一)--快速开始、使用k8s配置文件
- php使用session来保存用户登录信息
- 使用django的用户帐号登录openfire
- 使用dctmtk实现DICOM文件的发送(StoreSCU)