楼主的意思是说速度慢吗?差别不可能这样大,可能是你的代码有问题。

解决方案 »

  1.   

    下面是我的代码,请各位大虾看看问题处在什么地方,服务器方是用C++Builder写的一个简单的服务程序,接受后马上返回:
    import java.util.*;
    import java.io.*;
    import java.net.*;
    import java.lang.*;public class StreamSocket implements SocketBase {
    String m_sHost;
    int m_nPort;
    private Socket m_SocketInstance;
    private OutputStream m_OutStream;
    private InputStream m_InStream; public StreamSocket(String sHost, int nPort) {
    m_sHost = sHost;
    m_nPort = nPort;
    }
    //main function for testing
    public static void main(String[] args) {
    StreamSocket streamSocket1 = new StreamSocket("127.0.0.1", 12345);
    streamSocket1.connect();
    long nowtm = System.currentTimeMillis();
    try{
    for(long i = 1; i< 10000000; i++)
    {
    String s= "this is a test";
    byte[] readbuf = new byte[40];
    if(streamSocket1.write(s.getBytes(), 0, s.length(), 0) == -1)
    {
    System.out.println("write err");
    break;
    }
    //System.out.println("Begin to read....");
    if(streamSocket1.read(readbuf, 40, 1000, 0) > 0)
    {
    ;//System.out.println("Read=" + new String(readbuf));
    }
    else
    {
    System.out.println("read err");
    break;
    }
    if(i%10 == 0)
    {
    System.out.println("Send " + i + " record, use=" + (System.currentTimeMillis() - nowtm) );
    nowtm = System.currentTimeMillis();
    }
    }
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    finally
    {
    streamSocket1.close();
    System.exit(0);
    }
    } public boolean connect()
    {
    return connect(m_sHost, m_nPort);
    }
    public boolean connect(String sHost, int nPort)
    {
    try{
    if(m_SocketInstance != null)
    m_SocketInstance.close();
    m_SocketInstance = new Socket(sHost, nPort);
    m_SocketInstance.setSoTimeout(1000);//set time out limits
    m_OutStream = new BufferedOutputStream(m_SocketInstance.getOutputStream());
    m_InStream = new BufferedInputStream(m_SocketInstance.getInputStream());
    return true;
    }
    catch(UnknownHostException e)
    {
    System.err.println(e);
    return false;
    }
    catch(Exception e)
    {
    System.err.println(e);
    return false;
    }
    }
    public int write(byte[] buff, int offset, int length, long flags) throws IOException
    {
    if(m_SocketInstance == null || !isActive())
    return -1;
    try{
    synchronized(this){
    m_OutStream.write(buff, offset, length);
    m_OutStream.flush();
    }
    return 0;
    }
    catch(IOException e)
    {
       System.err.println(e);
       throw e;
    }
    catch (Exception e)
    {
       System.err.println(e);
       return -1;
    }
       }
       public int read(byte[] buff, int length, long timesout, long flags) throws IOException
       {
       if(m_SocketInstance == null || !isActive())
    return -1;   try{
    int readsize = 0;
    synchronized(this){
    long lstm = System.currentTimeMillis();
    while(true)
    {
    try
    {
    if(System.currentTimeMillis() - lstm >= timesout)
    break;
    if(m_InStream.available() >= 0)
    readsize += m_InStream.read(buff, readsize, length - readsize);
    }
    catch(IOException e)
    {
    if(readsize > 0)
    break;
    throw e; }
    }
    }
       return readsize;
    }
    catch(Exception e)
    {
    System.err.println(e);
    return -1;
    }
    } public boolean isActive()
    {
    return m_SocketInstance == null ? false : m_SocketInstance.isConnected();
    } public void close()
    {
    try{
      if (m_SocketInstance != null)
      {
             m_SocketInstance.close();
             m_SocketInstance = null;
    }
    m_InStream = null;
    m_OutStream = null;
    }
       catch(Exception e)
       {
    System.err.println(e);
       }
    }