• Socket编程


     1 /**
     2  *客户端
     3  */
     4 import java.io.*;
     5 import java.net.*;
     6 
     7 public class Client {
     8     Socket socket;
     9     BufferedReader in;
    10     PrintWriter out;
    11 
    12     public Client() {
    13         try {
    14             socket = new Socket("xxx.xxx.xxx.xxx", 10000);
    15             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    16             out = new PrintWriter(socket.getOutputStream(), true);
    17             BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
    18             out.println(line.readLine());
    19             
    20             line.close();
    21             out.close();
    22             in.close();
    23             socket.close();
    24         } catch (IOException e) {
    25         }
    26     }
    27 
    28     public static void main(String[] args) {
    29         new Client();
    30     }
    31 }
    View Code
     1 /**
     2  * 监听一个客户的服务器端
     3  */
     4 import java.net.*;
     5 import java.io.*;
     6 
     7 public class Server {
     8     private ServerSocket ss;
     9     private Socket socket;
    10     private BufferedReader in;
    11     private PrintWriter out;
    12 
    13     public Server() {
    14         try {
    15             ss = new ServerSocket(10000);
    16             while (true) {
    17                 socket = ss.accept();
    18                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    19                 out = new PrintWriter(socket.getOutputStream(), true);
    20                 String line = in.readLine();
    21                 out.println("you input is :" + line);
    22                 
    23                 out.close();
    24                 in.close();
    25                 socket.close();
    26                 ss.close();
    27             }
    28         } catch (IOException e) {
    29         }
    30     }
    31 
    32     public static void main(String[] args) {
    33         new Server();
    34     }
    35 }
    View Code
     1 /**
     2  * 监听多个客户的服务器端
     3  * 
     4  * 主程序监听一端口,等待客户接入;同时构造一个线程类,准备接管会话。
     5  * 当一个Socket会话产生后,将这个会话交给线程处理,然后主程序继续监听。
     6  */
     7 import java.io.*;
     8 import java.net.*;
     9 
    10 public class MultiServer extends ServerSocket {
    11     private static final int SERVER_PORT = 10000;
    12 
    13     public MultiServer() throws IOException {
    14         super(SERVER_PORT);
    15         try {
    16             while (true) {
    17                 Socket socket = accept();
    18                 new CreateServerThread(socket);
    19             }
    20         } catch (IOException e) {
    21         } finally {
    22             close();
    23         }
    24     }
    25 
    26     // --- CreateServerThread
    27     class CreateServerThread extends Thread {
    28         private Socket client;
    29         private BufferedReader in;
    30         private PrintWriter out;
    31 
    32         public CreateServerThread(Socket s) throws IOException {
    33             client = s;
    34             in = new BufferedReader(new InputStreamReader(
    35                     client.getInputStream(), "GB2312"));
    36             out = new PrintWriter(client.getOutputStream(), true);
    37             out.println("--- Welcome ---");
    38             start();
    39         }
    40 
    41         public void run() {
    42             try {
    43                 String line = in.readLine();
    44                 while (!line.equals("bye")) {
    45                     String msg = createMessage(line);
    46                     out.println(msg);
    47                     line = in.readLine();
    48                 }
    49                 out.println("--- See you, bye! ---");
    50                 client.close();
    51             } catch (IOException e) {
    52             }
    53         }
    54 
    55         private String createMessage(String line) {
    56             // xxxxxxxxx;
    57             return line;
    58         }
    59     }
    60 
    61     public static void main(String[] args) throws IOException {
    62         new MultiServer();
    63     }
    64 }
    View Code
  • 相关阅读:
    FireFox浏览器的下载和安装、借助RamDisk让你的FireFox飞起来
    XXX is not in the sudoers file. This incident will be reported 的问题解决方案
    UVA How Big Is It?
    CentOS配置smaba与Windows共享文件
    博客说明
    linux 下安装jdk及配置jdk环境图解
    swift 笔记 (十二) —— 下标
    依据先中序序列或后中序序列确定二叉树
    opencv2使用形态学滤波对图像进行边缘及角点检測
    $POST 、$HTTP_RAW_POST_DATA、php://input三者之间的差别
  • 原文地址:https://www.cnblogs.com/perfy/p/3137246.html
Copyright © 2020-2023  润新知