Chinaunix首页 | 论坛 | 博客
  • 博客访问: 441541
  • 博文数量: 52
  • 博客积分: 1076
  • 博客等级: 准尉
  • 技术积分: 821
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-18 20:05
个人简介

Hello World !

文章分类

全部博文(52)

文章存档

2016年(1)

2014年(1)

2013年(8)

2012年(38)

2011年(4)

分类: Java

2013-03-11 23:10:00

服务端
SocketManager.java
SocketManagerSocketManagerSocketManagerSocketManager

点击(此处)折叠或打开

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;

  6. public class SocketManager {
  7.     /**
  8.      * @param args
  9.      * @throws IOException
  10.      */
  11.     public static void main(String[] args) {
  12.         SocketManager manager = new SocketManager();
  13.         manager.doListen();
  14.     }

  15.     public void doListen() {
  16.         ServerSocket server;
  17.         try {
  18.             server = new ServerSocket(9991);
  19.             while (true) {
  20.                 Socket client = server.accept();
  21.                 new Thread(new SSocket(client)).start();
  22.             }
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }

  26.     }

  27.     //服务器进程
  28.     class SSocket implements Runnable {

  29.         Socket client;

  30.         public SSocket(Socket client) {
  31.             this.client = client;
  32.         }

  33.         public void run() {
  34.             DataInputStream input;
  35.             DataOutputStream output;
  36.             try {
  37.                 input = new DataInputStream(client.getInputStream());
  38.                 output = new DataOutputStream(client.getOutputStream());
  39.                 //
  40.                 String listMsg = input.readUTF();
  41.                 output.writeUTF("Send : " + listMsg + " \r\n HelloVillage...");
  42.                 System.out.println("Recive: " + listMsg);
  43.                 listMsg = input.readUTF();
  44.                 output.writeUTF("Send Second: " + listMsg + " \r\n HelloVillage...");
  45.                 System.out.println("Recive Second: " + listMsg);
  46.             } catch (IOException e) {
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.     }
  51. }

客服端
SocketClient.java

点击(此处)折叠或打开

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;

  7. public class SocketClient {

  8.     public static void main(String[] args) {
  9.         Socket socket = null;
  10.         try {
  11.             socket = new Socket("127.0.0.1", 9991);
  12.             //向服务器端第一次发送字符串
  13.             OutputStream netOut = socket.getOutputStream();
  14.             DataOutputStream doc = new DataOutputStream(netOut);
  15.             DataInputStream in = new DataInputStream(socket.getInputStream());
  16.             //向服务器端第二次发送字符串
  17.             doc.writeUTF("list");
  18.             String res = in.readUTF();
  19.             System.out.println(res);
  20.             doc.writeUTF("bye");
  21.             res = in.readUTF();
  22.             System.out.println(res);
  23.             doc.close();
  24.             in.close();
  25.         } catch (UnknownHostException e) {
  26.             e.printStackTrace();
  27.         } catch (IOException e) {
  28.             e.printStackTrace();
  29.         } {
  30.             if (socket != null) {
  31.                 try {
  32.                     socket.close();
  33.                 } catch (IOException e) {
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }

阅读(11516) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~