关于C#Socket传输延迟的问题!!急,在线等!

loverjohn 2007-05-29 09:37:37
写了一个使用Socket传输的小程序.
2台不同路由下主机,一台为服务器,另一台为客户端.

程序为客户端向服务器发送一段字符串(19---30bpys),服务器响应并返回消息.基本与MSDN范例一样,只是我加上一个不断循环的线程,使得客户端能不断的向服务器发送消息,阻塞间隔时间为1秒.

在本机测试完全正常,从客户端发送和服务器响应时间和线程堵塞时间一致.
但拿到两台主机上测试,则出现客户端发送一条数据则平均要等到5秒左右才能有一条成功响应.虽然线程阻塞还是为1秒.
程序需要延迟稳定,起码需要达到700ms一次的传输速度.
刚刚开始接触Socket,所以有可能问题表达不是很清楚,有强人或以前有过类似问题并最后解决的,请帮忙分析一下,在下不盛感激.

在线等!
...全文
2010 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
bourbon1795 2010-10-20
  • 打赏
  • 举报
回复
防火墙也是影响的因素之一。
蔡袅 2010-10-20
  • 打赏
  • 举报
回复
我之前的做法是先判断网络是否稳定通畅,再Socket传输
sixpence_chen 2010-10-20
  • 打赏
  • 举报
回复
服务器端的程序这样的结构不好,只要一个消息阻塞,其他的消息也都阻塞了。网络中不可能每一个消息都是通畅的。
服务器端,最好一个Accept,用一个线程来处理。(手头没有C#的例子代码)
如果会用线程池,当然也可以。
zcty520 2010-10-20
  • 打赏
  • 举报
回复
楼主,我用你的代码生成程序在局域网中循环发送了10000遍,不做sleep的话,每秒能发送几百条数据,应该算正常。
呵呵。可能是你们网络延时造成的问题。
Manonloki 2010-10-20
  • 打赏
  • 举报
回复
MARK
littlegang 2007-05-29
  • 打赏
  • 举报
回复
简单的用Thread.sleep(1000);

个人感觉,这样似乎不大好,

另外不清楚,为何要sleep?
Qim 2007-05-29
  • 打赏
  • 举报
回复
在本机测试完全正常,从客户端发送和服务器响应时间和线程堵塞时间一致.
但拿到两台主机上测试,则出现客户端发送一条数据则平均要等到5秒左右才能有一条成功响应.虽然线程阻塞还是为1秒.
-------------------------------------------------------------------------
在一台机器上测试很正常,在网络上测试有很大延迟,各位说是什么原因呢??
在本机测试很正常,说明和代码没什么关系,在网络上测试有很大延迟,说明网络状况不是很好。网络的问题是程序不能解决的!
-------------------------------------------------------------------------
只能这样理解了.
loverjohn 2007-05-29
  • 打赏
  • 举报
回复
to Qim
实际上我在代码里面做过修改,打开连接后,程序结束后关闭socket,得到的结果还是一样,还是那恶心的延迟.
Qim 2007-05-29
  • 打赏
  • 举报
回复
问题出在服务器端,每收到一次数据,发送完后.就断开了当前连接,然后再重新等待连接.

这样相当于,每次发送接收都是一个独立的过程.重新绑定,连接是一个耗时的过程.
应该每次连接收功后,保持当前连接,把所有数据发送接收发送接收全部搞定后,再关闭连接.
wzd24 2007-05-29
  • 打赏
  • 举报
回复
在本机测试完全正常,从客户端发送和服务器响应时间和线程堵塞时间一致.
但拿到两台主机上测试,则出现客户端发送一条数据则平均要等到5秒左右才能有一条成功响应.虽然线程阻塞还是为1秒.
-------------------------------------------------------------------------
在一台机器上测试很正常,在网络上测试有很大延迟,各位说是什么原因呢??
在本机测试很正常,说明和代码没什么关系,在网络上测试有很大延迟,说明网络状况不是很好。网络的问题是程序不能解决的!
loverjohn 2007-05-29
  • 打赏
  • 举报
回复
to Qim
那到不是什么损失,本来也是msdn上的代码

//////////////////服务器端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener {

// Incoming data from the client.
public static string data = null;

public static void StartListening() {
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Bind the socket to the local endpoint and
// listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(10);

// Start listening for connections.
while (true) {
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;

// An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<e>") > -1) {
break;
}
}

// Show the data on the console.
Console.WriteLine( "Text received : {0}", data);

// Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data);

handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}

} catch (Exception e) {
Console.WriteLine(e.ToString());
}

Console.WriteLine("\nPress ENTER to continue...");
Console.Read();

}

public static int Main(String[] args) {
StartListening();
return 0;
}
}
///////////客户端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketClient {


public static void StartClient() {
// Data buffer for incoming data.
byte[] bytes = new byte[1024];

// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);

// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP);

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<e>");

// Send the data through the socket.
int bytesSent = sender.Send(msg);

// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec));

// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();

} 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());
}
}

public static int Main(String[] args) {
StartClient();
return 0;
}
}
Qim 2007-05-29
  • 打赏
  • 举报
回复
只把接收和发送的代码贴一下.不会给你带来损失吧?
估计大家看了代码,很快会有思路.
只在这里干想.不知道想到啥时候?
loverjohn 2007-05-29
  • 打赏
  • 举报
回复
to littlegang
简单的用Thread.sleep(1000);
loverjohn 2007-05-29
  • 打赏
  • 举报
回复
to forgot
没用c++,一是不是很熟悉,我是从java转到C#的,二是觉得只是一个简单的socket,所以就没用,c#足够胜任吧.

to twoboy
实际上我把msdn上面异步的例程照搬下来,放上去还是一样的延迟,似乎发送过去的字符串也是需要5秒左右才能接收并响应
littlegang 2007-05-29
  • 打赏
  • 举报
回复
客户端“线程阻塞还是为1秒”

使用什么方式阻塞的?

Twoboy 2007-05-29
  • 打赏
  • 举报
回复
对于Socket程序,不要偷懒,写个通讯类,结构会清晰一些
loverjohn 2007-05-29
  • 打赏
  • 举报
回复
to Qim
能否给点代码提示呢?你做的是同步socket还是异步socket?
Twoboy 2007-05-29
  • 打赏
  • 举报
回复
为什么不用异步呢?我以前做过一个Socket通信程序,只是用的是异步多个客户端,没有出小如上问题,但是没在不同的网段试过
forgot 2007-05-29
  • 打赏
  • 举报
回复
參考http://www.codeproject.com/cs/internet/socketsincs.asp,有詳細的教程,還有Demo和源碼,應該能滿足的要求,不懂再問。

建議:用C++做Socket速度可能更快。
Qim 2007-05-29
  • 打赏
  • 举报
回复
以前做过socket,没有楼主说的那种情况.
加载更多回复(9)

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧