Socket向服务端发送消息工具类

本文介绍了一个Java Socket客户端的实现示例,包括如何连接到指定的主机和端口,发送消息,并接收服务器响应的方法。此外还提供了两种模式:一种是发送消息并等待回复,另一种是仅发送消息不等待回复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yanek.util.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

/**
 * Socket客户端,向服务端发送消息
 * 
 *
 */
public class SocketClient {
 private static Logger logger = Logger.getLogger(SocketClient.class
   .getName());
 private static final int MAX_TIMEOUT = 10;
 
 private SocketClient() {
 }

 /**
  * 向服务端发送消息
  *
  * @param host
  *            主机Host或IP
  * @param port
  *            端口
  * @param timeout
  *            超时,单位秒
  * @param content
  *            发送内容
  */
 public static void send(String host, int port, int timeout, String content) {
  Socket s = null;
  PrintWriter out = null;
  try {
   s = new Socket(host, port);
   s
     .setSoTimeout((timeout > MAX_TIMEOUT ? MAX_TIMEOUT
       : timeout) * 1000);
   out = new PrintWriter(s.getOutputStream());
   out.write(content);
   out.flush();
  } catch (Exception e) {
   logger.error(e);
  } finally {
   if (s != null)
    try {
     s.close();
    } catch (IOException e) {
    }
   if (out != null)
    out.close();
   s = null;
   out = null;
  }
 }

 /**
  * 向SocketServer发送通信指令并获取回复数据
  *
  * @param host 主机名称或IP
  * @param port 端口
  * @param timeout 超时时间(秒)
  * @param content 指令内容
  * @return
  */
 public static String sendAndGetReply(String host, int port, int timeout,
   String content) {
  String encode = "utf-8";
  Socket s = null;
  BufferedReader in = null;
  PrintWriter out = null;
  String line = null;
  try {
   content = URLEncoder.encode(content, encode);
   s = new Socket(host, port);
   s
     .setSoTimeout((timeout > MAX_TIMEOUT ? MAX_TIMEOUT
       : timeout) * 1000);
   in = new BufferedReader(new InputStreamReader(s.getInputStream()));
   out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s
     .getOutputStream())), true);
   out.println(content);
   line = in.readLine();
  } catch (Exception e) {
   logger.error(e);
  } finally {
   if (s != null)
    try {
     s.close();
    } catch (IOException e) {
    }
   if (out != null)
    out.close();
   if (in != null)
    try {
     in.close();
    } catch (IOException e) {
    }
   s = null;
   out = null;
   in = null;
  }
  try {
   line = URLEncoder.encode(line, encode);
  } catch (UnsupportedEncodingException e) {
   logger.error(e);
  }
  return line;
 }
 
 /**
  * 向SocketServer发送通信指令,无同步回复消息
  *
  * @param host 主机名称或IP
  * @param port 端口
  * @param timeout 超时时间(秒)
  * @param content 指令内容
  * @return
  */
 public static void sendAndNoReply(String host, int port, int timeout,
   String content) {
  String encode = "utf-8";
  Socket s = null;
  PrintWriter out = null;
  try {
   content = URLEncoder.encode(content, encode);
   s = new Socket(host, port);
   s
     .setSoTimeout((timeout > MAX_TIMEOUT ? MAX_TIMEOUT
       : timeout) * 1000);
   out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s
     .getOutputStream())), true);
   out.println(content);
  } catch (Exception e) {
   logger.error(e);
  } finally {
   if (s != null)
    try {
     s.close();
    } catch (IOException e) {
    }
   if (out != null)
    out.close();
   s = null;
   out = null;
  }
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值