Java中socket编程

本博客实现的功能是:客户端向服务端发送socket请求,要求将服务端随机生成的1000个整数通过网络将其传送到客户端。
服务端代码:

package com.src.netservice;
import java.net.*;
import java.io.*;

public class service extends Thread{

    /**
     * @param args
     */
    private final static int DataLen = 1000;
    private static int[] data;
    private String str; 
    private ServerSocket serverSocket;

    public service(int port) throws IOException {
        data = new int[DataLen];
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(3000);
    }

    public void MakeData() {
        for(int i=1; i1] = (int)(Math.random()*100);
    } 

    public void IntToStr() {
        str = "";
        for (int tmp: data) {
            str = str+String.valueOf(tmp)+" ";  
        }
    }

    public void run() {
        while (true){
            try {
                System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
                Socket server = serverSocket.accept();
                DataInputStream in = new DataInputStream(server.getInputStream());
                System.out.println(in.readUTF());
                MakeData();
                IntToStr();
                DataOutputStream out = new DataOutputStream(server.getOutputStream());
                out.writeUTF("Thank you for connecting to " + server.getRemoteSocketAddress() + "\n" 
                                + "The Return Data is " + str + "\n");  
                server.close();
            } catch (SocketTimeoutException s) {
                System.out.println("Socket timed out!");  
            } catch (IOException e){
                e.printStackTrace();
            }
         }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int port = 10000;
        try {
             Thread t = new service(port);
             t.start();
        } catch(IOException e) {
             e.printStackTrace();
        }
    }
}

客户端代码:

package com.src.netclient;
import java.net.*;
import java.io.*;

public class client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
          final String serverName = "192.168.1.10";
          final int serviceport = 10000;
          try {
             System.out.println("Connecting to " + serverName + " on port " + serviceport);
             Socket client = new Socket(serverName, serviceport);
             OutputStream outToServer = client.getOutputStream();
             DataOutputStream out = new DataOutputStream(outToServer);
             out.writeUTF("Hello from "+ client.getLocalSocketAddress());
             InputStream inFromServer = client.getInputStream();
             DataInputStream in = new DataInputStream(inFromServer);
             System.out.println("Server says " + in.readUTF());
             client.close();
          } catch (IOException e) {
              e.printStackTrace();
          }  
    }
}

客户端向服务端发送socket请求,在服务端的consle中打印的信息如下:
Waiting for client on port 10000…
Hello from /192.168.1.10:58706
Waiting for client on port 10000…
Hello from /192.168.1.10:58707
Waiting for client on port 10000…
Hello from /192.168.1.10:58708
Waiting for client on port 10000…
Hello from /192.168.1.10:58709
Waiting for client on port 10000…
Hello from /192.168.1.10:58710
Waiting for client on port 10000…
Hello from /192.168.1.10:58711
Waiting for client on port 10000…
Hello from /192.168.1.10:58712
Waiting for client on port 10000…
Socket timed out!
Waiting for client on port 10000…
Socket timed out!
从上面控制台输出的信息可以看到,客户端执行Socket client = new Socket(serverName, serviceport)后,端口不断加1。
从服务端传回到客户端的数据流,打印在consle中的信息如下,其中包含了在客户端随机身生成的正整数(0~100)。
Connecting to 192.168.1.10 on port 10000
Server says Thank you for connecting to /192.168.1.10:58637
The Return Data is 68 45 64 97 45 27 92 7 75 85 7 53 45 42 7 29 5 6 72 8 50 85 22 84 7 64 72 52 50 19 2 88 63 18 20 53 40 62 83 70 24 75 1 26 81 31 70 16 73 67 52 66 90 62 43 68 39 37 59 18 52 89 43 87 33 6 24 14 69 74 29 9 84 58 58 19 13 34 63 67 91 61 8 62 40 96 50 38 30 7 20 38 72 70 37 45 65 40 88 17 73 37 56 40 74 15 55 81 57 82 57 17 65 87 65 98 16 19 80 57 55 47 72 78 57 92 37 97 53 35 56 90 72 48 31 57 30 2 11 24 99 6 15 98 9 10 6 28 34 90 69 78 81 81 16 66 6 84 65 21 36 35 89 58 30 16 19 33 81 85 8 7 89 13 77 21 58 76 26 31 24 50 76 98 96 80 20 48 21 4 56 85 71 93 82 76 30 83 53 41 42 92 26 54 50 81 71 79 87 42 0 2 76 98 13 51 74 73 25 60 83 4 36 13 21 65 96 83 86 55 44 1 41 93 22 90 20 80 43 73 75 43 91 71 12 91 20 11 59 98 73 29 53 8 15 73 71 36 21 88 77 74 6 50 64 6 16 1 14 14 21 68 76 77 66 50 79 75 48 29 74 62 67 0 97 20 8 95 73 68 66 72 98 0 8 43 32 28 11 28 15 98 95 70 30 16 54 99 69 32 79 96 61 39 48 87 41 63 83 13 11 76 71 23 50 34 52 77 97 89 39 76 84 50 29 31 87 40 12 23 65 63 63 23 37 86 5 44 44 30 33 29 91 24 41 92 42 50 80 30 62 32 27 50 3 10 92 78 67 3 35 89 13 85 86 52 81 82 2 72 99 72 69 7 64 51 10 74 26 66 94 43 37 19 92 25 35 93 21 63 33 18 69 72 11 61 58 94 48 5 36 37 63 6 48 85 61 19 29 16 54 28 17 12 5 60 56 25 16 50 3 48 2 65 72 22 35 50 32 87 0 13 19 29 5 46 14 21 63 89 78 46 63 27 64 10 21 71 96 46 66 71 81 1 70 32 80 62 81 86 39 78 68 71 81 29 81 63 71 54 94 15 1 33 76 41 50 39 40 50 5 83 31 6 94 32 66 73 13 3 38 78 57 37 41 89 4 16 40 67 81 0 37 74 73 69 41 93 15 96 60 85 91 3 43 5 33 40 35 98 93 91 76 41 2 92 71 91 77 91 46 20 84 64 73 10 80 72 88 49 78 76 98 85 94 26 11 93 25 98 41 81 2 75 77 42 40 84 12 7 74 23 31 60 46 85 54 11 39 20 6 24 13 91 86 31 26 43 35 67 41 51 92 98 50 3 68 81 30 57 81 93 65 30 73 69 29 96 88 6 29 8 56 30 55 17 97 61 46 49 43 12 34 47 3 87 77 38 1 94 90 64 44 68 44 79 93 51 89 25 61 92 58 30 3 32 26 71 89 72 47 11 58 53 93 91 67 38 78 5 41 77 82 8 22 90 85 7 85 41 96 0 78 77 46 85 71 44 86 2 84 53 79 52 83 19 33 25 65 1 53 59 92 40 36 92 32 41 24 76 65 39 84 99 26 61 40 9 25 42 14 19 60 36 89 66 16 46 96 26 91 23 80 81 37 83 81 14 47 49 63 47 49 68 16 10 72 30 50 30 84 2 11 32 78 79 95 56 91 89 94 43 12 10 17 94 10 0 18 70 79 31 3 80 54 57 90 25 76 82 13 53 48 65 23 33 29 72 70 95 39 79 61 41 42 13 25 10 90 2 6 92 90 56 0 61 23 52 50 58 32 74 76 64 56 27 1 85 8 66 72 19 78 83 43 90 48 42 19 99 25 73 79 7 31 43 46 50 76 42 42 81 39 1 15 64 73 55 72 48 9 26 76 32 25 6 1 58 81 97 40 80 71 48 85 21 98 76 71 58 89 28 79 94 89 86 64 24 91 24 47 29 52 16 53 52 40 10 50 49 31 35 58 47 67 8 53 32 22 48 79 48 93 97 41 16 86 14 83 68 68 83 45 49 32 99 55 69 81 62 16 50 0 52 97 5 27 17 82 61 76 28 4 61 1 34 95 99 86 74 46 77 80 44 49 44 85 71 39 35 4 0 19 14 17 99 80 44 61 64 57 13 12 51 85 62 59 56 43 70 68 34 47 24 7 96 82 86 94 15 7 76 64 34 85 59 56 14 10 36 21 48 90 64 25 42 80 23 22 71 93 98 20 43 44 30 41 47 9 0

你可能感兴趣的:(Java)