皮皮网

【asp投资理财源码】【口红小精灵源码】【eclipse怎么追踪源码】javasocket类源码_java socket源码

时间:2024-12-28 19:52:26 分类:百科 来源:android图库相册源码

1.java socket网络编程
2.用Java Socket编程,类源实现简单的码j码asp投资理财源码Echo功能

javasocket类源码_java socket源码

java socket网络编程

       //==============Server.java=================//

       import java.io.IOException;

       import java.net.ServerSocket;

       import java.net.Socket;

       public class Server {

        public static void main(String[] args) throws IOException {

        ServerSocket s = new ServerSocket();

        System.out.println("服务器就绪,请启动客户端.");

        Socket so = s.accept();

        byte[] buff = new byte[];

        int read = so.getInputStream().read(buff);

        String[] abc=new String(buff,0,read).split("\\D+");

        int a = Integer.parseInt(abc[0]);

        int b = Integer.parseInt(abc[1]);

        int c = Integer.parseInt(abc[2]);

        if(!cbt(a,b,c))

        so.getOutputStream().write("输入的数据无法组成三角形.".getBytes());

        else

        so.getOutputStream().write(getArea(a,b,c).getBytes());

        so.getOutputStream().flush();

        so.close();

        s.close();

        }

        private static String getArea(int a, int b, int c) {

        float s = (a+b+c)/2f;

        return "面积: "+Math.sqrt(s*(s-a)*(s-b)*(s-c));

        }

        private static boolean cbt(int a, int b, int c) {

        return a>0&&b>0&&c>0&&a+b>c&&b+c>a&&a+c>b;

        }

       }

       //=================Client.java======================//

       import java.io.IOException;

       import java.net.Socket;

       import java.net.UnknownHostException;

       public class Client {

        public static void main(String[] args) throws UnknownHostException, IOException {

        System.out.println("输入三角形的三边并用逗号隔开,如: (3,4,5) ");

        byte[] buff=new byte[];

        int r = System.in.read(buff);

        String ipaddr = "localhost";//根据情况改变,在本机调试就不改了

        Socket so = new Socket(ipaddr,);

        so.getOutputStream().write(new String(buff,0,r).getBytes());

        r = so.getInputStream().read(buff);

        so.close();

        String rs = new String(buff,0,r);

        System.out.println(rs);

        }

       }

       //先启动Server,再启动Client

用Java Socket编程,实现简单的类源口红小精灵源码Echo功能

       // 服务器

       import java.io.*;

       import java.net.*;

       public class MyServer {

        public static void main(String[] args) throws IOException{

        ServerSocket server=new ServerSocket();

        while (true) {

        Socket client=server.accept();

        BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));

        PrintWriter out=new PrintWriter(client.getOutputStream());

        while(true){

        String str=in.readLine();

        System.out.println(str);

        out.println("has receive....");

        out.flush();

        if(str.equals("exit"))

        break;

        }

        client.close();

        }

        }

       }

       // 客户端

       import java.net.*;

       import java.io.*;

       public class Client{

        static Socket server;

        public static void main(String[] args)throws Exception{

        server=new Socket(InetAddress.getLocalHost(),);

        BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));

        PrintWriter out=new PrintWriter(server.getOutputStream());

        BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));

        while(true){

        String str=wt.readLine();

        out.println(str);

        out.flush();

        if(str.equals("end")){

        break;

        }

        System.out.println(in.readLine());

        }

        server.close();

        }

       }

copyright © 2016 powered by 皮皮网   sitemap