Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566592
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-12 16:06:34


 socket_tutorial.zip  

本例包含两个类:JabberServer 是服务端,在9089端口监听来自客户端的请求
JabberClient是客户端,它会首先和服务端建立连接。当客户端与服务器端的两个socket建立了专线连接后,连接的一端能向另一端连续写入字节,也能从另一端连续读入字节,也就是建立了专线连接的两个socket是以io流的方式进行数据交换的,java提供了Socket.getInputStream方法返回socket的输入流对象,Socket.getOuputStream方法返回socket的输出流对象。只要连接的一端向该输出流对象写入了数据,连接的另一端就能从其输入流对象中读取到这些数据。


直接上代码。


  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;

  9. public class JabberServer {
  10.     // Choose a port outside of the range 1-1024:
  11.     public static final int PORT = 9089;

  12.     public static void main(String[] args) throws IOException {
  13.         ServerSocket s = new ServerSocket(PORT);
  14.         System.out.println("ServerSocket Started: " + s);
  15.         try {
  16.             // Blocks until a connection occurs:
  17.             Socket socket = s.accept();
  18.             try {
  19.                 System.out
  20.                         .println("ServerSocket Accect a socket, connection accepted: "
  21.                                 + socket);
  22.                 BufferedReader in = new BufferedReader(new InputStreamReader(
  23.                         socket.getInputStream()));
  24.                 // Output is automatically flushed
  25.                 // by PrintWriter:
  26.                 PrintWriter out = new PrintWriter(new BufferedWriter(
  27.                         new OutputStreamWriter(socket.getOutputStream())), true);
  28.                 while (true) {
  29.                     // the server inputstream object read a line from socket
  30.                     String str = in.readLine();
  31.                     if (str.equals("END"))
  32.                         break;
  33.                     System.out.println("Echoing: " + str);
  34.                     out.println(str);
  35.                 }
  36.                 // Always close the two sockets...
  37.             } finally {
  38.                 System.out.println(" server closing...");
  39.                 socket.close();
  40.             }
  41.         } finally {
  42.             s.close();
  43.         }
  44.     }
  45. }

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.net.InetAddress;
  8. import java.net.Socket;

  9. public class JabberClient {

  10.     public static void main(String[] args) throws IOException {
  11.         // Passing null to getByName() produces the
  12.         // special "Local Loopback" IP address, for
  13.         // testing on one machine w/o a network:
  14.         // InetAddress addr = InetAddress.getByName(null);
  15.         // Alternatively, you can use
  16.         // the address or name:
  17.         // InetAddress addr =
  18.         // InetAddress.getByName("127.0.0.1");
  19.         InetAddress addr = InetAddress.getByName("localhost");
  20.         System.out.println("address = " + addr);
  21.         Socket socket = new Socket(addr, 9089);
  22.         // Guard everything in a try-finally to make
  23.         // sure that the socket is closed:
  24.         try {
  25.             System.out.println("Client socket = " + socket);
  26.             BufferedReader in = new BufferedReader(new InputStreamReader(
  27.                     socket.getInputStream()));
  28.             // Output is automatically flushed
  29.             // by PrintWriter:
  30.             PrintWriter out = new PrintWriter(new BufferedWriter(
  31.                     new OutputStreamWriter(socket.getOutputStream())), true);
  32.             out.print("we are data come from client output, server can get us by its input stream");
  33.             for (int i = 0; i < 10; i++) {
  34.                 out.println("howdy " + i);
  35.                 String str = in.readLine();
  36.                 System.out.println(str);
  37.             }
  38.             out.println("END");
  39.         } finally {
  40.             System.out.println("closing...");
  41.             socket.close();
  42.         }
  43.     }

  44. }


谢谢访问!

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