(一)Socket 编程 | Java 网络编程

Java 网络编程

(一)Socket 编程 | Java 网络编程

Socket 基本使用

        String hostName = p.getProperty("ServerIP"); // 获取服务器端IP,如:10.0.70.30
        String port = p.getProperty("ServerPort"); // 获取端口号,如:8080
        try {
            socket = new Socket(hostName, Integer.parseInt(port)); // 创建套接字对象
            oos = new ObjectOutputStream(socket.getOutputStream()); // 创建对象输出流
            ois = new ObjectInputStream(socket.getInputStream()); // 创建对象输入流
        } catch (Exception e) {
            JOptionPane.showMessageDialog(new JFrame(), "网络连接失败,请检查配置参数!");
            try {
                socket.close(); // 关闭套接字对象
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        }

使用案例

package com.hk.client;

import com.hk.client.login.LoginFrame;
import com.hk.server.model.Oper;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Properties;

/**
 * @author: shipleyleo
 * @create: 2023-05-27 18:22:36
 */
public class ClientMainClass {
    public static Socket socket; // 套接字
    public static ObjectInputStream ois; // Socket 对象输入流
    public static ObjectOutputStream oos; // Socket 对象输出流
    public static List flights; // 航班对象集合
    public static Oper currentUser; // 当前登录的网点
    public static ClientMainFrame clientFrame; // 客户端主界面
//    public static OrderFrame currentOrderFrame; // 当前订单界面

    public static void init() {
        Properties p = new Properties(); // 创建配置文件对象
        try {
            p.load(new FileInputStream(System.getProperty("user.dir") + "\\src\\com\\hk\\client\\client.properties")); // 读取配置文件
        } catch (IOException e) {
            JOptionPane.showMessageDialog(new JFrame(), "配置文件丢失或已损坏,请成功新定义配置文件!");
            System.exit(0);
        }

        String hostName = p.getProperty("ServerIP"); // 获取服务器端IP
        String port = p.getProperty("ServerPort"); // 获取端口号
        try {
            socket = new Socket(hostName, Integer.parseInt(port)); // 创建套接字对象
            oos = new ObjectOutputStream(socket.getOutputStream()); // 创建对象输出流
            ois = new ObjectInputStream(socket.getInputStream()); // 创建对象输入流
        } catch (Exception e) {
            JOptionPane.showMessageDialog(new JFrame(), "网络连接失败,请检查配置参数!");
            try {
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        ClientMainClass.init(); // 调用初始化方法
        new LoginFrame().showMe(); // 显示登录界面
    }

}


参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值