套接字的多线程技术
Posted Kooing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了套接字的多线程技术相关的知识,希望对你有一定的参考价值。
上一次的用的套接字如果不受到数据就会有阻塞
我们可以使用多线程技术,把数据发过去,由服务器端的线程来操作,一个线程操作一个发过去的数据,然后发回来,不会阻塞客户端
测试代码
客户端
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.URL; import java.util.Scanner; public class Test { public static void main(String args[]){ DataInputStream in=null; DataOutputStream out=null; Socket socket1 = null; InetAddress ip = null; Read read1=null; Thread thread1; try{ read1=new Read(); thread1=new Thread(read1); // ip=InetAddress.getByName("127.0.0.1"); // InetSocketAddress ip2=new InetSocketAddress(ip,2010); // System.out.println("3333"); // if(socket1.isConnected()){} // else{ // System.out.println("3333"); // socket1.connect(ip2); // System.out.println("222"); socket1=new Socket("127.0.0.1",2017); in=new DataInputStream(socket1.getInputStream()); out=new DataOutputStream(socket1.getOutputStream()); read1.setInputStream(in); thread1.start(); // } } catch(Exception e){ System.out.println("111"); System.out.println(e.getMessage()); } Scanner scanner1=new Scanner(System.in); for(;scanner1.hasNext();){ double r=scanner1.nextDouble(); try{ out.writeDouble(r); } catch(Exception e){ System.out.println(e.getMessage()); System.out.println("444"); } } } } class Read implements Runnable{ private URL url; private DataInputStream in1; void setInputStream(DataInputStream a){ in1=a; } public void run(){ double result=0; for(;;){ try{ result=in1.readDouble(); System.out.println("受到了"+result); } catch(Exception e){ System.out.println("333"); System.out.println(e.getMessage()); } } } }
服务器端
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class test2{ public static void main(String args[]){ DataInputStream in=null; DataOutputStream out=null; ServerSocket serverSocket1=null; Socket socket1 = null; readAndWrite thread1; for(;;){ try{ serverSocket1=new ServerSocket(2017); } catch(Exception e){ System.out.println("111"); System.out.println(e.getMessage()); } try{ socket1=serverSocket1.accept(); } catch(Exception e){ System.out.println("22"); System.out.println(e.getMessage()); } if(socket1!=null) new readAndWrite(socket1).start(); } } } class readAndWrite extends Thread{ DataInputStream in=null; DataOutputStream out=null; Socket socket1; double a,b; readAndWrite(Socket socket){ socket1=socket; } @Override public void run() { // TODO 自动生成的方法存根 for(;;){ try { in=new DataInputStream(socket1.getInputStream()); out=new DataOutputStream(socket1.getOutputStream()); a=in.readDouble(); System.out.println("受到"+a); b=a*2; try{ Thread.sleep(3000);//故意拖慢,显示结果 } catch(Exception e){ System.out.println(e.toString()); } out.writeDouble(b); } catch (IOException e) { // TODO 自动生成的 catch 块 System.out.println("111"); System.out.println(e.getMessage()); } } } }
效果
以上是关于套接字的多线程技术的主要内容,如果未能解决你的问题,请参考以下文章