WEB开发网
开发学院软件开发Java Java网络编程从入门到精通(29):服务端Socket的... 阅读

Java网络编程从入门到精通(29):服务端Socket的选项

 2009-09-22 00:00:00 来源:WEB开发网   
核心提示: packageserver;importjava.net.*;publicclassTestReceiveBufferSize{publicstaticvoidmain(String[]args)throwsException{ServerSocketserverSocket=newServer

package server;
import java.net.*;
public class TestReceiveBufferSize
{
    public static void main(String[] args) throws Exception
    {
        ServerSocket serverSocket = new ServerSocket(1234);
        serverSocket.setReceiveBufferSize(2048); // 将接收缓冲区设为2K
        while (true)
        {
            Socket socket = serverSocket.accept();
            // 如果客户端请求使用的是本地IP地址,重新将Socket对象的接
            // 收缓冲区设为1K            
            if (socket.getInetAddress().isLoopbackAddress())
                socket.setReceiveBufferSize(1024);
            System.out.println("serverSocket:"
                            + serverSocket.getReceiveBufferSize());
            System.out.println("socket:" + socket.getReceiveBufferSize());
            socket.close();
        }
    }
}

执行如下命令:

java server.TestReceiveBufferSize
执行如下三个命令 (192.168.18.100为本机IP地址):
telnet 192.168.18.100 1234
telnet localhost 1234
telnet 192.168.18.100 1234

运行结果:

serverSocket:2048
socket:2048
serverSocket:2048
socket:1024
serverSocket:2048
socket:2048

上一页  1 2 3 4 5 6  下一页

Tags:Java 网络编程 入门

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接