在一些基于C#的代码中,我通过webservice中的web应用程序的WebMethod将一个Java应用程序连接到一个Java服务器。不幸的是,这种情况发生得相当缓慢。例如,当Java服务器将一些数据回显给C#客户机时,我得到以下结果:
我读到过,人们可以用一些服务器应用进行实时通信(~60次/秒),有时甚至是数百万次流/秒。我的实施会有什么问题?它在一个打开的连接上发送多条消息,所以对象创建开销应该只显示在第一条消息上?为什么我的短信要花费500毫秒?
当web应用程序启动并连接到同一个C#服务器时,每次调用该web方法时,就会启动该webmethod。
public static IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
public static IPAddress ipAddress = ipHostInfo.AddressList[0];
public static IPEndPoint remoteEP = new IPEndPoint(ipAddress, 9999);
// Create a TCP/IP socket.
public static Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static int z = 0;
[WebMethod]
public BenchmarkData_ StartClient()
{
lock(lck)
{
z++;
if (z == 1)
{
sender.Connect(remoteEP);
}
}
int bytesRec = 0;
int boy = 0;
byte[] bytes = new byte[1024 * 1024];
int bytesSent = 0;
SocketFlags sf = new SocketFlags();
Stopwatch sw = new Stopwatch(); Stopwatch sw2 = new Stopwatch();
#region r
lock (lck)
{
sw.Start();
// Data buffer for incoming data.
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
// Create a TCP/IP socket.
sender.ReceiveBufferSize = 1024 * 1024;
sender.ReceiveTimeout = 1;
// Connect the socket to the remote endpoint. Catch any errors.
try
{
Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
// Send the data through the socket.
bytesSent = sender.Send(msg);
// Receive the response from the remote device.
sw.Stop();
sw2.Start();
while ((bytesRec = sender.Receive(bytes)) > 0)
{
boy += bytesRec;
}
Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
// Release the socket.
// sender.Shutdown(SocketShutdown.Both);
// sender.Close();
sw2.Stop();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
return new BenchmarkData_() { .... };
}以下是Java代码(半伪代码)
serverSocket=new ServerSocket(port); // in listener thread
Socket socket=serverSocket.accept(); // in listener thread
// in a dedicated thread per connection made:
out=new BufferedOutputStream( socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
boolean reading=true;
ArrayList<Byte> incoming=new ArrayList<Byte>();
while (in.available() == 0)
{
Thread.sleep(3);
}
while (in.available() > 0)
{
int bayt=-2;
try {
bayt=in.read();
} catch (IOException e) { e.printStackTrace(); }
if (bayt == -1)
{
reading = false;
}
else
{
incoming.add((byte) bayt);
}
}
byte [] incomingBuf=new byte[incoming.size()];
for(int i = 0; i < incomingBuf.length; i++)
{
incomingBuf[i] = incoming.get(i);
}
msg = new String(incomingBuf, StandardCharsets.UTF_8);
if (msg.length() < 8192)
System.out.println("Socket Thread: "+msg);
else
System.out.println("Socket Thread: long msg.");
OutputStreamWriter outW = new OutputStreamWriter(out);
System.out.println(socket.getReceiveBufferSize());
outW.write(testStr.toString()); // 32MB, 4MB, ... 1kB versions
outW.flush();发布于 2015-08-06 19:01:29
替换中解决的问题
while ((bytesRec = sender.Receive(bytes))>0)
{
boy += bytesRec;
}使用
while (sender.Available <= 0) ;
while (sender.Available>0)
{
bytesRec = sender.Receive(bytes);
boy += bytesRec;
}现在它以微秒为单位读取1kB,而不是500毫秒。因为它检查单个整数而不是试图在整个缓冲区上读取?也许吧。但它现在并不能读取从服务器发送的所有消息。它需要某种类型的标题才能知道要读多少。读取大约几千字节,即使服务器发送兆字节。
当服务器发送3MB而客户端读取的量完全相同时,则需要30 3MB。都在同一台机器里。试图读取服务器发送的内容(甚至一个字节)会引发异常,因此TCP发送的数量实际上与客户端所需的数量完全相同。
https://stackoverflow.com/questions/31842409
复制相似问题