第3章 TCP/IP Socket网络通讯------第4章 实现Socket C/S应用程序
使用Socket:
需要指定IP地址和端口号。
搭建服务器:
1.声明ServerSocket(端口号);
2.serversocket.accept();等待连接,如果连接成功,获取socket对象
3.获取输出流----socket.getOutputStream()
4.获取输入流----socket.getInputStream()
5.使用流进行读写,关闭流,关闭socket
搭建客户端:
1.创建一个scoket并声明Ip和端口号
2.获取输出流----socket.getOutputStream()
3.获取输入流----socket.getInputStream()
4.使用流进行读写,关闭流,关闭socket
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
----讲InputStream包装为一个BufferedReader,并且使用utf-8来作为编码集
DataInputStream dis = new DataInputStream( socket.getInputStream());----获取数据输入流
String s = dis.readUTF();----使用utf-8来作为编码集
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ois.readObject(); 获取到一个Object类型的数据
多线程的服务器:主线程只负责处理ServerSocket,只负责等待连接,一旦有客户端连接成功,启动一个子线程,
所有的数据交互都是在子线程中完成的。
1 while(true){ 2 Socket s = server.accept(); 3 ThreadServie service = new ThreadService(s); 4 Thread t = new Thread(service); 5 t.start(); 6 }
应用程序请求网络的权限:
<uses-permission android:name="android.permission.INTERNET" />
ANR:ANR是Application Not Responding的缩写,即应用程序无响应
CalledFormWrongThreadException:在错误的线程中调用了方法
不允许在子线程中进行UI界面的更新。不允许在主线程中进行耗时操作和网络请求
案例代码:
package: com.gzy.server.utils
1 public class CloseUtils { 2 /** 3 * 关闭Closeable对象 4 * @param closeable 5 */ 6 public static void closeQuietly(Closeable closeable) { 7 if(null != closeable) { 8 try { 9 closeable.close(); 10 } catch (IOException e) { 11 e.printStackTrace(); 12 } 13 } 14 } 15 }
package: com.gzy.server
1 /** 2 * 负责与客户端连接 3 * @author Administrator 4 * 5 */ 6 public class DownloadServer { 7 8 public final static int PORT = 4321; //指定端口号 9 private ServerSocket mServerSocket; 10 private boolean loop = true; 11 12 public DownloadServer() { 13 try { 14 mServerSocket = new ServerSocket(PORT); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 19 while (loop) { 20 try { 21 Socket mSocket = mServerSocket.accept(); 22 System.out.println("已接收客户端连接..."); 23 24 Thread mThread = new Thread(new SocketThread(mSocket)); 25 mThread.start(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 } 31 32 public void shutdown() { 33 loop = false; 34 } 35 36 public static void main(String[] args) { 37 System.out.println("启动服务器..."); 38 new DownloadServer(); 39 } 40 }
package: com.gzy.server
1 /** 2 * 负责多线程 3 * @author Administrator 4 * 5 */ 6 public class SocketThread implements Runnable { 7 8 private Socket socket; 9 10 public SocketThread(Socket socket) { 11 this.socket = socket; 12 } 13 14 @Override 15 public void run() { 16 ObjectInputStream ois = null; 17 ObjectOutputStream oos = null; 18 19 try { 20 ois = new ObjectInputStream(socket.getInputStream()); 21 String cmd = ois.readUTF(); 22 System.out.println("服务器接收命令:cmd=[" + cmd + "]" ); 23 24 oos = new ObjectOutputStream(socket.getOutputStream()); 25 DownloadService mService = new DownloadService(cmd); 26 Object object = mService.doRequst(); 27 oos.writeObject(object); 28 oos.flush(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } finally { 32 CloseUtils.closeQuietly(oos); 33 CloseUtils.closeQuietly(ois); 34 CloseUtils.closeQuietly(socket); 35 } 36 } 37 }
package: com.gzy.server
1 /** 2 * 负责对具体命令的处理工作 3 * @author Administrator 4 * 5 */ 6 public class DownloadService { 7 8 /** 9 * cmd list: 10 * 1 -- retieve file list 11 * 2 -- retieve file after date 12 * 3filename -- download file 13 */ 14 private String cmd; 15 private static final String FILEPATH = "D:/Workspace"; 16 17 public DownloadService(String cmd) { 18 this.cmd = cmd; 19 } 20 21 public Object doRequst() { 22 char cmdChar = cmd.charAt(0); 23 Object object = null; 24 25 switch(cmdChar) { 26 case '1': 27 //获取文件名列表 28 object = getFileList(); 29 break; 30 case '2': 31 //TODO 32 break; 33 case '3': 34 //TODO 35 break; 36 } 37 return object; 38 } 39 40 /** 41 * 获取所有文件名列表 42 * @return 43 */ 44 private String[] getFileList() { 45 File file = new File(FILEPATH); 46 String[] list = file.list(); 47 return list; 48 } 49 }
com.gzy.server.testclient
1 public class Client { 2 private static final String POST = "127.0.0.1"; 3 4 public static void main(String[] args) { 5 Socket socket = null; 6 ObjectOutputStream oos = null; 7 ObjectInputStream ois = null; 8 9 try { 10 socket = new Socket(POST, DownloadServer.PORT); 11 oos = new ObjectOutputStream(socket.getOutputStream()); 12 oos.writeUTF("1"); 13 oos.flush(); 14 15 ois = new ObjectInputStream(socket.getInputStream()); 16 String[] list = (String[])ois.readObject();//获取文件列表 17 System.out.println("接收文件目录: "); 18 for (String str : list) { 19 System.out.println("[" + str + "]"); 20 } 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } finally { 24 CloseUtils.closeQuietly(socket); 25 CloseUtils.closeQuietly(oos); 26 CloseUtils.closeQuietly(ois); 27 } 28 } 29 }
posted on 2015-12-20 19:28 starFarming 阅读(318) 评论(0) 编辑 收藏 举报