欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java网络通信基础编程(必看篇)

程序员文章站 2024-02-22 10:56:46
方式一:同步阻塞方式(bio): 服务器端(server): package com.ietree.basicskill.socket.mode1; im...

方式一:同步阻塞方式(bio):

服务器端(server):

package com.ietree.basicskill.socket.mode1;

import java.io.ioexception;
import java.net.serversocket;
import java.net.socket;

/**
 * 服务端
 */
public class server {
  // 端口号
  final static int port = 8765;
  public static void main(string[] args) {
    serversocket server = null;
     try {
      server = new serversocket(port);
      system.out.println("server start......");
      // 进行阻塞
      socket socket = server.accept();
      // 创建一个程序执行客户端的任务
      new thread(new serverhandler(socket)).start();
      
    } catch (ioexception e) {
      e.printstacktrace();
    } finally {
      if(server != null){
        try {
          server.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      server = null;
    }
  }
}

采用多线程来处理接收到的请求(serverhandler):

package com.ietree.basicskill.socket.mode1;

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.socket;

public class serverhandler implements runnable {
  private socket socket;
  public serverhandler(socket socket) {
    this.socket = socket;
  }

  @override
  public void run() {
    bufferedreader in = null;
    printwriter out = null;
    try {
      in = new bufferedreader(new inputstreamreader(this.socket.getinputstream()));
      out = new printwriter(this.socket.getoutputstream(), true);
      string body = null;
      while (true) {
        body = in.readline();
        if(body == null){
          break;
        }
        system.out.println("server: " + body);
        out.println("服务器端回送响应的数据。");
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      if(in != null){
        try {
          in.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      if(out != null){
        try {
          out.close();
        } catch (exception e) {
          e.printstacktrace();
        }
      }
      if(socket != null){
        try {
          socket.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      socket = null;
    }
  }
}

客户端(client):

package com.ietree.basicskill.socket.mode1;

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.socket;

/**
 * 客户端
 */
public class client {
  final static string address = "127.0.0.1";
  final static int port = 8765;
  public static void main(string[] args) {
    socket socket = null;
    bufferedreader in = null;
    printwriter out = null;
    try {
      socket = new socket(address, port);
      in = new bufferedreader(new inputstreamreader(socket.getinputstream()));
      out = new printwriter(socket.getoutputstream(), true);
      
      // 向服务器端发送数据
      out.println("接收到客户端的请求数据......");
      string response = in.readline();
      system.out.println("client: " + response);
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      if(in != null){
        try {
          in.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      if(out != null){
        try {
          out.close();
        } catch (exception e) {
          e.printstacktrace();
        }
      }
      if(socket != null){
        try {
          socket.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      socket = null;
    }
  }
}

程序输出:

server:

server start......
server: 接收到客户端的请求数据......

client:

client: 服务器端回送响应的数据。

同步非阻塞(nio)

异步非阻塞(aio)

以上这篇java网络通信基础编程(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。