Java C/S socket点对点通信

(一)了解Socket
传输层有两种通信模式,一种是面向连接的可靠的通信,另一种是无连接的不可靠的通信,TCP是一种面向连接的保证可靠传输的协议,
通过TCP协议传输,得到的是一个顺序的无差错的数据流。而UDP是无连接的不可靠的通信。
socket 是基于TCP协议的一种可靠的通信,它是应用层和传输层之间的一个通信接口。
发送方和接收方的成对的两个socket之间必须建 立连接,以便在TCP协议的基础上进行通信,当一个socket(通常都是server socket服务器)等待建立连接时,
另一个socket(客户端)可以要求进行连接,一旦这两个socket连接起来,它们就可以进行双向数据传输,双方都可以进行发送 或接收操作。
(二)循环服务器模式
程序功能为:
(1) 服务器可以接受任何客户的连接
(2) 服务器在同一时刻只能与一个客户通信,直到该客户退出才可以接收下一个客户
(3) 客户程序使用命令行参数指定服务器地址
(4) 客户端输入的文本都发送给服务器
(5) 客户使用输入end时停止发送,关闭连接
代码:

public class Service1 { 
    static int num = 1 ;
    public static void main(String[] args) throws IOException{   
        ServerSocket server=new ServerSocket(10086); //创建绑定到特定端口的服务器套接字。  
        System.out.println("....Service Start!");
        while(true){   
            Socket client=server.accept(); //等待客户连接请求
            System.out.println("Client"+num+" Connect!");
            num++ ;
            //读取客户端数据流
            BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));   
            // 获取写往客户端的输出流,true表示自动刷新 
            PrintWriter out=new PrintWriter(client.getOutputStream(),true);   
            while(true){   
                String str=in.readLine();   
                System.out.println("Client: "+str);   
                out.println("has receive....");   
                out.flush();  
                if(str.equals("end")){
                    System.out.println("Client Close!") ;
                    break ;
                }
            }   
            client.close();   
        }   
    }  
}  

(三) 多线程服务器
程序功能为:
(1) 服务器可以接受任何客户的连接
(2) 客户程序使用命令行参数指定服务器地址
(3) 客户端输入的文本都发送给服务器
(4) 客户使用输入end时停止发送,关闭连接
(5)服务器能同时连接多个客户端

import java.io.*;   
import java.net.*;   
public class Service1 { 
    static int num = 1 ;
    public static void main(String[] args) throws IOException{   
        ServerSocket server=new ServerSocket(10086); //创建绑定到特定端口的服务器套接字。  
        System.out.println("....Service Start!");
        while(true){   
            Socket client=server.accept(); //等待客户连接请求
            S_Thread thread = new S_Thread(client) ; //开启一个线程处理连接
            thread.start() ;
        } 
    }  
}
public class S_Thread extends Thread{
    Socket client ;
    public S_Thread(Socket client){
        this.client = client ;
    }
    public void run() {     
        BufferedReader in = null;
        PrintWriter out = null;// 获取写往客户端的输出流,true表示自动刷新 
        try {
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(),true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        while(true){   
            String str = null;
            try {
                str = in.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
            System.out.println("Client: "+str);   
            out.println("has receive....");   
            out.flush();  
            if(str.equals("end")){
                System.out.println("Client Close!") ;
                break ;
            }
        }
        try {
            client.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

(四)客户端

public class Client { 
     Socket so ;
     BufferedReader netin ;
     BufferedReader usein;
     PrintWriter out;   
    public static void main(String[] args) throws Exception {
    Client client = new Client();   //创建一个客户端
    client.runMain() ;
    }
    public void runMain() {
        try { 
            //创建连接指定服务器和端口的Socket
           so= new Socket("localhost",10086);
           System.out.println("...Connect"); 
           //创建键盘输入的数据流
           usein  = new  BufferedReader(new InputStreamReader(System.in));
           //创建发送到服务器端的数据流
           out= new PrintWriter(so.getOutputStream());

            boolean flag=true;
            while(flag) {           

                String theLine=usein.readLine();
                //通过流发送信息给服务器端
                out.println( theLine);
                out.flush();

                BufferedReader in=new BufferedReader(new InputStreamReader(so.getInputStream())); 
                String str=in.readLine();   
                System.out.println("Server:"+str); 

                if(theLine.equals("end")){
                    so.close() ;
                    break ;
                }       
             }  
            }
        catch(Exception e) {            
        }
    }
}

(五)
详细可运行代码:
(http://download.csdn.net/download/hdfgo/10186745)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值