Socket连接入门

服务器接收客户端请求步骤: 
  1.创建一个ServerSocket实例,监听客户端发来的请求。 
  2.与客户端获取连接后,创建一个Socket实例,利用I/O流与客户端进行通信,完毕后关闭Socket。

  当然,服务器可以接收多个客户端的请求,所以如果服务器是一个一个顺序相应肯定会带来不好的体验,因此使用多线程来为多个客户端提供服务 

package com.socket;
 
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class SocketServer {
    public static void main(String[] arg){
        try{
            int count = 0;
            //穿件服务器socket,绑定端口,并监听此端口
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("***服务器已启动,等待客户端连接***");
            //循环监听等待客户端连接
            while(true){
                //调用accept()方法开始监听,等待客户端连接
                Socket socket =serverSocket.accept();
                //创建一个新的线程
                ServerThread serverThread = new ServerThread(socket);
                //启动线程
                serverThread.start();
                count++;
                System.out.println("客户端的数量为:"+count);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

多线程类处理请求:

package com.socket;
 
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * 服务器线程处理类
 */
public class ServerThread extends  Thread {
    //和本线程相关的Socket
    Socket socket = null;
    public ServerThread(Socket socket){
        this.socket = socket;
    }
    //线程执行操作,响应客户端请求
    public void run(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            //3、获取输入流,并读取客户端信息
            is = socket.getInputStream();
 
            isr = new InputStreamReader(is);//将字节流包装为字符流
            br = new BufferedReader(isr);//为输入流添加缓冲
            String info = null;
            while((info = br.readLine()) != null){
                System.out.println("我是服务器,客户端说:" + info);
            }
            socket.shutdownInput();//关闭输入流
            //4、获取输出流,响应客户端请求
            os = socket.getOutputStream();
            pw = new PrintWriter(os);//包装为打印流
            pw.write("欢迎您,服务器接收到你信息。");
            pw.flush();
 
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if ( pw != null)
                    pw.close();
                if(os != null)
                    os.close();
                if(br != null)
                    br.close();
                if(isr != null)
                    isr.close();
                if(is !=null)
                    is.close();
                if(socket != null)
                    socket.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

 

客户端向服务器发送请求可分为以下步骤: 
  1.创建一个Socket实例 
  2.利用I/O流与服务器进行通信 
  3.关闭socket 

package com.socket;
 
import java.io.*;
import java.net.Socket;
 
/**
 * 客户端
 */
public class cline {
    public  static  void main(String[] arg){
        try {
            //1、创建客户端Socket,目标服务器地址和端口
            Socket socket = new Socket("localhost",8888);
            //2、获取输出流,向服务器发送信息
            OutputStream os = socket.getOutputStream();//字节输出流
            PrintWriter pw = new PrintWriter(os);
            pw.write("用户名:admin  ; 密码:5555");
            pw.flush();
            socket.shutdownOutput();//关闭输出流
 
            //3、接收服务器的响应信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while((info = br.readLine()) != null){
                System.out.println("我是客户端,服务器端说:"+info);
            }
            br.close();
            is.close();
 
            pw.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值