java socket网络编程(多线程技术)

Posted 我有一壶酒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java socket网络编程(多线程技术)相关的知识,希望对你有一定的参考价值。

Client.java

import java.io.*;
import java.net.*;
import java.util.*;
public class Client  {
   public static void main(String args[]) {
      Scanner scanner = new Scanner(System.in);
      Socket mysocket=null;
      DataInputStream in=null;
      DataOutputStream out=null;
      Thread readData ; 
      Read read=null;
      try{  mysocket=new Socket();
            read = new Read();
            readData = new Thread(read);
            System.out.print("输入服务器的IP:");
            String IP = scanner.nextLine();
            System.out.print("输入端口号:");
            int port = scanner.nextInt();
            if(mysocket.isConnected()){}
            else{
              InetAddress  address=InetAddress.getByName(IP);
              InetSocketAddress socketAddress=new InetSocketAddress(address,port);
              mysocket.connect(socketAddress); 
              in =new DataInputStream(mysocket.getInputStream());
              out = new DataOutputStream(mysocket.getOutputStream());
              read.setDataInputStream(in);
              readData.start();
            }
       }
       catch(Exception e) {
            System.out.println("服务器已断开"+e);
       }
       System.out.print("输入园的半径(放弃请输入N):");
       while(scanner.hasNext()) {
           double radius=0; 
           try {
               radius = scanner.nextDouble();
           }
           catch(InputMismatchException exp){
              System.exit(0);
           }   
           try { 
               out.writeDouble(radius);
           }
           catch(Exception e) {}
       } 
   }
}

Read.java

import java.io.*;
public class Read implements Runnable {
   DataInputStream in;
   public void setDataInputStream(DataInputStream in) {
      this.in = in;
   }
   public void run() {
      double result=0;
      while(true) {
        try{ result=in.readDouble();
             System.out.println("圆的面积:"+result);
             System.out.print("输入园的半径(放弃请输入N):");
        }
        catch(IOException e) { 
             System.out.println("与服务器已断开"+e);
             break;
        }   
      }
   }
}

Server.java

import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
   public static void main(String args[]) {
      ServerSocket server=null;
      ServerThread thread;
      Socket you=null;
      while(true) {
        try{  server=new ServerSocket(2010);
        }
        catch(IOException e1) { 
              System.out.println("正在监听"); //ServerSocket对象不能重复创建
        } 
        try{  System.out.println(" 等待客户呼叫");
              you=server.accept();
              System.out.println("客户的地址:"+you.getInetAddress());
        } 
        catch (IOException e) {
              System.out.println("正在等待客户");
        }
        if(you!=null) { 
              new ServerThread(you).start(); //为每个客户启动一个专门的线程  
        }
      }
   }
}
class ServerThread extends Thread {
   Socket socket;
   DataOutputStream out=null;
   DataInputStream  in=null;
   String s=null;
   ServerThread(Socket t) {
      socket=t;
      try {  out=new DataOutputStream(socket.getOutputStream());
             in=new DataInputStream(socket.getInputStream());
      }
      catch (IOException e){}
   }  
   public void run() {        
      while(true) {
         try{  double r=in.readDouble();//堵塞状态,除非读取到信息
               double area=Math.PI*r*r;
               out.writeDouble(area);
         }
         catch (IOException e) {
               System.out.println("客户离开");
                return;
         }
      }
   } 
}

 

以上是关于java socket网络编程(多线程技术)的主要内容,如果未能解决你的问题,请参考以下文章

NO.71 一连接一线程 : 基于Socket的服务端的多线程模式

Java多线程与并发库4.传统线程同步通信技术

Java编程实例-tcp聊天室代码实现

Java Socket编程 标准范例(多线程)

java并发编程之上下文切换等并发编程的挑战

廖雪峰Java13网络编程-1Socket编程-3TCP多线程编程