1.perface:
程序实现功能主要是实现 p2p 式的 对话应答程序。
2.servicecode:
代码如下: server端:
public class TCPSever_v1_0_0 { public TCPSever_v1_0_0() { super(); // TODO Auto-generated constructor stub } public static void startServer() { // 1.创建TCp服务器 ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8000); DialogAdapter.showDialog(">>>服务器启动中>>>"); } catch (IOException e) { System.out.println("8000端口不能使用!"); e.printStackTrace(); } // 2.连接客户端的Socket Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { DialogAdapter.showDialog("客户端的Socket连接诶失败"); e.printStackTrace(); } DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; // 3.建立输入和输出流 try { dataOutputStream = new DataOutputStream(clientSocket .getOutputStream()); dataInputStream = new DataInputStream(clientSocket.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block DialogAdapter.showDialog("建立客户端的输入和输出流失败!"); e.printStackTrace(); } // 定义输入和输出的字符串 String inputstr, outputStr; // 输入和输出字符串 // 输出操作:发送到客户端: try { dataOutputStream.writeUTF("WElcome to our server:\n"); dataOutputStream.flush(); } catch (IOException e) { DialogAdapter.showDialog("输出失败:\n"); e.printStackTrace(); } // 输入操作:读入键盘信息,发送消息 DataInputStream stdin = new DataInputStream(System.in); try { while ((inputstr = dataInputStream.readUTF()) != null) { DialogAdapter.showDialog("【CustomerToServer:】" + inputstr); DialogAdapter.showDialog("【Server:】"); outputStr = stdin.readLine(); DialogAdapter.showDialog("[server 键入]:"+outputStr); dataOutputStream.writeUTF(outputStr); dataOutputStream.flush(); if ("bye".equals(outputStr)) { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 5.关闭流和socket try { dataOutputStream.close(); stdin.close(); dataInputStream.close(); clientSocket.close(); serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } client 端:
public class TCPclient_v1_0_0 { // 客户端的操作: public static void startclient() { Socket clientSocket = null; ; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; DataInputStream stdin = null; try { clientSocket = new Socket("localhost", 8000);// 发出连接请求 dataOutputStream = new DataOutputStream(clientSocket .getOutputStream()); dataInputStream = new DataInputStream(clientSocket.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { stdin = new DataInputStream(System.in); DialogAdapter.showDialog("请输入你的用户名:"); String username = stdin.readLine(); String strtoServer; String strfromServer; while ((strfromServer = dataInputStream.readUTF()) != null) { // 只要读取的服务段的数据是不是空的 DialogAdapter.showDialog("[ServerTOClient:]" + strfromServer); if ("bye".equals(strfromServer)) { // 如果从服务器上发来bye 信息 break; } DialogAdapter.showDialog("【client:】"); strtoServer = stdin.readLine(); dataOutputStream.writeUTF(username + "#" + strtoServer); dataOutputStream.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { dataOutputStream.close(); dataInputStream.close(); stdin.close(); clientSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3.reference:
转载于:https://blog.51cto.com/7050893/1190617