当前位置:编程学习 > JAVA >>

[java]网络编程(Socket编程、套接字编程)

Socket类----实现客户端套接字

                   ----套接字是两台机器间通信的端点,用于描述IP地址和端口,应用程序通常通过“套接字”向网络发送请求                         或者应答网络请求

                   ----构造方法:Socket(InetAdress address,int port)---创建一个流套接字,并将其连接到指定IP地址的端口                        号

模拟客户端


[java]
package guo; 
 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.Socket; 
 
public class Client1 { 
 
    public static void main(String[] args)  
    { 
        try 
        { 
            Socket socket = new Socket("127.0.0.1",8000); 
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); 
            BufferedReader in1 = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            while(true) 
            { 
                /*从键盘读一行*/ 
                String str = in.readLine(); 
                if(str==null || "".equals(str))break; 
                /*写给服务器端*/ 
                out.println(str); 
                String str1 = in1.readLine(); 
                System.out.println(str1); 
 
            } 
            socket.close(); 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 

模拟服务端

[java] 
package guo; 
 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class Service1 { 
 
    public static void main(String[] args)  
    { 
        try 
        { 
            /*开辟一个端口就是开启一个服务了---端口的分配必须是唯一的,因为端口是为了唯一标识每台计算机唯一服务的
             * 端口号是从0~65535之间的,前1024个端口已被Tcp/Ip作为保留端口,因此自己分配的端口只能是1024以后的*/ 
            ServerSocket s = new ServerSocket(8000); 
            System.out.println("服务已经开启,等待客户来访问!"); 
            /*等待客户来访问,如果没有客户就阻塞,一直等待*/ 
            Socket socket = s.accept(); 
            System.out.println("接受到客户的访问!"); 
            /*服务器从Socket的输入流中读取数据*/ 
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in)); 
            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); 
            while(true) 
            { 
                //从socket的输入流中进行读操作 
                String str = in.readLine(); 
                if("exit".equals(str))break; 
                /*打印到控制台*/ 
                System.out.println(str); 
                /*服务器端键盘*/ 
                String str1 = in1.readLine(); 
                out.println(str1); 
            } 
            socket.close(); 
            s.close(); 
 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 


补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,