java tcp链接慢_JAVA TCP套接字:数据传输速度慢

我用serversocket设置了一个服务器,并用客户机连接到它。它们通过交换机直接联网,ping时间为<1毫秒。

现在,我尝试通过套接字的输出流将“大量”数据从客户端推送到服务器。传输0.6GB需要23分钟。我可以通过SCP在几秒钟内推一个更大的文件。

知道我做错了什么吗?我基本上只是在套接字上循环和调用writeint。速度问题与数据来自何处无关,即使我只是发送一个常量整数而不是从磁盘读取。

我试着将两边的发送和接收缓冲区设置为4MB,没有骰子。我为读写器使用缓冲流,没有骰子。

我遗漏了什么吗?

编辑:代码

这是我做插座的地方

System.out.println("Connecting to " + hostname);

serverAddr = InetAddress.getByName(hostname);

// connect and wait for port assignment

Socket initialSock = new Socket();

initialSock.connect(new InetSocketAddress(serverAddr, LDAMaster.LDA_MASTER_PORT));

int newPort = LDAHelper.readConnectionForwardPacket(new DataInputStream(initialSock.getInputStream()));

initialSock.close();

initialSock = null;

System.out.println("Forwarded to " + newPort);

// got my new port, connect to it

sock = new Socket();

sock.setReceiveBufferSize(RECEIVE_BUFFER_SIZE);

sock.setSendBufferSize(SEND_BUFFER_SIZE);

sock.connect(new InetSocketAddress(serverAddr, newPort));

System.out.println("Connected to " + hostname + ":" + newPort + " with buffers snd=" + sock.getSendBufferSize() + " rcv=" + sock.getReceiveBufferSize());

// get the MD5s

try {

byte[] dataMd5 = LDAHelper.md5File(dataFile),

indexMd5 = LDAHelper.md5File(indexFile);

long freeSpace = 90210; // ** TODO: actually set this **

output = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));

input = new DataInputStream(new BufferedInputStream(sock.getInputStream()));

我在这里进行服务器端连接:

ServerSocket servSock = new ServerSocket();

servSock.setSoTimeout(SO_TIMEOUT);

servSock.setReuseAddress(true);

servSock.bind(new InetSocketAddress(LDA_MASTER_PORT));

int currPort = LDA_START_PORT;

while (true) {

try {

Socket conn = servSock.accept();

System.out.println("Got a connection. Sending them to port " + currPort);

clients.add(new MasterClientCommunicator(this, currPort));

clients.get(clients.size()-1).start();

Thread.sleep(500);

LDAHelper.sendConnectionForwardPacket(new DataOutputStream(conn.getOutputStream()), currPort);

currPort++;

} catch (SocketTimeoutException e) {

System.out.println("Done listening. Dispatching instructions.");

break;

}

catch (IOException e) {

e.printStackTrace();

}

catch (Exception e) {

e.printStackTrace();

}

}

好吧,这里是我运送超过0.6GB数据的地方。

public static void sendTermDeltaPacket(DataOutputStream out, TIntIntHashMap[] termDelta) throws IOException {

long bytesTransferred = 0, numZeros = 0;

long start = System.currentTimeMillis();

out.write(PACKET_TERM_DELTA); // header

out.flush();

for (int z=0; z < termDelta.length; z++) {

out.writeInt(termDelta[z].size()); // # of elements for each term

bytesTransferred += 4;

}

for (int z=0; z < termDelta.length; z++) {

for (int i=0; i < termDelta[z].size(); i++) {

out.writeInt(1);

out.writeInt(1);

}

}

到目前为止似乎很简单…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值