Socket编程

对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket。服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了。首先ServerSocket将在服务端监听某个端口,当发现客户端有Socket来试图连接它时,它会accept该Socket的连接请求,同时在服务端建立一个对应的Socket与之进行通信。这样就有两个Socket了,客户端和服务端各一个。


Socket编程_第1张图片
Socket模型

Socket编程

 Server端五步

  1.启动Server  ServerSocket serverSocket = new ServerSocket(port)
  2.侦听连接    Socket socket = serverSocket.accept();
  3.读取数据    BufferedReader input = new BufferedReader(new 
                InputStreamReader(socket.getInputStream()));
                input.readLine();
   4.写数据    BufferedWriter output = new BufferedWriter(new
                     OutputStreamWriter(socket.getOutputStream()));
                output.write();
  5.关闭连接    socket.close();
                serverSocket.close();

 Client端四步 

1.启动Server  ServerSocket serverSocket = new ServerSocket(port)
2.侦听连接  Socket socket = serverSocket.accept();
3.读取数据  BufferedReader input = new BufferedReader(new 
              InputStreamReader(socket.getInputStream()));
              input.readLine();
4.写数据      BufferedWriter output = new BufferedWriter(new
               OutputStreamWriter(socket.getOutputStream()));
              output.write();
5.关闭连接  socket.close();
            serverSocket.close();

下面实现一个简单的点对点的聊天,输入“bye”时,聊天结束

Sever.java

package com.day1006.oo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Server {
    public static void main(String[] args) {
        try {
            System.out.println("正在启动服务器...");
            ServerSocket serverSocket  = new ServerSocket(1000);
            System.out.println("服务器启动成功...");
            
            System.out.println("等待连接...");
            Socket socket = serverSocket.accept();
            System.out.println("连接成功...");
            
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            
            Scanner sc = new Scanner(System.in);
            boolean shouldContinue =true;
            String clientMsg;
            String serverMsg = null;
            while(shouldContinue){
                clientMsg = input.readLine();
                System.out.println("client:"+clientMsg);
                
                if(!"bye".equals(clientMsg)){
                    System.out.print("请输入回复:");
                    serverMsg = sc.nextLine();
                    System.out.println("server:"+serverMsg);
                    output.write(serverMsg+"\r");
                    output.flush();
                }
                
                if("bye".equals(serverMsg)|| "bye".equals(clientMsg)){
                    shouldContinue = false;
                }
            }
            
            
            
            serverSocket.close();
            sc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Client.java

package com.day1006.oo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        try {
            System.out.println("等待连接...");
            Socket socket = new Socket("localhost", 1000);
            System.out.println("连接成功...");

            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            Scanner sc = new Scanner(System.in);
            boolean shouldContinue = true;
            String clientMsg;
            String serverMsg = null;

            while (shouldContinue) {
                clientMsg = sc.nextLine();
                System.out.println("client:" + clientMsg);
                output.write(clientMsg + "\r");
                output.flush();
                if (!"bye".equals(clientMsg)) {
                    serverMsg = input.readLine();
                    System.out.println("server:" + serverMsg);
                }
                if ("bye".equals(serverMsg) || "bye".equals(clientMsg)) {
                    shouldContinue = false;
                }
            }

            sc.close();
            socket.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

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