美文网首页
JAVA中的scoket简单用法

JAVA中的scoket简单用法

作者: lvhaosir | 来源:发表于2017-12-10 19:33 被阅读0次

    客户端

    package client;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.Scanner;
    
    import javax.xml.ws.handler.MessageContext.Scope;
    
    public class Client1 {
        private Scanner sc=new Scanner(System.in);
        private OutputStream out;
        private InputStream in;
        private Socket socket;
        public Client1(){
            try {
                //创建socket对象
                socket = new Socket("127.0.0.1",8888);
                System.out.println("连上服务器...");
                while(true){
                    System.out.println("请输入:");
                    
                    out = socket.getOutputStream();
                    //输入
                    String next = sc.next();
                    
                    out.write(next.getBytes());
                    
                    
                    in = socket.getInputStream();
                    byte[] b=new byte[1024*1024];
                    
                    int len = in.read(b);
                    
                    String string = new String(b, 0, len);
                    
                    if("bye".equals(string)){
                        break;
                    }
                    System.out.println("服务器回复:"+string);
                }
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            Client1 c=new Client1();
        }
    }
    
    

    服务器端

    package server;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Server1 {
        
        private Scanner sc=new Scanner(System.in);
        public Server1(){
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
                System.out.println("服务器等待连接中.....");
                Socket socket = serverSocket.accept();
                System.out.println("连接成功");
                while(true){
                    
                    InputStream in = socket.getInputStream();
                    
                    byte[] b=new byte[1024*1024];
                    
                    int len = in.read(b);
                    
                    String id=new String(b,0,len);
                    
                    System.out.println("客户端说:"+id);
                    
                    OutputStream out = socket.getOutputStream();
                    
                    String next = sc.next();
                    out.write(next.getBytes());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            Server1 s=new Server1();
        }
    }
    
    

    多线程的服务器端

    package more.server;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ServerMore {
        public static void main(String[] args) {
            ServerMore s=new ServerMore();
        }
        public ServerMore(){
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
                System.out.println("服务器等待链接中.....");
                while(true){
                    Socket socket = serverSocket.accept();
                    System.out.println("连接成功");
                    ServerThread st=new ServerThread(socket);
                    new Thread(st).start();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    class ServerThread implements Runnable{
    
        private Socket socket;
        private InputStream in;
        private OutputStream out;
        public ServerThread(Socket socket){
            this.socket=socket;
        }
        @Override
        public void run() {
            try {
                while(true){
                    in = socket.getInputStream();
                    byte[] b=new byte[1024];
                    int len = in.read(b);
                    String msg=new String(b,0,len);
                    System.out.println("客户端发送过来的消息为:\t"+msg);
                    if("bye".equals(msg)){
                        break;
                    }
                    out=socket.getOutputStream();
                    out.write("我是服务器".getBytes());
                }
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:JAVA中的scoket简单用法

          本文链接:https://www.haomeiwen.com/subject/kbbvixtx.html