简体   繁体   English

如何向套接字客户端发送消息

[英]How to send message to socket client

I would like to add a method to send a String of message to client, it was hard to find a good example on the web. 我想添加一种向客户端发送消息字符串的方法,很难在网络上找到一个很好的例子。 Does it use write for sending message to port 8090, do I need to add flush as well? 它是否使用write将消息发送到端口8090,是否还需要添加flush?

try {
    InetAddress addr = InetAddress.getByName("127.0.0.1");
    int port = 8090;

    Socket socket = new Socket(addr, port);


} catch (UnknownHostException e) {
} catch (IOException e) {
}

查看“ 自定义网络”教程中的一些可用示例。

You can get the output stream object from the method socket.getOutputStream(). 您可以从方法socket.getOutputStream()获取输出流对象。 Once you get the output stream you can write using the stream. 一旦获得输出流,就可以使用该流进行写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Meteor中的消息服务器套接字发送到Java中的客户端 - Send message server socket in Meteor to client in Java 尝试向客户端套接字发送消息时发生NullPointerException - NullPointerException when trying to send a message to a Client Socket Spring Integration和TCP服务器套接字 - 如何向客户端发送消息? - Spring Integration and TCP server socket - how can I send a message to a client? 如何使用 Socket.IO-client Java 从 Android 向服务器发送 String 消息? - How send String message to server from Android with Socket.IO-client Java? 套接字的ArrayList无法将消息发送到客户端-Java - ArrayList of Socket doesn't work to send message to Client - Java (Java 服务器套接字)每分钟向客户端发送消息 - (Java server socket) send message every minute to a client 在java中在两个客户端套接字之间发送消息的步骤 - steps to send message betwwen two client socket in java 通过 Socket Outputstream 从 Swift 客户端向 Java 服务器发送消息 - Send message through Socket Outputstream from Swift Client to Java Server 在套接字中发送停止消息 - Send a stop message in a socket 如何使服务器在连接时向客户端发送消息? - How to get a server to send a client a message on connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM