socket.shutdownOutput()的用途 [英] purpose of socket.shutdownOutput()

查看:1060
本文介绍了socket.shutdownOutput()的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将数据发送到tcp服务器。我假设我需要使用 socket.shutdownOutput()来正确指示客户端完成发送请求。我的假设是否正确?如果不是,请让我知道 shutdownOutput()的用途。

客户

  DEF地址=新的InetSocketAddress(tcpIpAddress,TCPPORT为整数)
ClientSocket的=新的Socket()
clientSocket.connect(地址,FIVE_SECONDS)
clientSocket.setSoTimeout(FIVE_SECONDS)

//写入服务器时默认为4K
BufferedOutputStream outputStream = new BufferedOutputStream(clientSocket.getOutputStream(),4096)

//对数据进行编码
final byte [] bytes = reqFFF.getBytes(8859_1)
outputStream.write(bytes,0,bytes.length)
outputStream.flush()
clientSocket.shutdownOutput( )

服务器

  ServerSocket welcomeSocket = new ServerSocket(6789)

while(true)
{
println准备好接受连接
Socket connectionSocket = welcomeSocket.accept()
println接受客户端请求
BufferedInput流inFromClient =新的BufferedInputStream(connectionSocket.getInputStream())
的BufferedOutputStream outToClient =新的BufferedOutputStream(connectionSocket.getOutputStream())
ByteArrayOutputStream BOS =新ByteArrayOutputStream()

的println阅读数据逐字节
byte b = inFromClient.read()
while(b!= - 1)
{
bos.write(b)
b = inFromClient .read()
}
String s = bos.toString()

println(接收到的请求:[+ s +])

def resp =InvalidInput
if(s ==hit){resp =some data}

printlnSending resp:[+ resp +]

outToClient.write(resp.getBytes());
outToClient.flush()
}


解决方案

Socket.shutdownOutput() 表示客户端通过TCP连接完成发送任何数据。它会发送剩余的数据,然后是一个完全关闭OUTGOING连接的终止序列。无法发送任何更多数据,这也会向您的程序指出请求已完成。因此,如果您确定不需要发送更多数据,建议您使用它。



但不需要指出请求已完成(您没有如果您有多个请求,则一直打开/关闭输出),还有其他方法。


I am using the below code to send data to a tcp server. I am assuming that I need to use socket.shutdownOutput() to properly indicate that the client is done sending the request. Is my assumption correct? If not please let me know the purpose of shutdownOutput(). Also appreciate any further optimizations I can make.

Client

def address = new InetSocketAddress(tcpIpAddress, tcpPort as Integer)
clientSocket = new Socket()
clientSocket.connect(address, FIVE_SECONDS)
clientSocket.setSoTimeout(FIVE_SECONDS)

// default to 4K when writing to the server
BufferedOutputStream outputStream = new BufferedOutputStream(clientSocket.getOutputStream(), 4096)

//encode the data
final byte[] bytes = reqFFF.getBytes("8859_1")
outputStream.write(bytes,0,bytes.length)
outputStream.flush()
clientSocket.shutdownOutput()

Server

ServerSocket welcomeSocket = new ServerSocket(6789)

while(true)
{
    println "ready to accept connections"
    Socket connectionSocket = welcomeSocket.accept()
    println "accepted client req"
    BufferedInputStream inFromClient = new BufferedInputStream(connectionSocket.getInputStream())
    BufferedOutputStream outToClient = new BufferedOutputStream(connectionSocket.getOutputStream())
    ByteArrayOutputStream bos=new ByteArrayOutputStream()

    println "reading data byte by byte"  
    byte b=inFromClient.read()    
    while(b!=-1)
    {        
       bos.write(b)
       b=inFromClient.read()
    }
    String s=bos.toString()

    println("Received request: [" + s +"]")   

    def resp = "InvalidInput"
    if(s=="hit") { resp = "some data" }

    println "Sending resp: ["+resp+"]"

    outToClient.write(resp.getBytes());
    outToClient.flush()
}

解决方案

Socket.shutdownOutput() means that the client is finished sending any data through the TCP connection. It will send the remaining data followed by a termination sequence which will completely close its OUTGOING connection. It is not possible to send any further data, which will also indicate to your program that the request is completely finished. So its recommended if you are sure you don't have to send any more data.

But it's not needed to indicate that the request is finished (you don't have to open/close the output all the time if you have multiple requests), there are other ways.

这篇关于socket.shutdownOutput()的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆