黑马程序员-----网络通信
来源:互联网 发布:架子鼓打谱软件 编辑:程序博客网 时间:2024/05/01 07:35
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
用socket在客户端与服务器建立连接,用ServerSocket在服务器端接受客户端的连接请求。
客户端:package iheima;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.Socket;public class Client { public static void main(String[] args) { // TODO Auto-generated method stub try{ //连接到本地机,端口号 //如果连接到远程主机,则将IP设置为远程主机的IP即可 Socket connectServer = new Socket("localhost",5500); //将数据写入到socket上 DataOutputStream outtoServer = new DataOutputStream(connectServer.getOutputStream()); DataInputStream inFromServer = new DataInputStream(connectServer.getInputStream()); System.out.println("Login ... ..."); String name,pswd; boolean goon = true; String strinServer; BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\tname : "); //读数据 name = buf.readLine(); System.out.print("\tpassword : "); pswd = buf.readLine(); outtoServer.writeUTF(name+"-"+pswd); //写到socket中 outtoServer.flush(); //清空缓冲区,立即发送 while(goon){ strinServer = inFromServer.readUTF(); if(!strinServer.endsWith("ok")) System.out.println(strinServer); else goon = false; } inFromServer.close(); outtoServer.close(); connectServer.close(); }catch(Exception e){ e.printStackTrace(); }catch(NoClassDefFoundError e2){ e2.printStackTrace(); } }}
服务器端: package iheima;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.ServerSocket;import java.net.Socket;public class Server { public static void main(String[] args) { // TODO Auto-generated method stub try{ System.out.println("等待连接... ..."); //创建服务器套接字,端口号为5500必须与客户端一致 ServerSocket serverSocket = new ServerSocket(5500); //真挺来自客户端连接的请求 Socket connectToClient = serverSocket.accept(); System.out.println("侦听请求来自"+connectToClient.getInetAddress().getHostAddress()); DataInputStream inFromClient = new DataInputStream(connectToClient.getInputStream()); DataOutputStream outtoClient = new DataOutputStream(connectToClient.getOutputStream()); String name="liwei",pswd="123456"; String message,str; String confirm[]; str = inFromClient.readUTF(); confirm = str.split("-"); if(confirm[0].equals(name) && confirm[1].equals(pswd)){ message = "登录成功"; System.out.println("data have sent"); } else{ message = "验证失败"; System.out.println("sorry ... ..."); } //message.concat("ok"); outtoClient.writeUTF(message); outtoClient.flush(); inFromClient.close(); outtoClient.close(); connectToClient.close(); }catch(NoClassDefFoundError e){ e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } }}
0 0
- 黑马程序员-网络通信
- 黑马程序员-----网络通信
- "黑马程序员"Java网络通信
- 黑马程序员_Socket网络通信
- 黑马程序员—day24网络通信
- 黑马程序员---多线程和网络通信
- 黑马程序员——网络通信
- 黑马程序员 tcp /UTP网络通信
- 黑马程序员-----网络通信(第七篇)
- 黑马程序员——网络通信
- 黑马程序员——网络通信
- 黑马程序员 网络编程之网络通信三要素
- 黑马程序员——Java 网络通信 正则表达式
- 【黑马程序员】网络通信机制和Socket的简单了解
- 黑马程序员_网络编程-笔记遗漏TCP通信
- 黑马程序员——Java基础--- 网络通信
- 黑马程序员——Java之网络通信
- 黑马程序员_Socket通信
- [ROR]部分特点记录
- 软件测试常用术语
- 转帖——也谈文本文件与二进制文件 来自百度文库
- 抽象类(abstract)和接口(interface)的区别
- JavaScript学习笔记8-jQuery基本过滤选择器深度解析
- 黑马程序员-----网络通信
- 我心中的软件测试
- hdoj 2952 Counting Sheep 【dfs】
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
- 自定义:地图标注(大头针)
- poj 1160 Post Office(经典dp)
- 面向接口设计思想
- Quartz()整合Spring容器中bean及动态调度任务
- 建造者模式(二):游戏角色设计的建造者模式解决方案