Socket编程实例

用套接字实现客户——服务器交互计算,客户端输入三角形三边长并发送给服务器,服务器把计算出的三角形面积返回给客户。

TCPserver.java

package test9;
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class TCPserver { 
    public static final int NUM = 3; 
    public static void main(String args[]){ 
         
        TCPserver TCPserver = new TCPserver(); 
        try { 
            ServerSocket server = null; 
             
            try { 
                server = new ServerSocket(6666); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
             
            Socket socket = null; 
            try { 
                socket = server.accept(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
             
            String line = ""; 
            double a[] = new double[NUM]; 
            BufferedReader is = new BufferedReader(new InputStreamReader 
                    (socket.getInputStream())); 
             
            System.out.println("从客户端接收三角形的三条边:"); 
            for (int i = 0; i < a.length; i++) { 
                line = is.readLine(); 
                a[i] = Double.parseDouble(line); 
            } 
             
             
            System.out.println("三角形的面积是:" + TCPserver.countarea(a)); 
             
            is.close();//关闭读取流  
            socket.close();//关闭Socket  
            server.close();//关闭ServerSocket  
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
     
     
    //计算三角形面积的方法  
    public double countarea(double a[]){ 
        double area = 0.0; 
         
        double temp = 0.0; 
        double aside = 0.0; 
        double bside = 0.0; 
        double cside = 0.0; 
        aside = a[0]; 
        bside = a[1]; 
        cside = a[2]; 
        temp = a[0] + a[1] + a[2]; 
        temp = temp / 2; 
         
         
         
        area = Math.sqrt(temp * (temp - aside) * (temp - bside) * (temp - cside)); 
         
        return area; 
    } 
} 

TCPclient.java

package test9;

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
public class TCPclient { 
     
    public static final int NUM = 3; 
    public static void main(String args[]){ 
        try { 
            Socket socket = new Socket("127.0.0.1", 6666); 
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); 
             
            PrintWriter os = new PrintWriter(socket.getOutputStream()); 
             
            System.out.println("从客户端接收三角形的三条边发送到服务器:"); 
            System.out.println("请输入3条边:"); 
            String readline = ""; 
            for (int i = 0; i < NUM; i++) { 
                os.flush(); 
                readline = sin.readLine(); 
                os.println(readline); 
            } 
             
            os.close(); 
            socket.close(); 
             
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
     
}
  1. 两者放到同一目录下
  2. 先编译TCPserver.java
  3. 在编译运行TCPclient.java
  4. 依次输入三角形三条边的长

你可能感兴趣的:(Socket编程实例)