楼主的意思是说速度慢吗?差别不可能这样大,可能是你的代码有问题。
解决方案 »
- 急!急~运行时没有界面出现为什么的呀?怎么改?新手不懂请大侠们指点
- java程序可以调用oracle dbms_job.submit()存储过程来创建job
- 怎样在linux下获取本机ip地址
- 哈哈...终于找到感觉了,高兴,散分,随便问一下,这里有广东惠州人么?
- 请问哪里有java2 核心技术卷2可下(第6版 ),第5版也可,书店里没找到。谢谢!
- JFrame和Frame是什么关系,有什么区别
- 急急急!!!ie中怎么嵌入frame??有无其他办法那???高手快帮忙!
- java web start 一个怪现象,哪位高手请指教。。
- 要在IE中显示SWING里的控件光用HTMLCONV1_3转换就行了吗?请高手指教
- volatile关键字怎么不起作用
- javascript 疑难求解! (在线等待......)
- jcombox 获得用户自己输入,自动添加到JCOMBOX 的下拉框中,谢谢
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);
}
}