TCP使用(二)多用户发送文件和登录
来源:互联网 发布:建筑设计效果图软件 编辑:程序博客网 时间:2024/06/07 23:58
服务器端接收多个客户端同时发送的文件
血的教训:
在服务器端未添加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}}
- TCP使用(二)多用户发送文件和登录
- TCP多用户登录
- Android实战系列(二)---多用户类型登录(webview)
- 使用 VNC 实现多用户登录
- TCP发送文件(转)
- socket(TCP)发送文件
- 计算机多用户登录(扫盲)
- C# tcp发送消息和传输文件
- 多用户登录
- Java利用TCP协议发送文件(二)--数据包设计
- TCP发送文件
- TCP发送文件
- tcp 发送接收文件
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- 使用JavaMail创建邮件和发送邮件(二)
- Linux网络编程:TCP服务器(单进程多用户),使用select方法实现
- Linux网络编程:TCP服务器(单进程多用户),使用select方法实现
- Linux网络编程:TCP服务器(单进程多用户),使用select方法实现
- Couchbase备份
- 程序零散笔记
- Ubuntu的软件源更新常见问题及解决
- Android Activity Life Cycle - What are all these methods for?
- redis aof
- TCP使用(二)多用户发送文件和登录
- BaiduBCS云存储——java SDK下载问题
- [C#] 内存占用释放
- wcf配置服务
- 内核中的UDP socket流程(1)
- BOA服务器分析流程
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- 捣腾数字签名
- 什么时候释放bitmap