在一些基于tutoral的代码中,我通过web服务中的Web应用程序WebMethod
将C#Web应用程序连接到Java套接字服务器。不幸的是,这种情况发生的很慢。例如,当Java服务器将一些数据回传给C#客户端时,我得到以下结果:
- 发送的数据大小= 32MB,总时间= 980毫秒(没问题)
- 发送的数据大小= 4MB,总时间= 530毫秒(变得稍慢)
- 发送的数据大小= 1MB,总时间= 520毫秒(绝对瓶颈)
- 发送的数据大小= 1kB,总时间= 516毫秒(这必须是某些事情的某种恒定延迟)
我读过人们可以通过一些服务器应用程序进行实时通信(~60 / s),有时甚至可以进行数百万次流。我的实施可能会出现什么问题?它通过单个打开的连接发送多条消息,因此对象创建开销只应出现在第一条消息中?为什么我的消息传递开销大约为500毫秒?
C#webmethod在web-app启动时启动,并且每次调用此web方法时都连接到同一个Java服务器。
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();
答案 0 :(得分:0)
替换
解决了问题while ((bytesRec = sender.Receive(bytes))>0)
{
boy += bytesRec;
}
与
while (sender.Available <= 0) ;
while (sender.Available>0)
{
bytesRec = sender.Receive(bytes);
boy += bytesRec;
}
现在以1毫秒读取而不是500毫秒的微秒。因为它检查单个整数而不是尝试读取整个缓冲区?也许。但它现在没有读取从服务器发送的所有消息。它需要某种类型的标题才能知道要阅读多少。即使服务器发送兆字节,也要读取大约几千字节。
当服务器发送3MB且客户端读取的数量完全相同时,需要30ms。都在同一台机器上。尝试读取超过服务器发送的内容(甚至单个字节)会引发异常,因此TCP确实发送了客户端所需的完全相同的数量。