zoukankan      html  css  js  c++  java
  • Java Socket Example

    1.服务端:server

    package com.socket;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    import com.common.LogHelper;
     
    /**
     * ServerSide: receive message from Client and return it.
     * @author Guoliang
     *
     */
    public class MesgServer implements Runnable{
         
        public void run() {
            LogHelper.log("ServerStarted");
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
             
                while (true) {
                     
                        Socket socket = serverSocket.accept();
                        LogHelper.log("Request: " + socket.toString());
                         
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        PrintWriter out = new PrintWriter(socket.getOutputStream());
                         
                        out.println("Connected!");
                        out.flush();
                         
                        String lineInput;
                        while ((lineInput = in.readLine()) != null) {
                            LogHelper.log("GotMesgFromClient: " + lineInput);
                            out.println("ServerReturnMsg: " + lineInput);
                            out.flush();
                            if("exit".equals(lineInput)) {
                                break;
                            }
                        }
                         
                        out.close();
                        in.close();
                        socket.close();
                }
                 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
    }

    2.客户端 Client

    package com.socket;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    import com.common.LogHelper;
     
    public class MesgClient {
     
        /**
         * Send mesg to server side.
         * @param args
         * @throws IOException
         * @throws UnknownHostException
         */
        public static void main(String[] args) throws UnknownHostException, IOException {
             
            // Start server.
            new Thread(new MesgServer()).start();
             
            Socket socket = new Socket("localhost", 8888);
            LogHelper.log("Connecting...");
             
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             
            LogHelper.log(in.readLine());
             
            // Simplest testing
            out.println("Hello!");
            LogHelper.log(in.readLine());
             
            // Read user input.
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String strInput = null;
            while ((strInput = stdIn.readLine()) != null) {
                out.println(strInput);
                LogHelper.log("Send: " + strInput);
                LogHelper.log(in.readLine());
            }
        }
    }

    Output:

    [Thread-0]ServerStarted
    [main]Connecting...
    [Thread-0]Request: Socket[addr=/127.0.0.1,port=5569,localport=8888]
    [main]Connected!
    [Thread-0]GotMesgFromClient: Hello!
    [main]ServerReturnMsg: Hello!
    liguoliang.com  // user input.
    [Thread-0]GotMesgFromClient: liguoliang.com
    [main]Send: liguoliang.com
    [main]ServerReturnMsg: liguoliang.com

    转自:

    http://liguoliang.com/2013/java-socket-example/

  • 相关阅读:
    axublogcms1.1.0 Getshell
    易酷 cms2.5 本地文件包含漏洞 getshell
    通过 phpmyadmin getshell
    python 简单图像识别--验证码
    Linux 入侵检测小结
    beef + msf 实现内网渗透
    phpwind v9存在命令执行漏洞(登陆后台)
    缓冲区溢出实践
    《Metasploit魔鬼训练营》第四章(下)
    《Metasploit魔鬼训练营》第四章(上)
  • 原文地址:https://www.cnblogs.com/ae6623/p/4416484.html
Copyright © 2011-2022 走看看