Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522786
  • 博文数量: 114
  • 博客积分: 271
  • 博客等级: 二等列兵
  • 技术积分: 733
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-24 13:40
文章分类
文章存档

2014年(5)

2013年(14)

2012年(95)

分类: Java

2012-07-06 13:35:56

java 下的网络编程:
1.客户端

点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.io.PrintStream;
  3. import java.net.Socket;
  4. import java.net.UnknownHostException;

  5. public class Client {

  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         try {
  12.             Socket socket = new Socket("localhost", 8001);
  13.             PrintStream printStream = new PrintStream(socket.getOutputStream());
  14.             printStream.println("hello");
  15.             printStream.close();
  16.         } catch (UnknownHostException e) {
  17.             // TODO Auto-generated catch block
  18.             e.printStackTrace();
  19.         } catch (IOException e) {
  20.             // TODO Auto-generated catch block
  21.             e.printStackTrace();
  22.         }
  23.     }

  24. }
2.服务器端

点击(此处)折叠或打开

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;

  6. public class Server {

  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         try {
  13.             ServerSocket serverSocket = new ServerSocket(8001);
  14.             Socket socket = serverSocket.accept();
  15.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  16.             System.out.print(bufferedReader.readLine());
  17.         } catch (IOException e) {
  18.             // TODO Auto-generated catch block
  19.             e.printStackTrace();
  20.         }
  21.         
  22.     }

  23. }

阅读(1634) | 评论(0) | 转发(1) |
0

上一篇:java 学习笔记

下一篇:java 学习笔记4

给主人留下些什么吧!~~