使用socket进行单线程的网络编程

连接建立过程:Server端服务器监听端口的连接请求,Client端向Server服务端发出连接请求,Server服务端调用accep()函数发回接受请求消息。
步骤有:
1.创建Socket对象;
2.打开输入输出流,用于接收网络端口的消息;
3.进行读写操作;

  • 客户端Client的程序实现:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import java.util.Scanner;

     public class Client {
        public static final int port = 3001;
    
        public static void main(String[] args) {
            try {
                Socket s = new Socket("192.168.0.102", port);
    \\try catch用来在连接建立不成功时产生异常;
    \\192.168.0.102为服务器ip地址;端口号port从0到1023为系统保留,因此最好用1023以上的
                System.out.println("服务连接成功.........");
     \\若连接过久,重新运行程序时会运行出错。
          BufferedReader input = new BufferedReader(new     InputStreamReader(s.getInputStream()));
      \\getInputStream()只接收字节,而InputStreamReader()使之变为字符,BufferedReaderI()使之变为字符串;
          BufferedWriter output = new BufferedWriter(new        OutputStreamWriter(s.getOutputStream()));
          Scanner scan = new Scanner(System.in);
          String clientMsg;
          String serverMsg;
          clientMsg = scan.nextLine();
          
          while (!clientMsg.startsWith("bye") ) {
                 output.write("client say" + clientMsg + "\r\n");
     \\ 这里的\r\n为换行符,因为readLine()要读到换行符才产生作用。
              output.flush();
              serverMsg=input.readLine();
              System.out.println(serverMsg);
              clientMsg = scan.nextLine();
          }
          s.close();
          scan.close();
          input.close();
          output.close();
    
      } catch (IOException e) {
    
          e.printStackTrace();
      }
    }
    
    }
    
  • 服务端程序实现:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;

    public class Server {
      public static final int port=3002;
      public static void main(String[] args) {
      try {
          ServerSocket s = new ServerSocket(port);
          System.out.println("服务器已启动.........");
          Socket socket;
          socket = s.accept();
          System.out.println("已建立连接");
      BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    
      String serverMsg;
      String clientMsg;
      Scanner scan = new Scanner(System.in);
      clientMsg = input.readLine();
      while(!clientMsg.startsWith("bye")){
          System.out.println(clientMsg);
          serverMsg = scan.nextLine();
          output.write("server say"+serverMsg+"\r\n");
          output.flush();
          clientMsg = input.readLine();
      }
      System.out.println("关闭连接....");
      s.close();
      scan.close();
      socket.close();
    }
      catch (IOException e) {
          
          e.printStackTrace();
      }
     }
    }
    

    这是单个端口的socket编程,而若要进行多个客户端的socket编程则要用到多线程的知识,在此不赘述。
    要问我代码为什么写得这么屌?戳下面的链接你就知道,想成为码王一样的男人吗?我的代码都放在那了,想要吗?还等什么?赶快去拿吧!!

http://qingke.me/

你可能感兴趣的:(使用socket进行单线程的网络编程)