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

Java--Socket通信(客户端服务端双向)

程序员文章站 2024-03-07 20:40:57
新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端 两个工程的读写操作线程类基本上完全相同 服务端: import java.io.b...

新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端

两个工程的读写操作线程类基本上完全相同

服务端:

import java.io.bufferedreader; 
import java.io.datainputstream; 
import java.io.dataoutputstream; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
import java.net.serversocket; 
import java.net.socket; 
 
public class server { 
 
  public static final int port = 8000;//监听的端口号   
 
  public static void main(string[] args) {  
    server server = new server();  
    server.init();  
  }  
 
  public void init() {  
    serversocket serversocket = null; 
    try {  
      serversocket = new serversocket(port);  
      while (true) {  
        socket client = serversocket.accept();  
        //一个客户端连接就开户两个线程处理读写  
        new thread(new readhandlerthread(client)).start();  
        new thread(new writehandlerthread(client)).start();  
      }  
    } catch (exception e) {  
      e.printstacktrace();  
    } finally{ 
      try { 
        if(serversocket != null){ 
          serversocket.close(); 
        } 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  }  
}  
 
/* 
 *处理读操作的线程  
 */ 
class readhandlerthread implements runnable{ 
  private socket client; 
 
  public readhandlerthread(socket client) { 
    this.client = client; 
  } 
 
  @override 
  public void run() { 
    datainputstream dis = null; 
    try{ 
      while(true){ 
        //读取客户端数据  
        dis = new datainputstream(client.getinputstream()); 
        string reciver = dis.readutf(); 
        system.out.println("客户端发过来的内容:" + reciver);  
      } 
    }catch(exception e){ 
      e.printstacktrace(); 
    }finally{ 
      try { 
        if(dis != null){ 
          dis.close(); 
        } 
        if(client != null){ 
          client = null; 
        } 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
} 
 
/* 
 * 处理写操作的线程 
 */ 
class writehandlerthread implements runnable{ 
  private socket client; 
 
  public writehandlerthread(socket client) { 
    this.client = client; 
  } 
 
  @override 
  public void run() { 
    dataoutputstream dos = null; 
    bufferedreader br = null; 
    try{ 
      while(true){ 
        //向客户端回复信息  
        dos = new dataoutputstream(client.getoutputstream());  
        system.out.print("请输入:\t");  
        // 键盘录入  
        br = new bufferedreader(new inputstreamreader(system.in)); 
        string send = br.readline();  
        //发送数据 
        dos.writeutf(send);  
      } 
    }catch(exception e){ 
      e.printstacktrace(); 
    }finally{ 
      try { 
        if(dos != null){ 
          dos.close(); 
        } 
        if(br != null){ 
          br.close(); 
        } 
        if(client != null){ 
          client = null; 
        } 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
} 

客户端:

import java.io.bufferedreader; 
import java.io.datainputstream; 
import java.io.dataoutputstream; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
import java.net.socket; 
 
public class client { 
 
  public static final string ip = "localhost";//服务器地址  
  public static final int port = 8000;//服务器端口号  
 
  public static void main(string[] args) {  
    handler();  
  } 
 
  private static void handler(){ 
    try { 
      //实例化一个socket,并指定服务器地址和端口 
      socket client = new socket(ip, port); 
      //开启两个线程,一个负责读,一个负责写 
      new thread(new readhandlerthread(client)).start(); 
      new thread(new writehandlerthread(client)).start(); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
  } 
}  
 
/* 
 *处理读操作的线程  
 */ 
class readhandlerthread implements runnable{ 
  private socket client; 
 
  public readhandlerthread(socket client) { 
    this.client = client; 
  } 
 
  @override 
  public void run() { 
    datainputstream dis = null; 
    try { 
      while(true){ 
        //读取服务器端数据  
        dis = new datainputstream(client.getinputstream()); 
        string receive = dis.readutf();   
        system.out.println("服务器端返回过来的是: " + receive);  
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } finally{ 
      try { 
        if(dis != null){ 
          dis.close(); 
        } 
        if(client != null){ 
          client = null; 
        } 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
} 
 
/* 
 * 处理写操作的线程 
 */ 
class writehandlerthread implements runnable{ 
  private socket client; 
 
  public writehandlerthread(socket client) { 
    this.client = client; 
  } 
 
  @override 
  public void run() { 
    dataoutputstream dos = null; 
    bufferedreader br = null; 
    try { 
      while(true){ 
        //取得输出流 
        dos = new dataoutputstream(client.getoutputstream()); 
        system.out.print("请输入: \t");  
        //键盘录入 
        br = new bufferedreader(new inputstreamreader(system.in)); 
        string send = br.readline();  
        //发送数据 
        dos.writeutf(send);  
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } finally{ 
      try{ 
        if(dos != null){ 
          dos.close(); 
        } 
        if(br != null){ 
          br.close(); 
        } 
        if(client != null){ 
          client = null; 
        } 
      }catch(exception e){ 
        e.printstacktrace(); 
      } 
    } 
  } 
} 

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。