zoukankan      html  css  js  c++  java
  • JAVA TCP Socket

    服务器端

    package com.Pong.tcpip;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class TcpServer {
    
        //先启动服务器端程序
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket=new ServerSocket(8080);
            Socket socket=serverSocket.accept();//阻塞等待消息
            InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();
    //        outputStream.write("欢迎光临!".getBytes(), 0,"欢迎光临!".getBytes().length );
            outputStream.write("welcome!".getBytes() );
        /*    byte[] buf=new byte[1024];
            int len=inputStream.read(buf);
    //        System.out.println(new String(buf, 0, len));
            inputStream.close();*/
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            System.out.println(bufferedReader.readLine());
            bufferedReader.close();
            outputStream.close();
            socket.close();
            serverSocket.close();
        }
    }
    package com.Pong.tcpip;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Servicer implements Runnable {
        Socket socket;
    
        public Servicer(Socket socket) {
            super();
            this.socket = socket;
        }
    
        @Override
        public void run() {
        try {
            InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();
            
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
            while (true) {
                String strWord=bufferedReader.readLine();
    //            System.out.println(strWord+":"+strWord.length());
                if ("quit".equalsIgnoreCase(strWord)) {
                    break;
                }
                String strEcho=(new StringBuilder(strWord).reverse()).toString();
                //dataOutputStream.writeBytes(strWord+"---->"+strEcho+"
    ");
                //System.getProperty("line.separator")为跨平台换行符。Window为
    ,unix为
    
                dataOutputStream.writeBytes(strWord+"---->"+strEcho+System.getProperty("line.separator"));
            }
            bufferedReader.close();
            bufferedReader.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    
        }
        
        public static void main(String [] args)
        {
            try
            {
                ServerSocket ss=new ServerSocket(8080);
                while(true)
                {
                    Socket s=ss.accept();
                    new Thread(new Servicer(s)).start();
                }
                //ss.close();
            }catch(Exception e){e.printStackTrace();}
        }
    
    }

    客户端

    package com.Pong.tcpip;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    //后启动客户端程序
    public class TcpClient {
        //启动main时传进来两个参数:ip和端口号
        public static void main(String [] args) 
        {
        try
        {
            //Socket s=new Socket(InetAddress.getByName("192.168.0.213"),8001);
            if(args.length < 2)
            {
                System.out.println("Usage:java TcpClient ServerIP ServerPort");
                return;
            }
            //建立Socket
            Socket s=new Socket(InetAddress.getByName(args[0]),Integer.parseInt(args[1]));
            InputStream ips=s.getInputStream();
            OutputStream ops=s.getOutputStream();
            
            BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));//键盘输入
            DataOutputStream dos = new DataOutputStream(ops);
            BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));
    
            while(true)
            {
                String strWord = brKey.readLine();
                dos.writeBytes(strWord + System.getProperty("line.separator"));
                if(strWord.equalsIgnoreCase("quit"))
                    break;
                else
                    System.out.println(brNet.readLine());
            }
            dos.close();
            brNet.close();
            brKey.close();
            s.close();
        }catch(Exception e){e.printStackTrace();}
        }
    }
    /*发送和接受都是以Socket为信息载体*/
  • 相关阅读:
    Javascript数组与字符串常用api
    javaScript入门(函数)
    javascript入门(1)
    微信小程序组件解读和分析:一、view(视图容器 )
    微信小程序组件解读和分析:十五、switch 开关选择器
    微信小程序组件解读和分析:十四、slider滑动选择器
    微信小程序组件解读和分析:十三、radio单选项目
    微信小程序组件解读和分析:十二、picker滚动选择器
    微信小程序组件解读和分析:十一、label标签
    微信小程序组件解读和分析:十、input输入框
  • 原文地址:https://www.cnblogs.com/Kconnie/p/3976353.html
Copyright © 2011-2022 走看看